Нет описания

Functions.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. namespace MailPoet\WP;
  3. if (!defined('ABSPATH')) exit;
  4. use Plugin_Upgrader;
  5. use WP_Ajax_Upgrader_Skin;
  6. use WP_Error;
  7. class Functions {
  8. private static $instance;
  9. /**
  10. * @return Functions
  11. */
  12. public static function get() {
  13. if (self::$instance === null) {
  14. self::$instance = new Functions;
  15. }
  16. return self::$instance;
  17. }
  18. public static function set(Functions $instance) {
  19. self::$instance = $instance;
  20. }
  21. /**
  22. * @param string $tag
  23. * @param mixed ...$args
  24. * @return mixed
  25. */
  26. public function doAction($tag, ...$args) {
  27. return call_user_func_array('do_action', func_get_args());
  28. }
  29. /**
  30. * @param string $tag
  31. * @param mixed ...$args
  32. * @return mixed
  33. */
  34. public function applyFilters($tag, ...$args) {
  35. return call_user_func_array('apply_filters', func_get_args());
  36. }
  37. /**
  38. * @param string $tag
  39. * @param callable $functionToAdd
  40. * @param int $priority
  41. * @param int $acceptedArgs
  42. * @return bool
  43. */
  44. public function addAction($tag, $functionToAdd, $priority = 10, $acceptedArgs = 1) {
  45. return call_user_func_array('add_action', func_get_args());
  46. }
  47. public function __($text, $domain = 'default') {
  48. return __($text, $domain);
  49. }
  50. public function _e($text, $domain = 'default') {
  51. return _e($text, $domain);
  52. }
  53. public function _n($single, $plural, $number, $domain = 'default') {
  54. return _n($single, $plural, $number, $domain);
  55. }
  56. public function _x($text, $context, $domain = 'default') {
  57. return _x($text, $context, $domain);
  58. }
  59. public function addCommentMeta($commentId, $metaKey, $metaValue, $unique = false) {
  60. return add_comment_meta($commentId, $metaKey, $metaValue, $unique);
  61. }
  62. public function addFilter($tag, callable $functionToAdd, $priority = 10, $acceptedArgs = 1) {
  63. return add_filter($tag, $functionToAdd, $priority, $acceptedArgs);
  64. }
  65. /**
  66. * @param bool|array $crop
  67. */
  68. public function addImageSize($name, $width = 0, $height = 0, $crop = false) {
  69. return add_image_size($name, $width, $height, $crop);
  70. }
  71. public function addMenuPage($pageTitle, $menuTitle, $capability, $menuSlug, callable $function = null, $iconUrl = '', $position = null) {
  72. if (is_null($function)) {
  73. $function = function () {
  74. };
  75. }
  76. return add_menu_page($pageTitle, $menuTitle, $capability, $menuSlug, $function, $iconUrl, $position);
  77. }
  78. public function addQueryArg($key, $value = false, $url = false) {
  79. return add_query_arg($key, $value, $url);
  80. }
  81. public function addScreenOption($option, $args = []) {
  82. return add_screen_option($option, $args);
  83. }
  84. public function addShortcode($tag, callable $callback) {
  85. return add_shortcode($tag, $callback);
  86. }
  87. public function addSubmenuPage($parentSlug, $pageTitle, $menuTitle, $capability, $menuSlug, callable $function) {
  88. return add_submenu_page($parentSlug, $pageTitle, $menuTitle, $capability, $menuSlug, $function);
  89. }
  90. public function adminUrl($path = '', $scheme = 'admin') {
  91. return admin_url($path, $scheme);
  92. }
  93. public function currentFilter() {
  94. return current_filter();
  95. }
  96. public function currentTime($type, $gmt = false) {
  97. return current_time($type, $gmt);
  98. }
  99. public function currentUserCan($capability) {
  100. return current_user_can($capability);
  101. }
  102. public function dateI18n($dateformatstring, $timestampWithOffset = false, $gmt = false) {
  103. return date_i18n($dateformatstring, $timestampWithOffset, $gmt);
  104. }
  105. public function deleteCommentMeta($commentId, $metaKey, $metaValue = '') {
  106. return delete_comment_meta($commentId, $metaKey, $metaValue);
  107. }
  108. public function deleteOption($option) {
  109. return delete_option($option);
  110. }
  111. public function doShortcode($content, $ignoreHtml = false) {
  112. return do_shortcode($content, $ignoreHtml);
  113. }
  114. public function escAttr($text) {
  115. return esc_attr($text);
  116. }
  117. public function escHtml($text) {
  118. return esc_html($text);
  119. }
  120. public function escSql($sql) {
  121. return esc_sql($sql);
  122. }
  123. public function getBloginfo($show = '', $filter = 'raw') {
  124. return get_bloginfo($show, $filter);
  125. }
  126. public function getCategories($args = '') {
  127. return get_categories($args);
  128. }
  129. public function getTags($args = '') {
  130. return get_tags($args);
  131. }
  132. public function getComment($comment = null, $output = OBJECT) {
  133. return get_comment($comment, $output);
  134. }
  135. public function getCommentMeta($commentId, $key = '', $single = false) {
  136. return get_comment_meta($commentId, $key, $single);
  137. }
  138. public function getCurrentScreen() {
  139. return get_current_screen();
  140. }
  141. public function getCurrentUserId() {
  142. return get_current_user_id();
  143. }
  144. public function getDateFromGmt($string, $format = 'Y-m-d H:i:s') {
  145. return get_date_from_gmt($string, $format);
  146. }
  147. public function getGmtFromDate($string, $format = 'Y-m-d H:i:s') {
  148. return get_gmt_from_date($string, $format);
  149. }
  150. public function getEditProfileUrl($userId = 0, $scheme = 'admin') {
  151. return get_edit_profile_url($userId, $scheme);
  152. }
  153. public function getEditableRoles() {
  154. return get_editable_roles();
  155. }
  156. public function getLocale() {
  157. return get_locale();
  158. }
  159. public function getObjectTaxonomies($object, $output = 'names') {
  160. return get_object_taxonomies($object, $output);
  161. }
  162. public function getOption($option, $default = false) {
  163. return get_option($option, $default);
  164. }
  165. public function getPages($args = []) {
  166. return get_pages($args);
  167. }
  168. public function getPermalink($post, $leavename = false) {
  169. return get_permalink($post, $leavename);
  170. }
  171. public function getPluginPageHook($pluginPage, $parentPage) {
  172. return get_plugin_page_hook($pluginPage, $parentPage);
  173. }
  174. public function getPluginUpdates() {
  175. return get_plugin_updates();
  176. }
  177. public function getPlugins($pluginFolder = '') {
  178. return get_plugins($pluginFolder);
  179. }
  180. public function getPost($post = null, $output = OBJECT, $filter = 'raw') {
  181. return get_post($post, $output, $filter);
  182. }
  183. public function hasCategory($category = '', $post = null): bool {
  184. return has_category($category, $post);
  185. }
  186. public function hasTag($tag = '', $post = null): bool {
  187. return has_tag($tag, $post);
  188. }
  189. public function hasTerm($term = '', $taxonomy = '', $post = null): bool {
  190. return has_term($term, $taxonomy, $post);
  191. }
  192. public function getPostThumbnailId($post = null) {
  193. return get_post_thumbnail_id($post);
  194. }
  195. public function getPostTypes($args = [], $output = 'names', $operator = 'and') {
  196. return get_post_types($args, $output, $operator);
  197. }
  198. public function getPostType($post = null) {
  199. return get_post_type($post);
  200. }
  201. public function getPosts(array $args = null) {
  202. return get_posts($args);
  203. }
  204. public function getRole($role) {
  205. return get_role($role);
  206. }
  207. public function getSiteOption($option, $default = false, $deprecated = true) {
  208. return get_site_option($option, $default, $deprecated);
  209. }
  210. public function getSiteUrl($blogId = null, $path = '', $scheme = null) {
  211. return get_site_url($blogId, $path, $scheme);
  212. }
  213. public function getTemplatePart($slug, $name = null) {
  214. return get_template_part($slug, $name);
  215. }
  216. /**
  217. * @param string|array $args
  218. * @param string|array $deprecated
  219. */
  220. public function getTerms($args = [], $deprecated = '') {
  221. return get_terms($args, $deprecated);
  222. }
  223. /**
  224. * @param int|false $userId
  225. */
  226. public function getTheAuthorMeta($field = '', $userId = false) {
  227. return get_the_author_meta($field, $userId);
  228. }
  229. /**
  230. * @return false|int
  231. */
  232. public function getTheId() {
  233. return get_the_ID();
  234. }
  235. /**
  236. * @param int|\WP_User $userId
  237. */
  238. public function getUserLocale($userId = 0) {
  239. return get_user_locale($userId);
  240. }
  241. public function getUserMeta($userId, $key = '', $single = false) {
  242. return get_user_meta($userId, $key, $single);
  243. }
  244. public function getUserdata($userId) {
  245. return get_userdata($userId);
  246. }
  247. public function getUserBy($field, $value) {
  248. return get_user_by($field, $value);
  249. }
  250. public function hasFilter($tag, $functionToCheck = false) {
  251. return has_filter($tag, $functionToCheck);
  252. }
  253. public function homeUrl($path = '', $scheme = null) {
  254. return home_url($path, $scheme);
  255. }
  256. public function includesUrl($path = '', $scheme = null) {
  257. return includes_url($path, $scheme);
  258. }
  259. public function isAdmin() {
  260. return is_admin();
  261. }
  262. public function isEmail($email, $deprecated = false) {
  263. return is_email($email, $deprecated);
  264. }
  265. public function isMultisite() {
  266. return is_multisite();
  267. }
  268. public function isPluginActive($plugin) {
  269. return is_plugin_active($plugin);
  270. }
  271. public function isRtl() {
  272. return is_rtl();
  273. }
  274. public function isSerialized($data, $strict = true) {
  275. return is_serialized($data, $strict);
  276. }
  277. public function isUserLoggedIn() {
  278. return is_user_logged_in();
  279. }
  280. /**
  281. * @param string|false $deprecated
  282. * @param string|false $pluginRelPath
  283. */
  284. public function loadPluginTextdomain($domain, $deprecated = false, $pluginRelPath = false) {
  285. return load_plugin_textdomain($domain, $deprecated, $pluginRelPath);
  286. }
  287. public function loadTextdomain($domain, $mofile) {
  288. return load_textdomain($domain, $mofile);
  289. }
  290. public function numberFormatI18n($number, $decimals = 0) {
  291. return number_format_i18n($number, $decimals);
  292. }
  293. public function pluginBasename($file) {
  294. return plugin_basename($file);
  295. }
  296. public function pluginsUrl($path = '', $plugin = '') {
  297. return plugins_url($path, $plugin);
  298. }
  299. public function registerActivationHook($file, $function) {
  300. return register_activation_hook($file, $function);
  301. }
  302. public function registerPostType($postType, $args = []) {
  303. return register_post_type($postType, $args);
  304. }
  305. public function registerWidget($widget) {
  306. return register_widget($widget);
  307. }
  308. /**
  309. * @param string $tag
  310. * @param callable $functionToRemove
  311. * @param int $priority
  312. */
  313. public function removeAction($tag, $functionToRemove, $priority = 10) {
  314. return remove_action($tag, $functionToRemove, $priority);
  315. }
  316. public function removeAllActions($tag, $priority = false) {
  317. return remove_all_actions($tag, $priority);
  318. }
  319. public function removeAllFilters($tag, $priority = false) {
  320. return remove_all_filters($tag, $priority);
  321. }
  322. public function removeFilter($tag, callable $functionToRemove, $priority = 10) {
  323. return remove_filter($tag, $functionToRemove, $priority);
  324. }
  325. public function removeShortcode($tag) {
  326. return remove_shortcode($tag);
  327. }
  328. public function selfAdminUrl($path = '', $scheme = 'admin') {
  329. return self_admin_url($path, $scheme);
  330. }
  331. public function setTransient($transient, $value, $expiration = 0) {
  332. return set_transient($transient, $value, $expiration);
  333. }
  334. public function getTransient($transient) {
  335. return get_transient($transient);
  336. }
  337. public function deleteTransient($transient) {
  338. return delete_transient($transient);
  339. }
  340. public function shortcodeParseAtts($text) {
  341. return shortcode_parse_atts($text);
  342. }
  343. public function singlePostTitle($prefix = '', $display = true) {
  344. return single_post_title($prefix, $display);
  345. }
  346. public function siteUrl($path = '', $scheme = null) {
  347. return site_url($path, $scheme);
  348. }
  349. public function statusHeader($code, $description = '') {
  350. return status_header($code, $description);
  351. }
  352. public function stripslashesDeep($value) {
  353. return stripslashes_deep($value);
  354. }
  355. public function translate($text, $domain = 'default') {
  356. return translate($text, $domain);
  357. }
  358. public function unloadTextdomain($domain) {
  359. return unload_textdomain($domain);
  360. }
  361. public function updateOption($option, $value, $autoload = null) {
  362. return update_option($option, $value, $autoload);
  363. }
  364. public function wpAddInlineScript($handle, $data, $position = 'after') {
  365. return wp_add_inline_script($handle, $data, $position);
  366. }
  367. public function wpCreateNonce($action = -1) {
  368. return wp_create_nonce($action);
  369. }
  370. public function wpDequeueScript($handle) {
  371. return wp_dequeue_script($handle);
  372. }
  373. public function wpDequeueStyle($handle) {
  374. return wp_dequeue_style($handle);
  375. }
  376. public function wpEncodeEmoji($content) {
  377. return wp_encode_emoji($content);
  378. }
  379. public function wpEnqueueMedia(array $args = []) {
  380. return wp_enqueue_media($args);
  381. }
  382. public function wpEnqueueScript($handle, $src = '', array $deps = [], $ver = false, $inFooter = false) {
  383. return wp_enqueue_script($handle, $src, $deps, $ver, $inFooter);
  384. }
  385. public function wpEnqueueStyle($handle, $src = '', array $deps = [], $ver = false, $media = 'all') {
  386. return wp_enqueue_style($handle, $src, $deps, $ver, $media);
  387. }
  388. /**
  389. * @param string|\WP_Block_Type $name
  390. * @param array $args {
  391. * @return \WP_Block_Type|false
  392. */
  393. public function registerBlockType($name, $args = []) {
  394. return register_block_type($name, $args);
  395. }
  396. public function wpGetAttachmentImageSrc($attachmentId, $size = 'thumbnail', $icon = false) {
  397. return wp_get_attachment_image_src($attachmentId, $size, $icon);
  398. }
  399. public function wpGetCurrentUser() {
  400. return wp_get_current_user();
  401. }
  402. public function wpGetPostTerms($postId, $taxonomy = 'post_tag', array $args = []) {
  403. return wp_get_post_terms($postId, $taxonomy, $args);
  404. }
  405. public function wpGetReferer() {
  406. return wp_get_referer();
  407. }
  408. public function wpGetTheme($stylesheet = null, $themeRoot = null) {
  409. return wp_get_theme($stylesheet, $themeRoot);
  410. }
  411. public function wpInsertPost(array $postarr, $wpError = false) {
  412. return wp_insert_post($postarr, $wpError);
  413. }
  414. public function wpDeletePost(int $id, $force = false) {
  415. return wp_delete_post($id, $force);
  416. }
  417. public function wpJsonEncode($data, $options = 0, $depth = 512) {
  418. return wp_json_encode($data, $options, $depth);
  419. }
  420. public function wpLocalizeScript($handle, $objectName, array $l10n) {
  421. return wp_localize_script($handle, $objectName, $l10n);
  422. }
  423. public function wpLoginUrl($redirect = '', $forceReauth = false) {
  424. return wp_login_url($redirect, $forceReauth);
  425. }
  426. public function wpParseArgs($args, $defaults = '') {
  427. return wp_parse_args($args, $defaults);
  428. }
  429. public function wpParseUrl($url, $component = -1) {
  430. return wp_parse_url($url, $component);
  431. }
  432. public function wpSpecialcharsDecode($string, $quoteStyle = ENT_NOQUOTES ) {
  433. return wp_specialchars_decode($string, $quoteStyle);
  434. }
  435. public function wpPrintScripts($handles = false) {
  436. return wp_print_scripts($handles);
  437. }
  438. public function wpRemoteGet($url, array $args = []) {
  439. return wp_remote_get($url, $args);
  440. }
  441. public function wpRemotePost($url, array $args = []) {
  442. return wp_remote_post($url, $args);
  443. }
  444. public function wpRemoteRetrieveBody($response) {
  445. return wp_remote_retrieve_body($response);
  446. }
  447. public function wpRemoteRetrieveResponseCode($response) {
  448. return wp_remote_retrieve_response_code($response);
  449. }
  450. public function wpRemoteRetrieveResponseMessage($response) {
  451. return wp_remote_retrieve_response_message($response);
  452. }
  453. public function wpSafeRedirect($location, $status = 302) {
  454. return wp_safe_redirect($location, $status);
  455. }
  456. public function wpSetCurrentUser($id, $name = '') {
  457. return wp_set_current_user($id, $name);
  458. }
  459. public function wpStaticizeEmoji($text) {
  460. return wp_staticize_emoji($text);
  461. }
  462. public function wpTrimWords($text, $numWords = 55, $more = null) {
  463. return wp_trim_words($text, $numWords, $more);
  464. }
  465. public function wpUploadDir($time = null, $createDir = true, $refreshCache = false) {
  466. return wp_upload_dir($time, $createDir, $refreshCache);
  467. }
  468. public function wpVerifyNonce($nonce, $action = -1) {
  469. return wp_verify_nonce($nonce, $action);
  470. }
  471. public function wpautop($pee, $br = true) {
  472. return wpautop($pee, $br);
  473. }
  474. public function inTheLoop(): bool {
  475. return in_the_loop();
  476. }
  477. public function isMainQuery(): bool {
  478. return is_main_query();
  479. }
  480. /**
  481. * @param string $action
  482. * @param array|object $args
  483. * @return object|array|WP_Error
  484. */
  485. public function pluginsApi($action, $args = []) {
  486. require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  487. return plugins_api($action, $args);
  488. }
  489. /**
  490. * @param int|string|array $post Optional. Post ID, title, slug, or array of such. Default empty.
  491. * @return bool Whether the query is for an existing single post.
  492. */
  493. public function isSingle($post = '') {
  494. return is_single($post);
  495. }
  496. /**
  497. * @param int|string|array $page Optional. Page ID, title, slug, or array of such. Default empty.
  498. * @return bool Whether the query is for an existing single page.
  499. */
  500. public function isPage($page = '') {
  501. return is_page($page);
  502. }
  503. /**
  504. * @param string|array $postTypes Optional. Post type or array of post types. Default empty.
  505. * @return bool Whether the query is for an existing single post of any of the given post types.
  506. */
  507. public function isSingular($postTypes = ''): bool {
  508. return is_singular($postTypes);
  509. }
  510. /**
  511. * @param string $package
  512. * @param array $args
  513. * @return bool|WP_Error
  514. */
  515. public function installPlugin($package, $args = []) {
  516. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  517. require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
  518. require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
  519. $upgrader = new Plugin_Upgrader(new WP_Ajax_Upgrader_Skin());
  520. return $upgrader->install($package, $args);
  521. }
  522. /**
  523. * @param string $plugin
  524. * @param string $redirect
  525. * @param bool $networkWide
  526. * @param bool $silent
  527. * @return WP_Error|null
  528. */
  529. public function activatePlugin($plugin, $redirect = '', $networkWide = false, $silent = false) {
  530. return activate_plugin($plugin, $redirect, $networkWide, $silent);
  531. }
  532. public function wpGetAttachmentImageSrcset(int $attachmentId, $size = 'medium', $imageMeta = null) {
  533. return wp_get_attachment_image_srcset($attachmentId, $size, $imageMeta);
  534. }
  535. public function getResultsFromWpDb($query, ...$args) {
  536. global $wpdb;
  537. return $wpdb->get_results($wpdb->prepare($query, $args));
  538. }
  539. /**
  540. * @return string|null Prefixed table name
  541. */
  542. public function getWPTableName(string $table) {
  543. global $wpdb;
  544. if (property_exists($wpdb, $table)) {
  545. return $wpdb->$table;
  546. }
  547. return null;
  548. }
  549. /**
  550. * @param string $host
  551. * @return array|bool
  552. */
  553. public function parseDbHost($host) {
  554. global $wpdb;
  555. return $wpdb->parse_db_host($host);
  556. }
  557. /**
  558. * @param int|\WP_Post $post
  559. * @param string $context
  560. * @return string|null
  561. */
  562. public function getEditPostLink($post, string $context = 'display') {
  563. return get_edit_post_link($post, $context);
  564. }
  565. /**
  566. * @param string $string
  567. * @param array[]|string $allowedHtml
  568. * @param array $allowedProtocols
  569. * @return string
  570. */
  571. public function wpKses(string $string, $allowedHtml, $allowedProtocols = []) {
  572. return wp_kses($string, $allowedHtml, $allowedProtocols);
  573. }
  574. public function deprecatedHook(string $hook_name, string $version, string $replacement, string $message) {
  575. _deprecated_hook($hook_name, $version, $replacement, $message);
  576. }
  577. public function getTheContent($moreLinkText = null, $stripTeaser = false, $post = null) {
  578. return get_the_content($moreLinkText, $stripTeaser, $post);
  579. }
  580. public function getTheExcerpt($post = null) {
  581. return get_the_excerpt($post);
  582. }
  583. public function hasExcerpt($post = null) {
  584. return has_excerpt($post);
  585. }
  586. }