prop( 'hookup' ) ) { $hookup = new self( $cmb ); // Hook in the hookup... how meta. return $hookup->universal_hooks(); } return false; } public function universal_hooks() { foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); } if ( is_admin() ) { // Register our scripts and styles for cmb. $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); $this->maybe_enqueue_column_display_styles(); switch ( $this->object_type ) { case 'post': return $this->post_hooks(); case 'comment': return $this->comment_hooks(); case 'user': return $this->user_hooks(); case 'term': return $this->term_hooks(); case 'options-page': return $this->options_page_hooks(); } } return $this; } public function post_hooks() { // Fetch the context we set in our call. $context = $this->cmb->prop( 'context' ) ? $this->cmb->prop( 'context' ) : 'normal'; // Call the proper hook based on the context provided. switch ( $context ) { case 'form_top': add_action( 'edit_form_top', array( $this, 'add_context_metaboxes' ) ); break; case 'before_permalink': add_action( 'edit_form_before_permalink', array( $this, 'add_context_metaboxes' ) ); break; case 'after_title': add_action( 'edit_form_after_title', array( $this, 'add_context_metaboxes' ) ); break; case 'after_editor': add_action( 'edit_form_after_editor', array( $this, 'add_context_metaboxes' ) ); break; default: add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); } add_action( 'add_meta_boxes', array( $this, 'remove_default_tax_metaboxes' ) ); add_action( 'add_attachment', array( $this, 'save_post' ) ); add_action( 'edit_attachment', array( $this, 'save_post' ) ); add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); if ( $this->cmb->has_columns ) { foreach ( $this->cmb->box_types() as $post_type ) { add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); } } return $this; } public function comment_hooks() { add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); add_action( 'edit_comment', array( $this, 'save_comment' ) ); if ( $this->cmb->has_columns ) { add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); } return $this; } public function user_hooks() { $priority = $this->get_priority(); add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); add_action( 'personal_options_update', array( $this, 'save_user' ) ); add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); add_action( 'user_register', array( $this, 'save_user' ) ); if ( $this->cmb->has_columns ) { add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); } return $this; } public function term_hooks() { if ( ! function_exists( 'get_term_meta' ) ) { wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) ); } if ( ! $this->cmb->prop( 'taxonomies' ) ) { wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) ); } $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); $show_on_term_add = $this->cmb->prop( 'new_term_section' ); $priority = $this->get_priority( 8 ); foreach ( $this->taxonomies as $taxonomy ) { // Display our form data. add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); $show_on_add = is_array( $show_on_term_add ) ? in_array( $taxonomy, $show_on_term_add ) : (bool) $show_on_term_add; /** * Filter to determine if the term's fields should show in the "Add term" section. * * The dynamic portion of the hook name, $cmb_id, is the metabox id. * * @param bool $show_on_add Default is the value of the new_term_section cmb parameter. * @param object $cmb The CMB2 instance */ $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); // Display form in add-new section (unless specified not to). if ( $show_on_add ) { add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); } if ( $this->cmb->has_columns ) { add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); } } add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); return $this; } public function options_page_hooks() { $option_keys = $this->cmb->options_page_keys(); if ( ! empty( $option_keys ) ) { foreach ( $option_keys as $option_key ) { $this->options_hookup[ $option_key ] = new CMB2_Options_Hookup( $this->cmb, $option_key ); $this->options_hookup[ $option_key ]->hooks(); } } return $this; } /** * Registers styles for CMB2 * * @since 2.0.7 */ protected static function register_styles() { if ( self::$css_registration_done ) { return; } // Only use minified files if SCRIPT_DEBUG is off. $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; $front = is_admin() ? '' : '-front'; $rtl = is_rtl() ? '-rtl' : ''; /** * Filters the registered style dependencies for the cmb2 stylesheet. * * @param array $dependencies The registered style dependencies for the cmb2 stylesheet. */ $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); self::$css_registration_done = true; } /** * Registers scripts for CMB2 * * @since 2.0.7 */ protected static function register_js() { if ( self::$js_registration_done ) { return; } $hook = is_admin() ? 'admin_footer' : 'wp_footer'; add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); self::$js_registration_done = true; } /** * Registers scripts and styles for CMB2 * * @since 1.0.0 */ public static function register_scripts() { self::register_styles(); self::register_js(); } /** * Enqueues scripts and styles for CMB2 in admin_head. * * @since 1.0.0 * * @param string $hook Current hook for the admin page. */ public function do_scripts( $hook ) { $hooks = array( 'post.php', 'post-new.php', 'page-new.php', 'page.php', 'comment.php', 'edit-tags.php', 'term.php', 'user-new.php', 'profile.php', 'user-edit.php', ); // only pre-enqueue our scripts/styles on the proper pages // show_form_for_type will have us covered if we miss something here. if ( in_array( $hook, $hooks, true ) ) { if ( $this->cmb->prop( 'cmb_styles' ) ) { self::enqueue_cmb_css(); } if ( $this->cmb->prop( 'enqueue_js' ) ) { self::enqueue_cmb_js(); } } } /** * Register the CMB2 field column headers. * * @since 2.2.2 * * @param array $columns Array of columns available for the admin page. */ public function register_column_headers( $columns ) { $fields = $this->cmb->prop( 'fields' ); foreach ( $fields as $key => $field ) { if ( ! isset( $field['column'] ) ) { continue; } $column = $field['column']; if ( false === $column['position'] ) { $columns[ $field['id'] ] = $column['name']; } else { $before = array_slice( $columns, 0, absint( $column['position'] ) ); $before[ $field['id'] ] = $column['name']; $columns = $before + $columns; } $column['field'] = $field; $this->columns[ $field['id'] ] = $column; } return $columns; } /** * The CMB2 field column display output. * * @since 2.2.2 * * @param string $column_name Current column name. * @param mixed $object_id Current object ID. */ public function column_display( $column_name, $object_id ) { if ( isset( $this->columns[ $column_name ] ) ) { $field = new CMB2_Field( array( 'field_args' => $this->columns[ $column_name ]['field'], 'object_type' => $this->object_type, 'object_id' => $this->cmb->object_id( $object_id ), 'cmb_id' => $this->cmb->cmb_id, ) ); $this->cmb->get_field( $field )->render_column(); } } /** * Returns the column display. * * @since 2.2.2 */ public function return_column_display( $empty, $custom_column, $object_id ) { ob_start(); $this->column_display( $custom_column, $object_id ); $column = ob_get_clean(); return $column ? $column : $empty; } /** * Output the CMB2 box/fields in an alternate context (not in a standard metabox area). * * @since 2.2.4 */ public function add_context_metaboxes() { if ( ! $this->show_on() ) { return; } $page = get_current_screen()->id; foreach ( $this->cmb->box_types() as $object_type ) { $screen = convert_to_screen( $object_type ); // If we're on the right post-type/object... if ( isset( $screen->id ) && $screen->id === $page ) { // Show the box. $this->output_context_metabox(); } } } /** * Output the CMB2 box/fields in an alternate context (not in a standard metabox area). * * @since 2.2.4 */ public function output_context_metabox() { $title = $this->cmb->prop( 'title' ); /* * To keep from outputting the open/close markup, do not include * a 'title' property in your metabox registration array. * * To output the fields 'naked' (without a postbox wrapper/style), then * add a `'remove_box_wrap' => true` to your metabox registration array. */ $add_wrap = ! empty( $title ) || ! $this->cmb->prop( 'remove_box_wrap' ); $add_handle = $add_wrap && ! empty( $title ); // Open the context-box wrap. $this->context_box_title_markup_open( $add_handle ); // Show the form fields. $this->cmb->show_form(); // Close the context-box wrap. $this->context_box_title_markup_close( $add_handle ); } /** * Output the opening markup for a context box. * * @since 2.2.4 * @param bool $add_handle Whether to add the metabox handle and opening div for .inside. */ public function context_box_title_markup_open( $add_handle = true ) { $title = $this->cmb->prop( 'title' ); $page = get_current_screen()->id; add_filter( "postbox_classes_{$page}_{$this->cmb->cmb_id}", array( $this, 'postbox_classes' ) ); echo '