Geen omschrijving

class-functions.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. if ( ! class_exists( 'UM_Functions' ) ) {
  3. /**
  4. * Class UM_Functions
  5. */
  6. class UM_Functions {
  7. /**
  8. * Store URL
  9. *
  10. * @var string
  11. */
  12. var $store_url = 'https://ultimatemember.com/';
  13. /**
  14. * WP remote Post timeout
  15. * @var int
  16. */
  17. var $request_timeout = 60;
  18. /**
  19. * UM_Functions constructor.
  20. */
  21. function __construct() {
  22. }
  23. /**
  24. * Check if AJAX now
  25. *
  26. * @return bool
  27. */
  28. function is_ajax() {
  29. return function_exists( 'wp_doing_ajax' ) ? wp_doing_ajax() : defined( 'DOING_AJAX' );
  30. }
  31. /**
  32. * Check frontend nonce
  33. *
  34. * @param bool $action
  35. */
  36. function check_ajax_nonce( $action = false ) {
  37. $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( $_REQUEST['nonce'] ) : '';
  38. $action = empty( $action ) ? 'um-frontend-nonce' : $action;
  39. if ( ! wp_verify_nonce( $nonce, $action ) ) {
  40. wp_send_json_error( esc_js( __( 'Wrong Nonce', 'ultimate-member' ) ) );
  41. }
  42. }
  43. /**
  44. * What type of request is this?
  45. *
  46. * @param string $type String containing name of request type (ajax, frontend, cron or admin)
  47. *
  48. * @return bool
  49. */
  50. public function is_request( $type ) {
  51. switch ( $type ) {
  52. case 'admin' :
  53. return is_admin();
  54. case 'ajax' :
  55. return defined( 'DOING_AJAX' );
  56. case 'cron' :
  57. return defined( 'DOING_CRON' );
  58. case 'frontend' :
  59. return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
  60. }
  61. return false;
  62. }
  63. /**
  64. * Help Tip displaying
  65. *
  66. * Function for render/displaying UltimateMember help tip
  67. *
  68. * @since 2.0.0
  69. *
  70. * @param string $tip Help tip text
  71. * @param bool $allow_html Allow sanitized HTML if true or escape
  72. * @param bool $echo Return HTML or echo
  73. * @return string
  74. */
  75. function tooltip( $tip, $allow_html = false, $echo = true ) {
  76. if ( $allow_html ) {
  77. $tip = htmlspecialchars( wp_kses( html_entity_decode( $tip ), array(
  78. 'br' => array(),
  79. 'em' => array(),
  80. 'strong' => array(),
  81. 'small' => array(),
  82. 'span' => array(),
  83. 'ul' => array(),
  84. 'li' => array(),
  85. 'ol' => array(),
  86. 'p' => array(),
  87. ) ) );
  88. } else {
  89. $tip = esc_attr( $tip );
  90. }
  91. ob_start(); ?>
  92. <span class="um_tooltip dashicons dashicons-editor-help" title="<?php echo $tip ?>"></span>
  93. <?php if ( $echo ) {
  94. ob_get_flush();
  95. return '';
  96. } else {
  97. return ob_get_clean();
  98. }
  99. }
  100. /**
  101. * @return mixed|void
  102. */
  103. function excluded_taxonomies() {
  104. $taxes = array(
  105. 'nav_menu',
  106. 'link_category',
  107. 'post_format',
  108. );
  109. /**
  110. * UM hook
  111. *
  112. * @type filter
  113. * @title um_excluded_taxonomies
  114. * @description Exclude taxonomies for UM
  115. * @input_vars
  116. * [{"var":"$taxes","type":"array","desc":"Taxonomies keys"}]
  117. * @change_log
  118. * ["Since: 2.0"]
  119. * @usage
  120. * <?php add_filter( 'um_excluded_taxonomies', 'function_name', 10, 1 ); ?>
  121. * @example
  122. * <?php
  123. * add_filter( 'um_excluded_taxonomies', 'my_excluded_taxonomies', 10, 1 );
  124. * function my_excluded_taxonomies( $taxes ) {
  125. * // your code here
  126. * return $taxes;
  127. * }
  128. * ?>
  129. */
  130. return apply_filters( 'um_excluded_taxonomies', $taxes );
  131. }
  132. /**
  133. * Output templates
  134. *
  135. * @access public
  136. * @param string $template_name
  137. * @param string $basename (default: '')
  138. * @param array $t_args (default: array())
  139. * @param bool $echo
  140. *
  141. * @return string|void
  142. */
  143. function get_template( $template_name, $basename = '', $t_args = array(), $echo = false ) {
  144. if ( ! empty( $t_args ) && is_array( $t_args ) ) {
  145. extract( $t_args );
  146. }
  147. $path = '';
  148. if ( $basename ) {
  149. // use '/' instead of "DIRECTORY_SEPARATOR", because wp_normalize_path makes the correct replace
  150. $array = explode( '/', wp_normalize_path( trim( $basename ) ) );
  151. $path = $array[0];
  152. }
  153. $located = $this->locate_template( $template_name, $path );
  154. if ( ! file_exists( $located ) ) {
  155. _doing_it_wrong( __FUNCTION__, sprintf( '<code>%s</code> does not exist.', $located ), '2.1' );
  156. return;
  157. }
  158. /**
  159. * UM hook
  160. *
  161. * @type filter
  162. * @title um_get_template
  163. * @description Change template location
  164. * @input_vars
  165. * [{"var":"$located","type":"string","desc":"template Located"},
  166. * {"var":"$template_name","type":"string","desc":"Template Name"},
  167. * {"var":"$path","type":"string","desc":"Template Path at server"},
  168. * {"var":"$t_args","type":"array","desc":"Template Arguments"}]
  169. * @change_log
  170. * ["Since: 2.0"]
  171. * @usage add_filter( 'um_get_template', 'function_name', 10, 4 );
  172. * @example
  173. * <?php
  174. * add_filter( 'um_get_template', 'my_get_template', 10, 4 );
  175. * function my_get_template( $located, $template_name, $path, $t_args ) {
  176. * // your code here
  177. * return $located;
  178. * }
  179. * ?>
  180. */
  181. $located = apply_filters( 'um_get_template', $located, $template_name, $path, $t_args );
  182. ob_start();
  183. /**
  184. * UM hook
  185. *
  186. * @type action
  187. * @title um_before_template_part
  188. * @description Make some action before include template file
  189. * @input_vars
  190. * [{"var":"$template_name","type":"string","desc":"Template Name"},
  191. * {"var":"$path","type":"string","desc":"Template Path at server"},
  192. * {"var":"$located","type":"string","desc":"template Located"},
  193. * {"var":"$t_args","type":"array","desc":"Template Arguments"}]
  194. * @change_log
  195. * ["Since: 2.0"]
  196. * @usage add_action( 'um_before_template_part', 'function_name', 10, 4 );
  197. * @example
  198. * <?php
  199. * add_action( 'um_before_template_part', 'my_before_template_part', 10, 4 );
  200. * function my_before_template_part( $template_name, $path, $located, $t_args ) {
  201. * // your code here
  202. * }
  203. * ?>
  204. */
  205. do_action( 'um_before_template_part', $template_name, $path, $located, $t_args );
  206. include( $located );
  207. /**
  208. * UM hook
  209. *
  210. * @type action
  211. * @title um_after_template_part
  212. * @description Make some action after include template file
  213. * @input_vars
  214. * [{"var":"$template_name","type":"string","desc":"Template Name"},
  215. * {"var":"$path","type":"string","desc":"Template Path at server"},
  216. * {"var":"$located","type":"string","desc":"template Located"},
  217. * {"var":"$t_args","type":"array","desc":"Template Arguments"}]
  218. * @change_log
  219. * ["Since: 2.0"]
  220. * @usage add_action( 'um_after_template_part', 'function_name', 10, 4 );
  221. * @example
  222. * <?php
  223. * add_action( 'um_after_template_part', 'my_after_template_part', 10, 4 );
  224. * function my_after_template_part( $template_name, $path, $located, $t_args ) {
  225. * // your code here
  226. * }
  227. * ?>
  228. */
  229. do_action( 'um_after_template_part', $template_name, $path, $located, $t_args );
  230. $html = ob_get_clean();
  231. if ( ! $echo ) {
  232. return $html;
  233. } else {
  234. echo $html;
  235. return;
  236. }
  237. }
  238. /**
  239. * Locate a template and return the path for inclusion.
  240. *
  241. * @access public
  242. * @param string $template_name
  243. * @param string $path (default: '')
  244. * @return string
  245. */
  246. function locate_template( $template_name, $path = '' ) {
  247. // check if there is template at theme folder
  248. $template = locate_template( array(
  249. trailingslashit( 'ultimate-member' . DIRECTORY_SEPARATOR . $path ) . $template_name
  250. ) );
  251. if ( ! $template ) {
  252. if ( $path ) {
  253. $template = trailingslashit( trailingslashit( WP_PLUGIN_DIR ) . $path );
  254. } else {
  255. $template = trailingslashit( um_path );
  256. }
  257. $template .= 'templates' . DIRECTORY_SEPARATOR . $template_name;
  258. }
  259. /**
  260. * UM hook
  261. *
  262. * @type filter
  263. * @title um_locate_template
  264. * @description Change template locate
  265. * @input_vars
  266. * [{"var":"$template","type":"string","desc":"Template locate"},
  267. * {"var":"$template_name","type":"string","desc":"Template Name"},
  268. * {"var":"$path","type":"string","desc":"Template Path at server"}]
  269. * @change_log
  270. * ["Since: 2.0"]
  271. * @usage add_filter( 'um_locate_template', 'function_name', 10, 3 );
  272. * @example
  273. * <?php
  274. * add_filter( 'um_locate_template', 'my_locate_template', 10, 3 );
  275. * function my_locate_template( $template, $template_name, $path ) {
  276. * // your code here
  277. * return $template;
  278. * }
  279. * ?>
  280. */
  281. return apply_filters( 'um_locate_template', $template, $template_name, $path );
  282. }
  283. /**
  284. * @return mixed|void
  285. */
  286. function cpt_list() {
  287. /**
  288. * UM hook
  289. *
  290. * @type filter
  291. * @title um_cpt_list
  292. * @description Extend UM Custom Post Types
  293. * @input_vars
  294. * [{"var":"$list","type":"array","desc":"Custom Post Types list"}]
  295. * @change_log
  296. * ["Since: 2.0"]
  297. * @usage
  298. * <?php add_filter( 'um_cpt_list', 'function_name', 10, 1 ); ?>
  299. * @example
  300. * <?php
  301. * add_filter( 'um_cpt_list', 'my_cpt_list', 10, 1 );
  302. * function my_admin_pending_queue( $list ) {
  303. * // your code here
  304. * return $list;
  305. * }
  306. * ?>
  307. */
  308. $cpt = apply_filters( 'um_cpt_list', array( 'um_form', 'um_directory' ) );
  309. return $cpt;
  310. }
  311. /**
  312. * @param array $array
  313. * @param string $key
  314. * @param array $insert_array
  315. *
  316. * @return array
  317. */
  318. function array_insert_before( $array, $key, $insert_array ) {
  319. $index = array_search( $key, array_keys( $array ) );
  320. if ( $index === false ) {
  321. return $array;
  322. }
  323. $array = array_slice( $array, 0, $index, true ) +
  324. $insert_array +
  325. array_slice( $array, $index, count( $array ) - 1, true );
  326. return $array;
  327. }
  328. /**
  329. * @since 2.1.0
  330. *
  331. * @param $var
  332. * @return array|string
  333. */
  334. function clean_array( $var ) {
  335. if ( is_array( $var ) ) {
  336. return array_map( array( $this, 'clean_array' ), $var );
  337. } else {
  338. return is_scalar( $var ) ? sanitize_text_field( $var ) : $var;
  339. }
  340. }
  341. /**
  342. * Replace the first match in the string, alternative for the `str_replace()` function
  343. *
  344. * @param string $search
  345. * @param string $replace
  346. * @param string $subject
  347. *
  348. * @return string
  349. */
  350. function str_replace_first( $search, $replace, $subject ) {
  351. $search = '/' . preg_quote( $search, '/' ) . '/';
  352. return preg_replace( $search, $replace, $subject, 1 );
  353. }
  354. }
  355. }