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

/wp-content/themes/faci-qsoft/includes/meta-box/inc/meta-box.php

https://gitlab.com/hop23typhu/bryepoxy
PHP | 479 lines | 246 code | 71 blank | 162 comment | 32 complexity | cddc6ff9b87447f13ddaaeaa5aa462f6 MD5 | raw file
  1. <?php
  2. // Prevent loading this file directly
  3. defined( 'ABSPATH' ) || exit;
  4. // Meta Box Class
  5. if ( ! class_exists( 'RW_Meta_Box' ) )
  6. {
  7. /**
  8. * A class to rapid develop meta boxes for custom & built in content types
  9. * Piggybacks on WordPress
  10. *
  11. * @author Rilwis
  12. * @author Co-Authors @see https://github.com/rilwis/meta-box
  13. * @license GNU GPL2+
  14. * @package RW Meta Box
  15. */
  16. class RW_Meta_Box
  17. {
  18. /**
  19. * @var array Meta box information
  20. */
  21. public $meta_box;
  22. /**
  23. * @var array Fields information
  24. */
  25. public $fields;
  26. /**
  27. * @var array Contains all field types of current meta box
  28. */
  29. public $types;
  30. /**
  31. * @var array Validation information
  32. */
  33. public $validation;
  34. /**
  35. * @var bool Used to prevent duplicated calls like revisions, manual hook to wp_insert_post, etc.
  36. */
  37. public $saved = false;
  38. /**
  39. * Create meta box based on given data
  40. *
  41. * @see demo/demo.php file for details
  42. *
  43. * @param array $meta_box Meta box definition
  44. *
  45. * @return RW_Meta_Box
  46. */
  47. function __construct( $meta_box )
  48. {
  49. // Run script only in admin area
  50. if ( ! is_admin() )
  51. return;
  52. // Assign meta box values to local variables and add it's missed values
  53. $this->meta_box = self::normalize( $meta_box );
  54. $this->fields = &$this->meta_box['fields'];
  55. $this->validation = &$this->meta_box['validation'];
  56. // Allow users to show/hide meta box
  57. // 1st action applies to all meta boxes
  58. // 2nd action applies to only current meta box
  59. $show = true;
  60. $show = apply_filters( 'rwmb_show', $show, $this->meta_box );
  61. $show = apply_filters( "rwmb_show_{$this->meta_box['id']}", $show, $this->meta_box );
  62. if ( ! $show )
  63. return;
  64. // Enqueue common styles and scripts
  65. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  66. // Add additional actions for fields
  67. $fields = self::get_fields( $this->fields );
  68. foreach ( $fields as $field )
  69. {
  70. call_user_func( array( self::get_class_name( $field ), 'add_actions' ) );
  71. }
  72. // Add meta box
  73. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
  74. // Hide meta box if it's set 'default_hidden'
  75. add_filter( 'default_hidden_meta_boxes', array( $this, 'hide' ), 10, 2 );
  76. // Save post meta
  77. add_action( 'save_post', array( $this, 'save_post' ) );
  78. // Attachment uses other hooks
  79. // @see wp_update_post(), wp_insert_attachment()
  80. add_action( 'edit_attachment', array( $this, 'save_post' ) );
  81. add_action( 'add_attachment', array( $this, 'save_post' ) );
  82. }
  83. /**
  84. * Enqueue common styles
  85. *
  86. * @return void
  87. */
  88. function admin_enqueue_scripts()
  89. {
  90. $screen = get_current_screen();
  91. // Enqueue scripts and styles for registered pages (post types) only
  92. if ( 'post' != $screen->base || ! in_array( $screen->post_type, $this->meta_box['post_types'] ) )
  93. return;
  94. wp_enqueue_style( 'rwmb', RWMB_CSS_URL . 'style.css', array(), RWMB_VER );
  95. // Load clone script conditionally
  96. $has_clone = false;
  97. $fields = self::get_fields( $this->fields );
  98. foreach ( $fields as $field )
  99. {
  100. if ( $field['clone'] )
  101. $has_clone = true;
  102. // Enqueue scripts and styles for fields
  103. call_user_func( array( self::get_class_name( $field ), 'admin_enqueue_scripts' ) );
  104. }
  105. if ( $has_clone )
  106. wp_enqueue_script( 'rwmb-clone', RWMB_JS_URL . 'clone.js', array( 'jquery' ), RWMB_VER, true );
  107. if ( $this->validation )
  108. {
  109. wp_enqueue_script( 'jquery-validate', RWMB_JS_URL . 'jquery.validate.min.js', array( 'jquery' ), RWMB_VER, true );
  110. wp_enqueue_script( 'rwmb-validate', RWMB_JS_URL . 'validate.js', array( 'jquery-validate' ), RWMB_VER, true );
  111. }
  112. // Auto save
  113. if ( $this->meta_box['autosave'] )
  114. wp_enqueue_script( 'rwmb-autosave', RWMB_JS_URL . 'autosave.js', array( 'jquery' ), RWMB_VER, true );
  115. }
  116. /**
  117. * Get all fields of a meta box, recursively
  118. *
  119. * @param array $fields
  120. *
  121. * @return array
  122. */
  123. static function get_fields( $fields )
  124. {
  125. $all_fields = array();
  126. foreach ( $fields as $field )
  127. {
  128. $all_fields[] = $field;
  129. if ( isset( $field['fields'] ) )
  130. $all_fields = array_merge( $all_fields, self::get_fields( $field['fields'] ) );
  131. }
  132. return $all_fields;
  133. }
  134. /**************************************************
  135. * SHOW META BOX
  136. **************************************************/
  137. /**
  138. * Add meta box for multiple post types
  139. *
  140. * @return void
  141. */
  142. function add_meta_boxes()
  143. {
  144. foreach ( $this->meta_box['post_types'] as $post_type )
  145. {
  146. add_meta_box(
  147. $this->meta_box['id'],
  148. $this->meta_box['title'],
  149. array( $this, 'show' ),
  150. $post_type,
  151. $this->meta_box['context'],
  152. $this->meta_box['priority']
  153. );
  154. }
  155. }
  156. /**
  157. * Hide meta box if it's set 'default_hidden'
  158. *
  159. * @param array $hidden Array of default hidden meta boxes
  160. * @param object $screen Current screen information
  161. *
  162. * @return array
  163. */
  164. function hide( $hidden, $screen )
  165. {
  166. if (
  167. 'post' === $screen->base
  168. && in_array( $screen->post_type, $this->meta_box['post_types'] )
  169. && $this->meta_box['default_hidden']
  170. )
  171. {
  172. $hidden[] = $this->meta_box['id'];
  173. }
  174. return $hidden;
  175. }
  176. /**
  177. * Callback function to show fields in meta box
  178. *
  179. * @return void
  180. */
  181. function show()
  182. {
  183. global $post;
  184. $saved = self::has_been_saved( $post->ID, $this->fields );
  185. // Container
  186. printf(
  187. '<div class="rwmb-meta-box" data-autosave="%s">',
  188. $this->meta_box['autosave'] ? 'true' : 'false'
  189. );
  190. wp_nonce_field( "rwmb-save-{$this->meta_box['id']}", "nonce_{$this->meta_box['id']}" );
  191. // Allow users to add custom code before meta box content
  192. // 1st action applies to all meta boxes
  193. // 2nd action applies to only current meta box
  194. do_action( 'rwmb_before', $this );
  195. do_action( "rwmb_before_{$this->meta_box['id']}", $this );
  196. foreach ( $this->fields as $field )
  197. {
  198. call_user_func( array( self::get_class_name( $field ), 'show' ), $field, $saved );
  199. }
  200. // Include validation settings for this meta-box
  201. if ( isset( $this->validation ) && $this->validation )
  202. {
  203. echo '
  204. <script>
  205. if ( typeof rwmb == "undefined" )
  206. {
  207. var rwmb = {
  208. validationOptions : jQuery.parseJSON( \'' , json_encode( $this->validation ) , '\' ),
  209. summaryMessage : "' , esc_js( __( 'Please correct the errors highlighted below and try again.', 'meta-box' ) ) , '"
  210. };
  211. }
  212. else
  213. {
  214. var tempOptions = jQuery.parseJSON( \'' , json_encode( $this->validation ) . '\' );
  215. jQuery.extend( true, rwmb.validationOptions, tempOptions );
  216. }
  217. </script>
  218. ';
  219. }
  220. // Allow users to add custom code after meta box content
  221. // 1st action applies to all meta boxes
  222. // 2nd action applies to only current meta box
  223. do_action( 'rwmb_after', $this );
  224. do_action( "rwmb_after_{$this->meta_box['id']}", $this );
  225. // End container
  226. echo '</div>';
  227. }
  228. /**************************************************
  229. * SAVE META BOX
  230. **************************************************/
  231. /**
  232. * Save data from meta box
  233. *
  234. * @param int $post_id Post ID
  235. *
  236. * @return void
  237. */
  238. function save_post( $post_id )
  239. {
  240. // Check if this function is called to prevent duplicated calls like revisions, manual hook to wp_insert_post, etc.
  241. if ( true === $this->saved )
  242. return;
  243. $this->saved = true;
  244. // Check whether form is submitted properly
  245. $id = $this->meta_box['id'];
  246. $nonce = isset( $_POST["nonce_{$id}"] ) ? sanitize_key( $_POST["nonce_{$id}"] ) : '';
  247. if ( empty( $_POST["nonce_{$id}"] ) || ! wp_verify_nonce( $nonce, "rwmb-save-{$id}" ) )
  248. return;
  249. // Autosave
  250. if ( defined( 'DOING_AUTOSAVE' ) && ! $this->meta_box['autosave'] )
  251. return;
  252. // Make sure meta is added to the post, not a revision
  253. if ( $the_post = wp_is_post_revision( $post_id ) )
  254. $post_id = $the_post;
  255. // Before save action
  256. do_action( 'rwmb_before_save_post', $post_id );
  257. do_action( "rwmb_{$this->meta_box['id']}_before_save_post", $post_id );
  258. foreach ( $this->fields as $field )
  259. {
  260. $name = $field['id'];
  261. $single = $field['clone'] || ! $field['multiple'];
  262. $old = get_post_meta( $post_id, $name, $single );
  263. $new = isset( $_POST[$name] ) ? $_POST[$name] : ( $single ? '' : array() );
  264. // Allow field class change the value
  265. $new = call_user_func( array( self::get_class_name( $field ), 'value' ), $new, $old, $post_id, $field );
  266. // Use filter to change field value
  267. // 1st filter applies to all fields with the same type
  268. // 2nd filter applies to current field only
  269. $new = apply_filters( "rwmb_{$field['type']}_value", $new, $field, $old );
  270. $new = apply_filters( "rwmb_{$name}_value", $new, $field, $old );
  271. // Call defined method to save meta value, if there's no methods, call common one
  272. call_user_func( array( self::get_class_name( $field ), 'save' ), $new, $old, $post_id, $field );
  273. }
  274. // After save action
  275. do_action( 'rwmb_after_save_post', $post_id );
  276. do_action( "rwmb_{$this->meta_box['id']}_after_save_post", $post_id );
  277. }
  278. /**************************************************
  279. * HELPER FUNCTIONS
  280. **************************************************/
  281. /**
  282. * Normalize parameters for meta box
  283. *
  284. * @param array $meta_box Meta box definition
  285. *
  286. * @return array $meta_box Normalized meta box
  287. */
  288. static function normalize( $meta_box )
  289. {
  290. // Set default values for meta box
  291. $meta_box = wp_parse_args( $meta_box, array(
  292. 'id' => sanitize_title( $meta_box['title'] ),
  293. 'context' => 'normal',
  294. 'priority' => 'high',
  295. 'post_types' => 'post',
  296. 'autosave' => false,
  297. 'default_hidden' => false,
  298. ) );
  299. /**
  300. * Use 'post_types' for better understanding and fallback to 'pages' for previous versions
  301. *
  302. * @since 4.4.1
  303. */
  304. if ( ! empty( $meta_box['pages'] ) )
  305. {
  306. $meta_box['post_types'] = $meta_box['pages'];
  307. }
  308. // Allow to set 'post_types' param by string
  309. if ( is_string( $meta_box['post_types'] ) )
  310. {
  311. $meta_box['post_types'] = array( $meta_box['post_types'] );
  312. }
  313. // Set default values for fields
  314. $meta_box['fields'] = self::normalize_fields( $meta_box['fields'] );
  315. // Allow to add default values for meta box
  316. $meta_box = apply_filters( 'rwmb_normalize_meta_box', $meta_box );
  317. $meta_box = apply_filters( "rwmb_normalize_{$meta_box['id']}_meta_box", $meta_box );
  318. return $meta_box;
  319. }
  320. /**
  321. * Normalize an array of fields
  322. *
  323. * @param array $fields Array of fields
  324. *
  325. * @return array $fields Normalized fields
  326. */
  327. static function normalize_fields( $fields )
  328. {
  329. foreach ( $fields as $k => $field )
  330. {
  331. $field = wp_parse_args( $field, array(
  332. 'id' => '',
  333. 'name' => '',
  334. 'multiple' => false,
  335. 'std' => '',
  336. 'desc' => '',
  337. 'format' => '',
  338. 'before' => '',
  339. 'after' => '',
  340. 'field_name' => isset( $field['id'] ) ? $field['id'] : '',
  341. 'required' => false,
  342. 'placeholder' => '',
  343. 'clone' => false,
  344. 'max_clone' => 0,
  345. 'sort_clone' => false,
  346. ) );
  347. $class = self::get_class_name( $field );
  348. // Make sure field has correct 'type', ignore warning error when users forget to set field type or set incorrect one
  349. if ( false === $class )
  350. {
  351. unset( $fields[$k] );
  352. continue;
  353. }
  354. // Allow field class add/change default field values
  355. $field = call_user_func( array( $class, 'normalize_field' ), $field );
  356. if ( isset( $field['fields'] ) )
  357. $field['fields'] = self::normalize_fields( $field['fields'] );
  358. // Allow to add default values for fields
  359. $field = apply_filters( 'rwmb_normalize_field', $field );
  360. $field = apply_filters( "rwmb_normalize_{$field['type']}_field", $field );
  361. $field = apply_filters( "rwmb_normalize_{$field['id']}_field", $field );
  362. $fields[$k] = $field;
  363. }
  364. return $fields;
  365. }
  366. /**
  367. * Get field class name
  368. *
  369. * @param array $field Field array
  370. *
  371. * @return bool|string Field class name OR false on failure
  372. */
  373. static function get_class_name( $field )
  374. {
  375. // Convert underscores to whitespace so ucwords works as expected. Otherwise: plupload_image -> Plupload_image instead of Plupload_Image
  376. $type = str_replace( '_', ' ', $field['type'] );
  377. // Uppercase first words
  378. $class = 'RWMB_' . ucwords( $type ) . '_Field';
  379. // Relace whitespace with underscores
  380. $class = str_replace( ' ', '_', $class );
  381. return class_exists( $class ) ? $class : false;
  382. }
  383. /**
  384. * Check if meta box has been saved
  385. * This helps saving empty value in meta fields (for text box, check box, etc.)
  386. *
  387. * @param int $post_id
  388. * @param array $fields
  389. *
  390. * @return bool
  391. */
  392. static function has_been_saved( $post_id, $fields )
  393. {
  394. foreach ( $fields as $field )
  395. {
  396. $value = get_post_meta( $post_id, $field['id'], ! $field['multiple'] );
  397. if (
  398. ( ! $field['multiple'] && '' !== $value )
  399. || ( $field['multiple'] && array() !== $value )
  400. )
  401. {
  402. return true;
  403. }
  404. }
  405. return false;
  406. }
  407. }
  408. }