PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/posts-to-posts/scb/Util.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 197 lines | 136 code | 43 blank | 18 comment | 14 complexity | fa97d4f1c3d6fc613efbb39f595a140a MD5 | raw file
  1. <?php
  2. // Various utilities
  3. class scbUtil {
  4. // Force script enqueue
  5. static function do_scripts( $handles ) {
  6. global $wp_scripts;
  7. if ( ! is_a( $wp_scripts, 'WP_Scripts' ) )
  8. $wp_scripts = new WP_Scripts();
  9. $wp_scripts->do_items( ( array ) $handles );
  10. }
  11. // Force style enqueue
  12. static function do_styles( $handles ) {
  13. self::do_scripts( 'jquery' );
  14. global $wp_styles;
  15. if ( ! is_a( $wp_styles, 'WP_Styles' ) )
  16. $wp_styles = new WP_Styles();
  17. ob_start();
  18. $wp_styles->do_items( ( array ) $handles );
  19. $content = str_replace( array( "'", "\n" ), array( '"', '' ), ob_get_clean() );
  20. echo "<script type='text/javascript'>\n";
  21. echo "jQuery(function ($) { $('head').prepend('$content'); });\n";
  22. echo "</script>";
  23. }
  24. // Enable delayed activation; to be used with scb_init()
  25. static function add_activation_hook( $plugin, $callback ) {
  26. if ( defined( 'SCB_LOAD_MU' ) )
  27. register_activation_hook( $plugin, $callback );
  28. else
  29. add_action( 'scb_activation_' . plugin_basename( $plugin ), $callback );
  30. }
  31. static function do_activation( $plugin ) {
  32. do_action( 'scb_activation_' . plugin_basename( $plugin ) );
  33. }
  34. // Allows more than one uninstall hooks.
  35. // Also prevents an UPDATE query on each page load.
  36. static function add_uninstall_hook( $plugin, $callback ) {
  37. register_uninstall_hook( $plugin, '__return_false' ); // dummy
  38. add_action( 'uninstall_' . plugin_basename( $plugin ), $callback );
  39. }
  40. static function do_uninstall( $plugin ) {
  41. do_action( 'uninstall_' . plugin_basename( $plugin ) );
  42. }
  43. // Get the current, full URL
  44. static function get_current_url() {
  45. return ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  46. }
  47. // Apply a function to each element of a ( nested ) array recursively
  48. static function array_map_recursive( $callback, $array ) {
  49. array_walk_recursive( $array, array( __CLASS__, 'array_map_recursive_helper' ), $callback );
  50. return $array;
  51. }
  52. static function array_map_recursive_helper( &$val, $key, $callback ) {
  53. $val = call_user_func( $callback, $val );
  54. }
  55. // Extract certain $keys from $array
  56. static function array_extract( $array, $keys ) {
  57. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'WP 3.1', 'wp_array_slice_assoc()' );
  58. return wp_array_slice_assoc( $array, $keys );
  59. }
  60. // Extract a certain value from a list of arrays
  61. static function array_pluck( $array, $key ) {
  62. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'WP 3.1', 'wp_list_pluck()' );
  63. return wp_list_pluck( $array, $key );
  64. }
  65. // Transform a list of objects into an associative array
  66. static function objects_to_assoc( $objects, $key, $value ) {
  67. _deprecated_function( __CLASS__ . '::' . __FUNCTION__, 'r41', 'scb_list_fold()' );
  68. return scb_list_fold( $objects, $key, $value );
  69. }
  70. // Prepare an array for an IN statement
  71. static function array_to_sql( $values ) {
  72. foreach ( $values as &$val )
  73. $val = "'" . esc_sql( trim( $val ) ) . "'";
  74. return implode( ',', $values );
  75. }
  76. // Example: split_at( '</', '<a></a>' ) => array( '<a>', '</a>' )
  77. static function split_at( $delim, $str ) {
  78. $i = strpos( $str, $delim );
  79. if ( false === $i )
  80. return false;
  81. $start = substr( $str, 0, $i );
  82. $finish = substr( $str, $i );
  83. return array( $start, $finish );
  84. }
  85. }
  86. // Return a standard admin notice
  87. function scb_admin_notice( $msg, $class = 'updated' ) {
  88. return "<div class='$class fade'><p>$msg</p></div>\n";
  89. }
  90. // Transform a list of objects into an associative array
  91. function scb_list_fold( $list, $key, $value ) {
  92. $r = array();
  93. if ( is_array( reset( $list ) ) ) {
  94. foreach ( $list as $item )
  95. $r[ $item[ $key ] ] = $item[ $value ];
  96. } else {
  97. foreach ( $list as $item )
  98. $r[ $item->$key ] = $item->$value;
  99. }
  100. return $r;
  101. }
  102. //_____Minimalist HTML framework_____
  103. /**
  104. * Generate an HTML tag. Atributes are escaped. Content is NOT escaped.
  105. */
  106. if ( ! function_exists( 'html' ) ):
  107. function html( $tag ) {
  108. $args = func_get_args();
  109. $tag = array_shift( $args );
  110. if ( is_array( $args[0] ) ) {
  111. $closing = $tag;
  112. $attributes = array_shift( $args );
  113. foreach ( $attributes as $key => $value ) {
  114. if ( false === $value )
  115. continue;
  116. if ( true === $value )
  117. $value = $key;
  118. $tag .= ' ' . $key . '="' . esc_attr( $value ) . '"';
  119. }
  120. } else {
  121. list( $closing ) = explode( ' ', $tag, 2 );
  122. }
  123. if ( in_array( $closing, array( 'area', 'base', 'basefont', 'br', 'hr', 'input', 'img', 'link', 'meta' ) ) ) {
  124. return "<{$tag} />";
  125. }
  126. $content = implode( '', $args );
  127. return "<{$tag}>{$content}</{$closing}>";
  128. }
  129. endif;
  130. // Generate an <a> tag
  131. if ( ! function_exists( 'html_link' ) ):
  132. function html_link( $url, $title = '' ) {
  133. if ( empty( $title ) )
  134. $title = $url;
  135. return html( 'a', array( 'href' => $url ), $title );
  136. }
  137. endif;
  138. //_____Compatibility layer_____
  139. // WP < ?
  140. if ( ! function_exists( 'set_post_field' ) ) :
  141. function set_post_field( $field, $value, $post_id ) {
  142. global $wpdb;
  143. $post_id = absint( $post_id );
  144. $value = sanitize_post_field( $field, $value, $post_id, 'db' );
  145. return $wpdb->update( $wpdb->posts, array( $field => $value ), array( 'ID' => $post_id ) );
  146. }
  147. endif;