CMB2_hookup

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

Defined (2)

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

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