CMB2_hookup

Handles hooking CMB2 forms/metaboxes into the post/attachement/user screens and handles hooking in and saving those fields.

Defined (1)

The class is defined in the following location(s).

/includes/CMB2_hookup.php  
  1. class CMB2_hookup extends CMB2_Hookup_Base { 
  2.  
  3. /** 
  4. * Only allow JS registration once 
  5. * @var bool 
  6. * @since 2.0.7 
  7. */ 
  8. protected static $js_registration_done = false; 
  9.  
  10. /** 
  11. * Only allow CSS registration once 
  12. * @var bool 
  13. * @since 2.0.7 
  14. */ 
  15. protected static $css_registration_done = false; 
  16.  
  17. /** 
  18. * CMB taxonomies array for term meta 
  19. * @var array 
  20. * @since 2.2.0 
  21. */ 
  22. protected $taxonomies = array(); 
  23.  
  24. /** 
  25. * Custom field columns. 
  26. * @var array 
  27. * @since 2.2.2 
  28. */ 
  29. protected $columns = array(); 
  30.  
  31. /** 
  32. * Constructor 
  33. * @since 2.0.0 
  34. * @param CMB2 $cmb The CMB2 object to hookup 
  35. */ 
  36. public function __construct( CMB2 $cmb ) { 
  37. $this->cmb = $cmb; 
  38. $this->object_type = $this->cmb->mb_object_type(); 
  39.  
  40. $this->universal_hooks(); 
  41.  
  42. if ( is_admin() ) { 
  43.  
  44. switch ( $this->object_type ) { 
  45. case 'post': 
  46. return $this->post_hooks(); 
  47. case 'comment': 
  48. return $this->comment_hooks(); 
  49. case 'user': 
  50. return $this->user_hooks(); 
  51. case 'term': 
  52. return $this->term_hooks(); 
  53.  
  54.  
  55. public function universal_hooks() { 
  56. foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { 
  57. add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); 
  58.  
  59. if ( is_admin() ) { 
  60. // register our scripts and styles for cmb 
  61. $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); 
  62. $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); 
  63.  
  64. $this->maybe_enqueue_column_display_styles(); 
  65.  
  66. public function post_hooks() { 
  67. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); 
  68. add_action( 'add_attachment', array( $this, 'save_post' ) ); 
  69. add_action( 'edit_attachment', array( $this, 'save_post' ) ); 
  70. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); 
  71.  
  72. if ( $this->cmb->has_columns ) { 
  73. foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { 
  74. add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); 
  75. add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); 
  76.  
  77. public function comment_hooks() { 
  78. add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); 
  79. add_action( 'edit_comment', array( $this, 'save_comment' ) ); 
  80.  
  81. if ( $this->cmb->has_columns ) { 
  82. add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); 
  83. add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); 
  84.  
  85. public function user_hooks() { 
  86. $priority = $this->get_priority(); 
  87.  
  88. add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); 
  89. add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); 
  90. add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); 
  91.  
  92. add_action( 'personal_options_update', array( $this, 'save_user' ) ); 
  93. add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); 
  94. add_action( 'user_register', array( $this, 'save_user' ) ); 
  95.  
  96. if ( $this->cmb->has_columns ) { 
  97. add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); 
  98. add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); 
  99.  
  100. public function term_hooks() { 
  101. if ( ! function_exists( 'get_term_meta' ) ) { 
  102. wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) ); 
  103.  
  104. if ( ! $this->cmb->prop( 'taxonomies' ) ) { 
  105. wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) ); 
  106.  
  107. $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); 
  108. $show_on_term_add = $this->cmb->prop( 'new_term_section' ); 
  109. $priority = $this->get_priority( 8 ); 
  110.  
  111. foreach ( $this->taxonomies as $taxonomy ) { 
  112. // Display our form data 
  113. add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); 
  114.  
  115. $show_on_add = is_array( $show_on_term_add ) 
  116. ? in_array( $taxonomy, $show_on_term_add ) 
  117. : (bool) $show_on_term_add; 
  118.  
  119. $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); 
  120.  
  121. // Display form in add-new section (unless specified not to) 
  122. if ( $show_on_add ) { 
  123. add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); 
  124.  
  125. if ( $this->cmb->has_columns ) { 
  126. add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); 
  127. add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); 
  128.  
  129. add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); 
  130. add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); 
  131. add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); 
  132.  
  133.  
  134. /** 
  135. * Registers styles for CMB2 
  136. * @since 2.0.7 
  137. */ 
  138. protected static function register_styles() { 
  139. if ( self::$css_registration_done ) { 
  140. return; 
  141.  
  142. // Only use minified files if SCRIPT_DEBUG is off 
  143. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; 
  144. $front = is_admin() ? '' : '-front'; 
  145. $rtl = is_rtl() ? '-rtl' : ''; 
  146.  
  147. // Filter required styles and register stylesheet 
  148. $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); 
  149. wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); 
  150. wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); 
  151.  
  152. self::$css_registration_done = true; 
  153.  
  154. /** 
  155. * Registers scripts for CMB2 
  156. * @since 2.0.7 
  157. */ 
  158. protected static function register_js() { 
  159. if ( self::$js_registration_done ) { 
  160. return; 
  161.  
  162. $hook = is_admin() ? 'admin_footer' : 'wp_footer'; 
  163. add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); 
  164.  
  165. self::$js_registration_done = true; 
  166.  
  167. /** 
  168. * Registers scripts and styles for CMB2 
  169. * @since 1.0.0 
  170. */ 
  171. public static function register_scripts() { 
  172. self::register_styles(); 
  173. self::register_js(); 
  174.  
  175. /** 
  176. * Enqueues scripts and styles for CMB2 in admin_head. 
  177. * @since 1.0.0 
  178. */ 
  179. public function do_scripts( $hook ) { 
  180. $hooks = array( 
  181. 'post.php',  
  182. 'post-new.php',  
  183. 'page-new.php',  
  184. 'page.php',  
  185. 'comment.php',  
  186. 'edit-tags.php',  
  187. 'term.php',  
  188. 'user-new.php',  
  189. 'profile.php',  
  190. 'user-edit.php',  
  191. ); 
  192. // only pre-enqueue our scripts/styles on the proper pages 
  193. // show_form_for_type will have us covered if we miss something here. 
  194. if ( in_array( $hook, $hooks, true ) ) { 
  195. if ( $this->cmb->prop( 'cmb_styles' ) ) { 
  196. self::enqueue_cmb_css(); 
  197. if ( $this->cmb->prop( 'enqueue_js' ) ) { 
  198. self::enqueue_cmb_js(); 
  199.  
  200. /** 
  201. * Register the CMB2 field column headers. 
  202. * @since 2.2.2 
  203. */ 
  204. public function register_column_headers( $columns ) { 
  205. $fields = $this->cmb->prop( 'fields' ); 
  206.  
  207. foreach ( $fields as $key => $field ) { 
  208. if ( ! isset( $field['column'] ) ) { 
  209. continue; 
  210.  
  211. $column = $field['column']; 
  212.  
  213. if ( false === $column['position'] ) { 
  214.  
  215. $columns[ $field['id'] ] = $column['name']; 
  216.  
  217. } else { 
  218.  
  219. $before = array_slice( $columns, 0, absint( $column['position'] ) ); 
  220. $before[ $field['id'] ] = $column['name']; 
  221. $columns = $before + $columns; 
  222.  
  223. $column['field'] = $field; 
  224. $this->columns[ $field['id'] ] = $column; 
  225.  
  226. return $columns; 
  227.  
  228. /** 
  229. * The CMB2 field column display output. 
  230. * @since 2.2.2 
  231. */ 
  232. public function column_display( $column_name, $object_id ) { 
  233. if ( isset( $this->columns[ $column_name ] ) ) { 
  234. $field = new CMB2_Field( array( 
  235. 'field_args' => $this->columns[ $column_name ]['field'],  
  236. 'object_type' => $this->object_type,  
  237. 'object_id' => $this->cmb->object_id( $object_id ),  
  238. 'cmb_id' => $this->cmb->cmb_id,  
  239. ) ); 
  240.  
  241. $this->cmb->get_field( $field )->render_column(); 
  242.  
  243. /** 
  244. * Returns the column display. 
  245. * @since 2.2.2 
  246. */ 
  247. public function return_column_display( $empty, $custom_column, $object_id ) { 
  248. ob_start(); 
  249. $this->column_display( $custom_column, $object_id ); 
  250. $column = ob_get_clean(); 
  251.  
  252. return $column ? $column : $empty; 
  253.  
  254. /** 
  255. * Add metaboxes (to 'post' or 'comment' object types) 
  256. * @since 1.0.0 
  257. */ 
  258. public function add_metaboxes() { 
  259.  
  260. if ( ! $this->show_on() ) { 
  261. return; 
  262.  
  263. /** 
  264. * To keep from registering an actual post-screen metabox,  
  265. * omit the 'title' attribute from the metabox registration array. 
  266. * (WordPress will not display metaboxes without titles anyway) 
  267. * This is a good solution if you want to output your metaboxes 
  268. * Somewhere else in the post-screen 
  269. */ 
  270. if ( ! $this->cmb->prop( 'title' ) ) { 
  271. return; 
  272.  
  273. foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { 
  274. if ( $this->cmb->prop( 'closed' ) ) { 
  275. add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); 
  276.  
  277. if ( count( $this->cmb->tax_metaboxes_to_remove ) ) { 
  278. $this->remove_default_tax_metaboxes( $post_type ); 
  279.  
  280. add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) ); 
  281.  
  282. /** 
  283. * Remove the specified default taxonomy metaboxes for a post-type. 
  284. * @since 2.2.3 
  285. * @param string $post_type Post type to remove the metabox for. 
  286. */ 
  287. protected function remove_default_tax_metaboxes( $post_type ) { 
  288. foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) { 
  289. if ( ! taxonomy_exists( $taxonomy ) ) { 
  290. continue; 
  291.  
  292. $mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}"; 
  293. remove_meta_box( $mb_id, $post_type, 'side' ); 
  294.  
  295. /** 
  296. * Add 'closed' class to metabox 
  297. * @since 2.0.0 
  298. * @param array $classes Array of classes 
  299. * @return array Modified array of classes 
  300. */ 
  301. public function close_metabox_class( $classes ) { 
  302. $classes[] = 'closed'; 
  303. return $classes; 
  304.  
  305. /** 
  306. * Display metaboxes for a post or comment object 
  307. * @since 1.0.0 
  308. */ 
  309. public function metabox_callback() { 
  310. $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID(); 
  311. $this->cmb->show_form( $object_id, $this->object_type ); 
  312.  
  313. /** 
  314. * Display metaboxes for new user page 
  315. * @since 1.0.0 
  316. */ 
  317. public function user_new_metabox( $section ) { 
  318. if ( $section == $this->cmb->prop( 'new_user_section' ) ) { 
  319. $object_id = $this->cmb->object_id(); 
  320. $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); 
  321. $this->user_metabox(); 
  322.  
  323. /** 
  324. * Display metaboxes for a user object 
  325. * @since 1.0.0 
  326. */ 
  327. public function user_metabox() { 
  328. $this->show_form_for_type( 'user' ); 
  329.  
  330. /** 
  331. * Display metaboxes for a taxonomy term object 
  332. * @since 2.2.0 
  333. */ 
  334. public function term_metabox() { 
  335. $this->show_form_for_type( 'term' ); 
  336.  
  337. /** 
  338. * Display metaboxes for an object type 
  339. * @since 2.2.0 
  340. * @param string $type Object type 
  341. * @return void 
  342. */ 
  343. public function show_form_for_type( $type ) { 
  344. if ( $type != $this->cmb->mb_object_type() ) { 
  345. return; 
  346.  
  347. if ( ! $this->show_on() ) { 
  348. return; 
  349.  
  350. if ( $this->cmb->prop( 'cmb_styles' ) ) { 
  351. self::enqueue_cmb_css(); 
  352. if ( $this->cmb->prop( 'enqueue_js' ) ) { 
  353. self::enqueue_cmb_js(); 
  354.  
  355. $this->cmb->show_form( 0, $type ); 
  356.  
  357. /** 
  358. * Determines if metabox should be shown in current context 
  359. * @since 2.0.0 
  360. * @return bool Whether metabox should be added/shown 
  361. */ 
  362. public function show_on() { 
  363. // If metabox is requesting to be conditionally shown 
  364. $show = $this->cmb->should_show(); 
  365.  
  366. /** 
  367. * Filter to determine if metabox should show. Default is true 
  368. * @param array $show Default is true, show the metabox 
  369. * @param mixed $meta_box_args Array of the metabox arguments 
  370. * @param mixed $cmb The CMB2 instance 
  371. */ 
  372. $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); 
  373.  
  374. return $show; 
  375.  
  376. /** 
  377. * Get the CMB priority property set to numeric hook priority. 
  378. * @since 2.2.0 
  379. * @param integer $default Default display hook priority. 
  380. * @return integer Hook priority. 
  381. */ 
  382. public function get_priority( $default = 10 ) { 
  383. $priority = $this->cmb->prop( 'priority' ); 
  384.  
  385. if ( ! is_numeric( $priority ) ) { 
  386. switch ( $priority ) { 
  387.  
  388. case 'high': 
  389. $priority = 5; 
  390. break; 
  391.  
  392. case 'low': 
  393. $priority = 20; 
  394. break; 
  395.  
  396. default: 
  397. $priority = $default; 
  398. break; 
  399.  
  400. return $priority; 
  401.  
  402. /** 
  403. * Save data from post metabox 
  404. * @since 1.0.0 
  405. * @param int $post_id Post ID 
  406. * @param mixed $post Post object 
  407. * @return null 
  408. */ 
  409. public function save_post( $post_id, $post = false ) { 
  410.  
  411. $post_type = $post ? $post->post_type : get_post_type( $post_id ); 
  412.  
  413. $do_not_pass_go = ( 
  414. ! $this->can_save( $post_type ) 
  415. // check user editing permissions 
  416. || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) ) 
  417. || ! current_user_can( 'edit_post', $post_id ) 
  418. ); 
  419.  
  420. if ( $do_not_pass_go ) { 
  421. // do not collect $200 
  422. return; 
  423.  
  424. // take a trip to reading railroad * if you pass go collect $200 
  425. $this->cmb->save_fields( $post_id, 'post', $_POST ); 
  426.  
  427. /** 
  428. * Save data from comment metabox 
  429. * @since 2.0.9 
  430. * @param int $comment_id Comment ID 
  431. * @return null 
  432. */ 
  433. public function save_comment( $comment_id ) { 
  434.  
  435. $can_edit = current_user_can( 'moderate_comments', $comment_id ); 
  436.  
  437. if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { 
  438. $this->cmb->save_fields( $comment_id, 'comment', $_POST ); 
  439.  
  440. /** 
  441. * Save data from user fields 
  442. * @since 1.0.x 
  443. * @param int $user_id User ID 
  444. * @return null 
  445. */ 
  446. public function save_user( $user_id ) { 
  447. // check permissions 
  448. if ( $this->can_save( 'user' ) ) { 
  449. $this->cmb->save_fields( $user_id, 'user', $_POST ); 
  450.  
  451. /** 
  452. * Save data from term fields 
  453. * @since 2.2.0 
  454. * @param int $term_id Term ID 
  455. * @param int $tt_id Term Taxonomy ID 
  456. * @param string $taxonomy Taxonomy 
  457. * @return null 
  458. */ 
  459. public function save_term( $term_id, $tt_id, $taxonomy = '' ) { 
  460. $taxonomy = $taxonomy ? $taxonomy : $tt_id; 
  461.  
  462. // check permissions 
  463. if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { 
  464. $this->cmb->save_fields( $term_id, 'term', $_POST ); 
  465.  
  466. /** 
  467. * Delete term meta when a term is deleted. 
  468. * @since 2.2.0 
  469. * @param int $term_id Term ID 
  470. * @param int $tt_id Term Taxonomy ID 
  471. * @param string $taxonomy Taxonomy 
  472. * @return null 
  473. */ 
  474. public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { 
  475. if ( $this->taxonomy_can_save( $taxonomy ) ) { 
  476.  
  477. foreach ( $this->cmb->prop( 'fields' ) as $field ) { 
  478. $data_to_delete[ $field['id'] ] = ''; 
  479.  
  480. $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); 
  481.  
  482. /** 
  483. * Determines if the current object is able to be saved 
  484. * @since 2.0.9 
  485. * @param string $type Current post_type or comment_type 
  486. * @return bool Whether object can be saved 
  487. */ 
  488. public function can_save( $type = '' ) { 
  489. return apply_filters( 'cmb2_can_save', ( 
  490. $this->cmb->prop( 'save_fields' ) 
  491. // check nonce 
  492. && isset( $_POST[ $this->cmb->nonce() ] ) 
  493. && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() ) 
  494. // check if autosave 
  495. && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  496. // get the metabox types & compare it to this type 
  497. && ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) ) 
  498. // Don't do updates during a switch-to-blog instance. 
  499. && ! ( is_multisite() && ms_is_switched() ) 
  500. ) ); 
  501.  
  502. /** 
  503. * Determine if taxonomy of term being modified is cmb2-editable. 
  504. * @since 2.2.0 
  505. * @param string $taxonomy Taxonomy of term being modified. 
  506. * @return bool Whether taxonomy is editable. 
  507. */ 
  508. public function taxonomy_can_save( $taxonomy ) { 
  509. if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) { 
  510. return false; 
  511.  
  512. $taxonomy_object = get_taxonomy( $taxonomy ); 
  513. // Can the user edit this term? 
  514. if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) { 
  515. return false; 
  516.  
  517. return true; 
  518.  
  519. /** 
  520. * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). 
  521. * @since 2.2.2.1 
  522. */ 
  523. protected function maybe_enqueue_column_display_styles() { 
  524. global $pagenow; 
  525. if ( 
  526. $pagenow 
  527. && $this->cmb->has_columns 
  528. && $this->cmb->prop( 'cmb_styles' ) 
  529. && in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 ) 
  530. ) { 
  531. self::enqueue_cmb_css( 'cmb2-display-styles' ); 
  532.  
  533. /** 
  534. * Includes CMB2 styles 
  535. * @since 2.0.0 
  536. */ 
  537. public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { 
  538. if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) { 
  539. return false; 
  540.  
  541. self::register_styles(); 
  542.  
  543. /** 
  544. * White list the options as this method can be used as a hook callback 
  545. * and have a different argument passed. 
  546. */ 
  547. return wp_enqueue_style( 'cmb2-display-styles' === $handle ? $handle : 'cmb2-styles' ); 
  548.  
  549. /** 
  550. * Includes CMB2 JS 
  551. * @since 2.0.0 
  552. */ 
  553. public static function enqueue_cmb_js() { 
  554. if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) { 
  555. return false; 
  556.  
  557. self::register_js(); 
  558. return true; 
  559.