Ei kuvausta

Helpers.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. // Exit if accessed directly
  3. /*******************************************************************************
  4. * Copyright (c) 2019, Code Atlantic LLC
  5. ******************************************************************************/
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9. class PUM_Helpers {
  10. public static function do_shortcode( $shortcode_text = '' ) {
  11. ob_start();
  12. $content = do_shortcode( $shortcode_text );
  13. $ob_content = ob_get_clean();
  14. if ( ! empty( $ob_content ) ) {
  15. $content .= $ob_content;
  16. }
  17. return $content;
  18. }
  19. public static function get_shortcodes_from_content( $content ) {
  20. $pattern = get_shortcode_regex();
  21. $shortcodes = array();
  22. if ( preg_match_all( '/' . $pattern . '/s', $content, $matches ) ) {
  23. foreach ( $matches[0] as $key => $value ) {
  24. $shortcodes[ $key ] = array(
  25. 'full_text' => $value,
  26. 'tag' => $matches[2][ $key ],
  27. 'atts' => shortcode_parse_atts( $matches[3][ $key ] ),
  28. 'content' => $matches[5][ $key ],
  29. );
  30. if ( ! empty( $shortcodes[ $key ]['atts'] ) ) {
  31. foreach ( $shortcodes[ $key ]['atts'] as $attr_name => $attr_value ) {
  32. // Filter numeric keys as they are valueless/truthy attributes.
  33. if ( is_numeric( $attr_name ) ) {
  34. $shortcodes[ $key ]['atts'][ $attr_value ] = true;
  35. unset( $shortcodes[ $key ]['atts'][ $attr_name ] );
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return $shortcodes;
  42. }
  43. /**
  44. * Gets the directory caching should be stored in.
  45. *
  46. * Accounts for various adblock bypass options.
  47. *
  48. * @return bool|string
  49. */
  50. public static function get_cache_dir_url() {
  51. $upload_dir = self::get_upload_dir_url();
  52. if ( false === $upload_dir ) {
  53. return false;
  54. }
  55. if ( ! pum_get_option( 'bypass_adblockers', false ) ) {
  56. return trailingslashit( $upload_dir ) . 'pum';
  57. }
  58. return $upload_dir;
  59. }
  60. /**
  61. * Gets the uploads directory path
  62. *
  63. * @since 1.10
  64. * @param string $path A path to append to end of upload directory URL.
  65. * @return bool|string The uploads directory path or false on failure
  66. */
  67. public static function get_upload_dir_path( $path = '' ) {
  68. $upload_dir = self::get_upload_dir();
  69. if ( false !== $upload_dir && isset( $upload_dir['basedir'] ) ) {
  70. $dir = $upload_dir['basedir'];
  71. if ( ! empty( $path ) ) {
  72. $dir = trailingslashit( $dir ) . $path;
  73. }
  74. return $dir;
  75. } else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * Gets the uploads directory URL
  81. *
  82. * @since 1.10
  83. * @param string $path A path to append to end of upload directory URL.
  84. * @return bool|string The uploads directory URL or false on failure
  85. */
  86. public static function get_upload_dir_url( $path = '' ) {
  87. $upload_dir = self::get_upload_dir();
  88. if ( false !== $upload_dir && isset( $upload_dir['baseurl'] ) ) {
  89. $url = preg_replace( '/^https?:/', '', $upload_dir['baseurl'] );
  90. if ( null === $url ) {
  91. return false;
  92. }
  93. if ( ! empty( $path ) ) {
  94. $url = trailingslashit( $url ) . $path;
  95. }
  96. return $url;
  97. } else {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Gets the Uploads directory
  103. *
  104. * @since 1.10
  105. * @return bool|array An associated array with baseurl and basedir or false on failure
  106. */
  107. public static function get_upload_dir() {
  108. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  109. $wp_upload_dir = wp_get_upload_dir();
  110. } else {
  111. $wp_upload_dir = wp_upload_dir();
  112. }
  113. if ( isset( $wp_upload_dir['error'] ) && false !== $wp_upload_dir['error'] ) {
  114. PUM_Utils_Logging::instance()->log( sprintf( 'Getting uploads directory failed. Error given: %s', esc_html( $wp_upload_dir['error'] ) ) );
  115. return false;
  116. } else {
  117. return $wp_upload_dir;
  118. }
  119. }
  120. /**
  121. * @deprecated Use get_upload_dir_url instead.
  122. */
  123. public static function upload_dir_url( $path = '' ) {
  124. $upload_dir = wp_upload_dir();
  125. $upload_dir = $upload_dir['baseurl'];
  126. $upload_dir = preg_replace( '/^https?:/', '', $upload_dir );
  127. if ( ! empty ( $path ) ) {
  128. $upload_dir = trailingslashit( $upload_dir ) . $path;
  129. }
  130. return $upload_dir;
  131. }
  132. /**
  133. * Sort array by priority value
  134. *
  135. * @param $a
  136. * @param $b
  137. *
  138. * @return int
  139. * @see PUM_Utils_Array::sort_by_priority instead.
  140. *
  141. * @deprecated 1.7.20
  142. */
  143. public static function sort_by_priority( $a, $b ) {
  144. return PUM_Utils_Array::sort_by_priority( $a, $b );
  145. }
  146. /**
  147. * Sort nested arrays with various options.
  148. *
  149. * @param array $array
  150. * @param string $type
  151. * @param bool $reverse
  152. *
  153. * @return array
  154. * @deprecated 1.7.20
  155. * @see PUM_Utils_Array::sort instead.
  156. *
  157. */
  158. public static function sort_array( $array = array(), $type = 'key', $reverse = false ) {
  159. return PUM_Utils_Array::sort( $array, $type, $reverse );
  160. }
  161. public static function post_type_selectlist_query( $post_type, $args = array(), $include_total = false ) {
  162. $args = wp_parse_args( $args, array(
  163. 'posts_per_page' => 10,
  164. 'post_type' => $post_type,
  165. 'post__in' => null,
  166. 'post__not_in' => null,
  167. 'post_status' => null,
  168. 'page' => 1,
  169. // Performance Optimization.
  170. 'no_found_rows' => ! $include_total ? true : false,
  171. 'update_post_term_cache' => false,
  172. 'update_post_meta_cache' => false,
  173. ) );
  174. if ( $post_type == 'attachment' ) {
  175. $args['post_status'] = 'inherit';
  176. }
  177. // Query Caching.
  178. static $queries = array();
  179. $key = md5( serialize( $args ) );
  180. if ( ! isset( $queries[ $key ] ) ) {
  181. $query = new WP_Query( $args );
  182. $posts = array();
  183. foreach ( $query->posts as $post ) {
  184. $posts[ $post->ID ] = $post->post_title;
  185. }
  186. $results = array(
  187. 'items' => $posts,
  188. 'total_count' => $query->found_posts,
  189. );
  190. $queries[ $key ] = $results;
  191. } else {
  192. $results = $queries[ $key ];
  193. }
  194. return ! $include_total ? $results['items'] : $results;
  195. }
  196. public static function taxonomy_selectlist_query( $taxonomies = array(), $args = array(), $include_total = false ) {
  197. if ( empty ( $taxonomies ) ) {
  198. $taxonomies = array( 'category' );
  199. }
  200. $args = wp_parse_args( $args, array(
  201. 'hide_empty' => false,
  202. 'number' => 10,
  203. 'search' => '',
  204. 'include' => null,
  205. 'exclude' => null,
  206. 'offset' => 0,
  207. 'page' => null,
  208. ) );
  209. if ( $args['page'] ) {
  210. $args['offset'] = ( $args['page'] - 1 ) * $args['number'];
  211. }
  212. // Query Caching.
  213. static $queries = array();
  214. $key = md5( serialize( $args ) );
  215. if ( ! isset( $queries[ $key ] ) ) {
  216. $terms = array();
  217. foreach ( get_terms( $taxonomies, $args ) as $term ) {
  218. $terms[ $term->term_id ] = $term->name;
  219. }
  220. $total_args = $args;
  221. unset( $total_args['number'] );
  222. unset( $total_args['offset'] );
  223. $results = array(
  224. 'items' => $terms,
  225. 'total_count' => $include_total ? wp_count_terms( $taxonomies, $total_args ) : null,
  226. );
  227. $queries[ $key ] = $results;
  228. } else {
  229. $results = $queries[ $key ];
  230. }
  231. return ! $include_total ? $results['items'] : $results;
  232. }
  233. /**
  234. * @param array $args
  235. * @param bool $include_total
  236. *
  237. * @return array|mixed
  238. */
  239. public static function user_selectlist_query( $args = array(), $include_total = false ) {
  240. $args = wp_parse_args( $args, array(
  241. 'role' => null,
  242. 'count_total' => ! $include_total ? true : false,
  243. ) );
  244. // Query Caching.
  245. static $queries = array();
  246. $key = md5( serialize( $args ) );
  247. if ( ! isset( $queries[ $key ] ) ) {
  248. $query = new WP_User_Query( $args );
  249. $users = array();
  250. foreach ( $query->get_results() as $user ) {
  251. /** @var WP_User $user */
  252. $users[ $user->ID ] = $user->display_name;
  253. }
  254. $results = array(
  255. 'items' => $users,
  256. 'total_count' => $query->get_total(),
  257. );
  258. $queries[ $key ] = $results;
  259. } else {
  260. $results = $queries[ $key ];
  261. }
  262. return ! $include_total ? $results['items'] : $results;
  263. }
  264. public static function popup_theme_selectlist() {
  265. $themes = array();
  266. foreach ( pum_get_all_themes() as $theme ) {
  267. $themes[ $theme->ID ] = $theme->post_title;
  268. }
  269. return $themes;
  270. }
  271. public static function popup_selectlist( $args = array() ) {
  272. $popup_list = array();
  273. $popups = pum_get_all_popups( $args );
  274. foreach ( $popups as $popup ) {
  275. if ( $popup->is_published() ) {
  276. $popup_list[ (string) $popup->ID ] = $popup->post_title;
  277. }
  278. }
  279. return $popup_list;
  280. }
  281. }