PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-pagenavi/scb/Util.php

https://gitlab.com/hop23typhu/bryepoxy
PHP | 476 lines | 252 code | 63 blank | 161 comment | 28 complexity | 6af768004afcb1b1f68b727dbd343d12 MD5 | raw file
  1. <?php
  2. /**
  3. * Various utilities.
  4. */
  5. class scbUtil {
  6. /**
  7. * Force script enqueue.
  8. *
  9. * @param array $handles
  10. *
  11. * @return void
  12. */
  13. public static function do_scripts( $handles ) {
  14. global $wp_scripts;
  15. if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
  16. $wp_scripts = new WP_Scripts();
  17. }
  18. $wp_scripts->do_items( ( array ) $handles );
  19. }
  20. /**
  21. * Force style enqueue.
  22. *
  23. * @param array $handles
  24. *
  25. * @return void
  26. */
  27. public static function do_styles( $handles ) {
  28. self::do_scripts( 'jquery' );
  29. global $wp_styles;
  30. if ( ! is_a( $wp_styles, 'WP_Styles' ) ) {
  31. $wp_styles = new WP_Styles();
  32. }
  33. ob_start();
  34. $wp_styles->do_items( ( array ) $handles );
  35. $content = str_replace( array( "'", "\n" ), array( '"', '' ), ob_get_clean() );
  36. echo "<script type='text/javascript'>\n";
  37. echo "//<![CDATA[";
  38. echo "jQuery(function ($) { $('head').prepend('$content'); });\n";
  39. echo "//]]>";
  40. echo "</script>";
  41. }
  42. /**
  43. * Enable delayed plugin activation. To be used with scb_init()
  44. *
  45. * @param string $plugin
  46. * @param string|array $callback
  47. *
  48. * @return void
  49. */
  50. public static function add_activation_hook( $plugin, $callback ) {
  51. if ( defined( 'SCB_LOAD_MU' ) ) {
  52. register_activation_hook( $plugin, $callback );
  53. } else {
  54. add_action( 'scb_activation_' . plugin_basename( $plugin ), $callback );
  55. }
  56. }
  57. /**
  58. * Execute activation hook.
  59. * For debugging.
  60. *
  61. * @param string $plugin
  62. *
  63. * @return void
  64. */
  65. public static function do_activation( $plugin ) {
  66. do_action( 'scb_activation_' . plugin_basename( $plugin ) );
  67. }
  68. /**
  69. * Allows more than one uninstall hooks.
  70. * Also prevents an UPDATE query on each page load.
  71. *
  72. * @param string $plugin
  73. * @param string|array $callback
  74. *
  75. * @return void
  76. */
  77. public static function add_uninstall_hook( $plugin, $callback ) {
  78. if ( ! is_admin() ) {
  79. return;
  80. }
  81. register_uninstall_hook( $plugin, '__return_false' ); // dummy
  82. add_action( 'uninstall_' . plugin_basename( $plugin ), $callback );
  83. }
  84. /**
  85. * Execute uninstall hook.
  86. * For debugging.
  87. *
  88. * @param string $plugin
  89. *
  90. * @return void
  91. */
  92. public static function do_uninstall( $plugin ) {
  93. do_action( 'uninstall_' . plugin_basename( $plugin ) );
  94. }
  95. /**
  96. * Get the current, full URL.
  97. *
  98. * @return string
  99. */
  100. public static function get_current_url() {
  101. return ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  102. }
  103. /**
  104. * Apply a function to each element of a ( nested ) array recursively.
  105. *
  106. * @param string|array $callback
  107. * @param array $array
  108. *
  109. * @return array
  110. */
  111. public static function array_map_recursive( $callback, $array ) {
  112. array_walk_recursive( $array, array( __CLASS__, 'array_map_recursive_helper' ), $callback );
  113. return $array;
  114. }
  115. public static function array_map_recursive_helper( &$val, $key, $callback ) {
  116. $val = call_user_func( $callback, $val );
  117. }
  118. /**
  119. * Extract certain $keys from $array.
  120. *
  121. * @deprecated WP 3.1
  122. * @deprecated Use wp_array_slice_assoc()
  123. * @see wp_array_slice_assoc()
  124. *
  125. * @param array $array
  126. * @param array $keys
  127. *
  128. * @return array
  129. */
  130. public static function array_extract( $array, $keys ) {
  131. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'WP 3.1', 'wp_array_slice_assoc()' );
  132. return wp_array_slice_assoc( $array, $keys );
  133. }
  134. /**
  135. * Extract a certain value from a list of arrays.
  136. *
  137. * @deprecated WP 3.1
  138. * @deprecated Use wp_list_pluck()
  139. * @see wp_list_pluck()
  140. *
  141. * @param array $array
  142. * @param string $key
  143. *
  144. * @return array
  145. */
  146. public static function array_pluck( $array, $key ) {
  147. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'WP 3.1', 'wp_list_pluck()' );
  148. return wp_list_pluck( $array, $key );
  149. }
  150. /**
  151. * Transform a list of objects into an associative array.
  152. *
  153. * @deprecated r41
  154. * @deprecated Use scb_list_fold()
  155. * @see scb_list_fold()
  156. *
  157. * @param array $objects
  158. * @param string $key
  159. * @param string $value
  160. *
  161. * @return array
  162. */
  163. public static function objects_to_assoc( $objects, $key, $value ) {
  164. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'r41', 'scb_list_fold()' );
  165. return scb_list_fold( $objects, $key, $value );
  166. }
  167. /**
  168. * Prepare an array for an IN statement.
  169. *
  170. * @param array $values
  171. *
  172. * @return string
  173. */
  174. public static function array_to_sql( $values ) {
  175. foreach ( $values as &$val ) {
  176. $val = "'" . esc_sql( trim( $val ) ) . "'";
  177. }
  178. return implode( ',', $values );
  179. }
  180. /**
  181. * Example: split_at( '</', '<a></a>' ) => array( '<a>', '</a>' )
  182. *
  183. * @param string $delim
  184. * @param string $str
  185. *
  186. * @return array
  187. */
  188. public static function split_at( $delim, $str ) {
  189. $i = strpos( $str, $delim );
  190. if ( false === $i ) {
  191. return false;
  192. }
  193. $start = substr( $str, 0, $i );
  194. $finish = substr( $str, $i );
  195. return array( $start, $finish );
  196. }
  197. }
  198. /**
  199. * Return a standard admin notice.
  200. *
  201. * @param string $msg
  202. * @param string $class (optional)
  203. *
  204. * @return string
  205. */
  206. function scb_admin_notice( $msg, $class = 'updated' ) {
  207. return html( "div class='$class fade'", html( "p", $msg ) );
  208. }
  209. /**
  210. * Transform a list of objects into an associative array.
  211. *
  212. * @param array $objects
  213. * @param string $key
  214. * @param string $value
  215. *
  216. * @return array
  217. */
  218. function scb_list_fold( $list, $key, $value ) {
  219. $r = array();
  220. if ( is_array( reset( $list ) ) ) {
  221. foreach ( $list as $item ) {
  222. $r[ $item[ $key ] ] = $item[ $value ];
  223. }
  224. } else {
  225. foreach ( $list as $item ) {
  226. $r[ $item->$key ] = $item->$value;
  227. }
  228. }
  229. return $r;
  230. }
  231. /**
  232. * Splits a list into sets, grouped by the result of running each value through $fn.
  233. *
  234. * @param array $list List of items to be partitioned.
  235. * @param callback $fn Function that takes an element and returns a string key.
  236. *
  237. * @return array
  238. */
  239. function scb_list_group_by( $list, $fn ) {
  240. $groups = array();
  241. foreach ( $list as $item ) {
  242. $key = call_user_func( $fn, $item );
  243. if ( null === $key ) {
  244. continue;
  245. }
  246. $groups[ $key ][] = $item;
  247. }
  248. return $groups;
  249. }
  250. //_____Database Table Utilities_____
  251. /**
  252. * Register a table with $wpdb.
  253. *
  254. * @param string $key The key to be used on the $wpdb object.
  255. * @param string $name (optional) The actual name of the table, without $wpdb->prefix.
  256. *
  257. * @return void
  258. */
  259. function scb_register_table( $key, $name = false ) {
  260. global $wpdb;
  261. if ( ! $name ) {
  262. $name = $key;
  263. }
  264. $wpdb->tables[] = $name;
  265. $wpdb->$key = $wpdb->prefix . $name;
  266. }
  267. /**
  268. * Runs the SQL query for installing/upgrading a table.
  269. *
  270. * @param string $key The key used in scb_register_table().
  271. * @param string $columns The SQL columns for the CREATE TABLE statement.
  272. * @param array $opts (optional) Various other options.
  273. *
  274. * @return void
  275. */
  276. function scb_install_table( $key, $columns, $opts = array() ) {
  277. global $wpdb;
  278. $full_table_name = $wpdb->$key;
  279. if ( is_string( $opts ) ) {
  280. $opts = array( 'upgrade_method' => $opts );
  281. }
  282. $opts = wp_parse_args( $opts, array(
  283. 'upgrade_method' => 'dbDelta',
  284. 'table_options' => '',
  285. ) );
  286. $charset_collate = '';
  287. if ( $wpdb->has_cap( 'collation' ) ) {
  288. if ( ! empty( $wpdb->charset ) ) {
  289. $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
  290. }
  291. if ( ! empty( $wpdb->collate ) ) {
  292. $charset_collate .= " COLLATE $wpdb->collate";
  293. }
  294. }
  295. $table_options = $charset_collate . ' ' . $opts['table_options'];
  296. if ( 'dbDelta' == $opts['upgrade_method'] ) {
  297. require_once ABSPATH . 'wp-admin/includes/upgrade.php';
  298. dbDelta( "CREATE TABLE $full_table_name ( $columns ) $table_options" );
  299. return;
  300. }
  301. if ( 'delete_first' == $opts['upgrade_method'] ) {
  302. $wpdb->query( "DROP TABLE IF EXISTS $full_table_name;" );
  303. }
  304. $wpdb->query( "CREATE TABLE IF NOT EXISTS $full_table_name ( $columns ) $table_options;" );
  305. }
  306. /**
  307. * Runs the SQL query for uninstalling a table.
  308. *
  309. * @param string $key The key used in scb_register_table().
  310. *
  311. * @return void
  312. */
  313. function scb_uninstall_table( $key ) {
  314. global $wpdb;
  315. $wpdb->query( "DROP TABLE IF EXISTS " . $wpdb->$key );
  316. }
  317. //_____Minimalist HTML framework_____
  318. /**
  319. * Generate an HTML tag. Atributes are escaped. Content is NOT escaped.
  320. *
  321. * @param string $tag
  322. *
  323. * @return string
  324. */
  325. if ( ! function_exists( 'html' ) ):
  326. function html( $tag ) {
  327. static $SELF_CLOSING_TAGS = array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' );
  328. $args = func_get_args();
  329. $tag = array_shift( $args );
  330. if ( is_array( $args[0] ) ) {
  331. $closing = $tag;
  332. $attributes = array_shift( $args );
  333. foreach ( $attributes as $key => $value ) {
  334. if ( false === $value ) {
  335. continue;
  336. }
  337. if ( true === $value ) {
  338. $value = $key;
  339. }
  340. $tag .= ' ' . $key . '="' . esc_attr( $value ) . '"';
  341. }
  342. } else {
  343. list( $closing ) = explode( ' ', $tag, 2 );
  344. }
  345. if ( in_array( $closing, $SELF_CLOSING_TAGS ) ) {
  346. return "<{$tag} />";
  347. }
  348. $content = implode( '', $args );
  349. return "<{$tag}>{$content}</{$closing}>";
  350. }
  351. endif;
  352. /**
  353. * Generate an <a> tag.
  354. *
  355. * @param string $url
  356. * @param string $title (optional)
  357. *
  358. * @return string
  359. */
  360. if ( ! function_exists( 'html_link' ) ):
  361. function html_link( $url, $title = '' ) {
  362. if ( empty( $title ) ) {
  363. $title = $url;
  364. }
  365. return html( 'a', array( 'href' => esc_url( $url ) ), $title );
  366. }
  367. endif;
  368. /**
  369. * Returns an array of query flags.
  370. *
  371. * @param object $wp_query (optional)
  372. *
  373. * @return array
  374. */
  375. function scb_get_query_flags( $wp_query = null ) {
  376. if ( ! $wp_query ) {
  377. $wp_query = $GLOBALS['wp_query'];
  378. }
  379. $flags = array();
  380. foreach ( get_object_vars( $wp_query ) as $key => $val ) {
  381. if ( 'is_' == substr( $key, 0, 3 ) && $val ) {
  382. $flags[] = substr( $key, 3 );
  383. }
  384. }
  385. return $flags;
  386. }
  387. //_____Compatibility layer_____
  388. /**
  389. * Update data from a post field based on Post ID.
  390. * @see https://core.trac.wordpress.org/ticket/10946
  391. *
  392. * @param string $field Post field name.
  393. * @param string $value Post field value.
  394. * @param int $post_id Post ID.
  395. *
  396. * @return bool Result of UPDATE query.
  397. */
  398. if ( ! function_exists( 'set_post_field' ) ) :
  399. function set_post_field( $field, $value, $post_id ) {
  400. global $wpdb;
  401. $post_id = absint( $post_id );
  402. $value = sanitize_post_field( $field, $value, $post_id, 'db' );
  403. return $wpdb->update( $wpdb->posts, array( $field => $value ), array( 'ID' => $post_id ) );
  404. }
  405. endif;