diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 536a1f6..66283ab 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -49,6 +49,7 @@ + diff --git a/custom_metadata.php b/custom_metadata.php index 2c6a7c1..5f767d0 100644 --- a/custom_metadata.php +++ b/custom_metadata.php @@ -1,152 +1,238 @@ - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -*/ +/** + * Plugin Name: Custom Metadata Manager + * Plugin URI: http://wordpress.org/extend/plugins/custom-metadata/ + * Description: An easy way to add custom fields to your object types (post, pages, custom post types, users) + * Author: Automattic, Stresslimit & Contributors + * Version: 0.8-dev + * Author URI: https://github.com/Automattic/custom-metadata/ + * + * Copyright 2010-2013 The Contributors + * + * GNU General Public License, Free Software Foundation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ /** - * set this to true in your wp-config.php file to enable debug/test mode + * Set this to true in your wp-config.php file to enable debug/test mode */ -if ( ! defined( 'CUSTOM_METADATA_MANAGER_DEBUG' ) ) +if ( ! defined( 'CUSTOM_METADATA_MANAGER_DEBUG' ) ) { define( 'CUSTOM_METADATA_MANAGER_DEBUG', false ); +} -if ( CUSTOM_METADATA_MANAGER_DEBUG ) - include_once 'custom_metadata_examples.php'; +if ( CUSTOM_METADATA_MANAGER_DEBUG ) { + require_once 'custom_metadata_examples.php'; +} +/** + * The custom_metadata_manager class. + */ class custom_metadata_manager { + /** + * @var array + */ var $errors = array(); + /** + * @var array + */ var $metadata = array(); + /** + * @var array + */ var $_non_post_types = array( 'user', 'comment' ); - // Object types that come "built-in" with WordPress + /** + * Object types that come "built-in" with WordPress. + * + * @var array + */ var $_builtin_object_types = array( 'post', 'page', 'user', 'comment' ); - // Column filter names + /** + * Column filter names. + * + * @var array + */ var $_column_types = array( 'posts', 'pages', 'users', 'comments' ); - // field types - var $_field_types = array( 'text', 'textarea', 'password', 'number', 'email', 'telephone', 'checkbox', 'radio', 'select', 'multi_select', 'upload', 'wysiwyg', 'datepicker', 'datetimepicker', 'timepicker', 'colorpicker', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_checkbox', 'link' ); - - // field types that are cloneable + /** + * Field types. + * + * @var array + */ + var $_field_types = array( 'text', 'textarea', 'password', 'number', 'email', 'telephone', 'checkbox', 'radio', 'select', 'multi_select', 'upload', 'wysiwyg', 'datepicker', 'datetimepicker', 'timepicker', 'colorpicker', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_checkbox', 'link' ); + + /** + * Field types that are cloneable. + * + * @var array + */ var $_cloneable_field_types = array( 'text', 'textarea', 'upload', 'password', 'number', 'email', 'tel' ); - // field types that support a default value + /** + * Field types that support a default value. + * + * @var array + */ var $_field_types_that_support_default_value = array( 'text', 'textarea', 'password', 'number', 'email', 'telephone', 'upload', 'wysiwyg', 'datepicker', 'datetimepicker', 'timepicker', 'link' ); - // field types that support the placeholder attribute + /** + * Field types that support the placeholder attribute. + * + * @var array + */ var $_field_types_that_support_placeholder = array( 'text', 'textarea', 'password', 'number', 'email', 'tel', 'upload', 'datepicker', 'datetimepicker', 'timepicker', 'link' ); - // field types that are read only by default + // field types that are read only by default. var $_field_types_that_are_read_only = array( 'upload', 'link', 'datepicker', 'datetimepicker', 'timepicker' ); - // field types that support being part of a multifield group - // @todo: workarounds needed for other field types + /** + * Field types that support being part of a multifield group. + * + * @todo: workarounds needed for other field types. + * + * @var array + */ var $_field_types_that_support_multifield = array( 'text', 'textarea', 'password', 'number', 'email', 'tel', 'select' ); - // taxonomy types + /** + * Taxonomy types. + * + * @var array + */ var $_taxonomy_fields = array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_checkbox', 'taxonomy_multi_select' ); - // filed types that are saved as multiples but not cloneable + /** + * Field types that are saved as multiples but not cloneable. + * + * @var array + */ var $_multiple_not_cloneable = array( 'taxonomy_checkbox' ); - // fields that always save as an array + /** + * Fields that always save as an array. + * + * @var array + */ var $_always_multiple_fields = array( 'taxonomy_checkbox', 'multi_select', 'taxonomy_multi_select' ); - // Object types whose columns are generated through apply_filters instead of do_action + /** + * Object types whose columns are generated through apply_filters instead of do_action. + * + * @var array + */ var $_column_filter_object_types = array( 'user' ); - // Whitelisted pages that get stylesheets and scripts + /** + * Whitelisted pages that get stylesheets and scripts. + * + * @var array + */ var $_pages_whitelist = array( 'edit.php', 'post.php', 'post-new.php', 'users.php', 'profile.php', 'user-edit.php', 'edit-comments.php', 'comment.php' ); - // the default args used for the wp_editor function + /** + * The default args used for the wp_editor function. + * + * @var array + */ var $default_editor_args = array(); - // singleton instance + /** + * Singleton instance. + * + * @var custom_metadata_manager + */ private static $instance; + /** + * Get/Create our singleton instance + * + * @return custom_metadata_manager Singelton + */ public static function instance() { - if ( isset( self::$instance ) ) + if ( isset( self::$instance ) ) { return self::$instance; + } - self::$instance = new custom_metadata_manager; + self::$instance = new custom_metadata_manager(); self::$instance->run_initial_hooks(); return self::$instance; } - // do nothing on construct + /** + * Do nothing on construct. + */ function __construct() {} + /** + * Main plugin hook. + */ function run_initial_hooks() { add_action( 'admin_init', array( $this, 'admin_init' ), 1000, 0 ); } + /** + * Set up plugin config. + */ function admin_init() { global $pagenow; - // filter our vars - $this->_non_post_types = apply_filters( 'custom_metadata_manager_non_post_types', $this->_non_post_types ); - $this->_builtin_object_types = apply_filters( 'custom_metadata_manager_builtin_object_types', $this->_builtin_object_types ); - $this->_column_types = apply_filters( 'custom_metadata_manager_column_types', $this->_column_types ); - $this->_field_types = apply_filters( 'custom_metadata_manager_field_types', $this->_field_types ); - $this->_cloneable_field_types = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_cloneable_field_types ); + // filter our vars. + $this->_non_post_types = apply_filters( 'custom_metadata_manager_non_post_types', $this->_non_post_types ); + $this->_builtin_object_types = apply_filters( 'custom_metadata_manager_builtin_object_types', $this->_builtin_object_types ); + $this->_column_types = apply_filters( 'custom_metadata_manager_column_types', $this->_column_types ); + $this->_field_types = apply_filters( 'custom_metadata_manager_field_types', $this->_field_types ); + $this->_cloneable_field_types = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_cloneable_field_types ); $this->_field_types_that_support_default_value = apply_filters( 'custom_metadata_manager_field_types_that_support_default_value', $this->_field_types_that_support_default_value ); - $this->_field_types_that_support_placeholder = apply_filters( 'custom_metadata_manager_field_types_that_support_placeholder', $this->_field_types_that_support_placeholder ); - $this->_field_types_that_are_read_only = apply_filters( 'custom_metadata_manager_field_types_that_are_read_only', $this->_field_types_that_are_read_only ); - $this->_field_types_that_support_multifield = apply_filters( 'custom_metadata_manager_field_types_that_support_multifield', $this->_field_types_that_support_multifield ); - $this->_taxonomy_fields = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_taxonomy_fields ); - $this->_column_filter_object_types = apply_filters( 'custom_metadata_manager_column_filter_object_types', $this->_column_filter_object_types ); - $this->_pages_whitelist = apply_filters( 'custom_metadata_manager_pages_whitelist', $this->_pages_whitelist ); - $this->default_editor_args = apply_filters( 'custom_metadata_manager_default_editor_args', $this->default_editor_args ); - - define( 'CUSTOM_METADATA_MANAGER_SELECT2_VERSION', '3.2' ); // version for included select2.js - define( 'CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION', '1.2' ); // version for included timepicker + $this->_field_types_that_support_placeholder = apply_filters( 'custom_metadata_manager_field_types_that_support_placeholder', $this->_field_types_that_support_placeholder ); + $this->_field_types_that_are_read_only = apply_filters( 'custom_metadata_manager_field_types_that_are_read_only', $this->_field_types_that_are_read_only ); + $this->_field_types_that_support_multifield = apply_filters( 'custom_metadata_manager_field_types_that_support_multifield', $this->_field_types_that_support_multifield ); + $this->_taxonomy_fields = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_taxonomy_fields ); + $this->_column_filter_object_types = apply_filters( 'custom_metadata_manager_column_filter_object_types', $this->_column_filter_object_types ); + $this->_pages_whitelist = apply_filters( 'custom_metadata_manager_pages_whitelist', $this->_pages_whitelist ); + $this->default_editor_args = apply_filters( 'custom_metadata_manager_default_editor_args', $this->default_editor_args ); + + define( 'CUSTOM_METADATA_MANAGER_SELECT2_VERSION', '3.2' ); // version for included select2.js. + define( 'CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION', '1.2' ); // version for included timepicker. define( 'CUSTOM_METADATA_MANAGER_VERSION', '0.8-dev' ); - define( 'CUSTOM_METADATA_MANAGER_URL' , apply_filters( 'custom_metadata_manager_url', trailingslashit( plugins_url( '', __FILE__ ) ) ) ); + define( 'CUSTOM_METADATA_MANAGER_URL', apply_filters( 'custom_metadata_manager_url', trailingslashit( plugins_url( '', __FILE__ ) ) ) ); $this->init_object_types(); - // Hook into load to initialize custom columns + // Hook into load to initialize custom columns. if ( in_array( $pagenow, $this->_pages_whitelist ) ) { add_action( 'load-' . $pagenow, array( $this, 'init_metadata' ) ); } - // Hook into admin_notices to show errors - if ( current_user_can( 'manage_options' ) ) + // Hook into admin_notices to show errors. + if ( current_user_can( 'manage_options' ) ) { add_action( 'admin_notices', array( $this, '_display_registration_errors' ) ); + } do_action( 'custom_metadata_manager_init' ); do_action( 'custom_metadata_manager_admin_init' ); } function init_object_types() { - foreach ( array_merge( get_post_types(), $this->_builtin_object_types ) as $object_type ) - $this->metadata[$object_type] = array(); + foreach ( array_merge( get_post_types(), $this->_builtin_object_types ) as $object_type ) { + $this->metadata[ $object_type ] = array(); + } } function init_metadata() { @@ -157,26 +243,26 @@ function init_metadata() { $this->init_columns(); - // Handle actions related to users + // Handle actions related to users. if ( $object_type == 'user' ) { global $user_id; - if ( empty( $user_id ) ) + if ( empty( $user_id ) ) { $user_id = get_current_user_id(); + } - // Editing another user's profile + // Editing another user's profile. add_action( 'edit_user_profile', array( $this, 'add_user_metadata_groups' ) ); add_action( 'edit_user_profile_update', array( $this, 'save_user_metadata' ) ); - // Allow user-editable fields on "Your Profile" + // Allow user-editable fields on "Your Profile". add_action( 'show_user_profile', array( $this, 'add_user_metadata_groups' ) ); add_action( 'personal_options_update', array( $this, 'save_user_metadata' ) ); - } else { - // Hook in to metaboxes - add_action( 'add_meta_boxes', array( $this, "add_post_metadata_groups" ) ); + // Hook in to metaboxes. + add_action( 'add_meta_boxes', array( $this, 'add_post_metadata_groups' ) ); - // Hook in to save + // Hook in to save. add_action( 'save_post', array( $this, 'save_post_metadata' ) ); add_action( 'edit_comment', array( $this, 'save_comment_metadata' ) ); } @@ -187,59 +273,64 @@ function init_metadata() { } function init_columns() { - $object_type = $this->_get_object_type_context(); // This is not really that clean, but it works. Damn inconsistencies! if ( post_type_exists( $object_type ) ) { - $column_header_name = sprintf( '%s_posts', $object_type ); + $column_header_name = sprintf( '%s_posts', $object_type ); $column_content_name = ( 'page' != $object_type ) ? 'posts' : 'pages'; } elseif ( $object_type == 'comment' ) { - $column_header_name = 'edit-comments'; + $column_header_name = 'edit-comments'; $column_content_name = 'comments'; } else { - // users + // users. $column_header_name = $column_content_name = $object_type . 's'; } - // Hook into Column Headers + // Hook into Column Headers. add_filter( "manage_{$column_header_name}_columns", array( $this, 'add_metadata_column_headers' ) ); - // User and Posts have different functions + // User and Posts have different functions. $custom_column_content_function = array( $this, "add_{$object_type}_metadata_column_content" ); - if ( ! is_callable( $custom_column_content_function ) ) + if ( ! is_callable( $custom_column_content_function ) ) { $custom_column_content_function = array( $this, 'add_metadata_column_content' ); + } // Hook into Column Content. Users get filtered, others get actioned. - if ( ! in_array( $object_type, $this->_column_filter_object_types ) ) + if ( ! in_array( $object_type, $this->_column_filter_object_types ) ) { add_action( "manage_{$column_content_name}_custom_column", $custom_column_content_function, 10, 3 ); - else + } else { add_filter( "manage_{$column_content_name}_custom_column", $custom_column_content_function, 10, 3 ); - + } } + /** + * Enqueues necessary scripts. + */ function enqueue_scripts() { wp_enqueue_media(); wp_enqueue_script( 'wplink' ); wp_enqueue_script( 'wpdialogs-popup' ); - wp_enqueue_style( 'wp-jquery-ui-dialog' ); - wp_enqueue_script( 'select2', apply_filters( 'custom_metadata_manager_select2_js', CUSTOM_METADATA_MANAGER_URL .'js/select2.min.js' ), array( 'jquery' ), CUSTOM_METADATA_MANAGER_SELECT2_VERSION, true ); - wp_enqueue_script( 'timepicker', apply_filters( 'custom_metadata_manager_timepicker_js', CUSTOM_METADATA_MANAGER_URL .'js/jquery-ui-timepicker.min.js' ), array( 'jquery', 'jquery-ui-datepicker' ), CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION, true ); - wp_enqueue_script( 'custom-metadata-manager-js', apply_filters( 'custom_metadata_manager_default_js', CUSTOM_METADATA_MANAGER_URL .'js/custom-metadata-manager.js' ), array( 'jquery', 'jquery-ui-datepicker', 'select2' ), CUSTOM_METADATA_MANAGER_VERSION, true ); + wp_enqueue_style( 'wp-jquery-ui-dialog' ); // @todo Is this here for a reason? + wp_enqueue_script( 'select2', apply_filters( 'custom_metadata_manager_select2_js', CUSTOM_METADATA_MANAGER_URL . 'js/select2.min.js' ), array( 'jquery' ), CUSTOM_METADATA_MANAGER_SELECT2_VERSION, true ); + wp_enqueue_script( 'timepicker', apply_filters( 'custom_metadata_manager_timepicker_js', CUSTOM_METADATA_MANAGER_URL . 'js/jquery-ui-timepicker.min.js' ), array( 'jquery', 'jquery-ui-datepicker' ), CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION, true ); + wp_enqueue_script( 'custom-metadata-manager-js', apply_filters( 'custom_metadata_manager_default_js', CUSTOM_METADATA_MANAGER_URL . 'js/custom-metadata-manager.js' ), array( 'jquery', 'jquery-ui-datepicker', 'select2' ), CUSTOM_METADATA_MANAGER_VERSION, true ); wp_enqueue_script( 'wp-color-picker' ); } + /** + * Enqueues necessary styles. + */ function enqueue_styles() { wp_enqueue_style( 'wp-jquery-ui-dialog' ); wp_enqueue_style( 'editor-buttons' ); - wp_enqueue_style( 'custom-metadata-manager-css', apply_filters( 'custom_metadata_manager_default_css', CUSTOM_METADATA_MANAGER_URL .'css/custom-metadata-manager.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION ); - wp_enqueue_style( 'jquery-ui-datepicker', apply_filters( 'custom_metadata_manager_jquery_ui_css', CUSTOM_METADATA_MANAGER_URL .'css/jquery-ui-smoothness.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION ); - wp_enqueue_style( 'select2', apply_filters( 'custom_metadata_manager_select2_css', CUSTOM_METADATA_MANAGER_URL .'css/select2.css' ), array(), CUSTOM_METADATA_MANAGER_SELECT2_VERSION ); + wp_enqueue_style( 'custom-metadata-manager-css', apply_filters( 'custom_metadata_manager_default_css', CUSTOM_METADATA_MANAGER_URL . 'css/custom-metadata-manager.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION ); + wp_enqueue_style( 'jquery-ui-datepicker', apply_filters( 'custom_metadata_manager_jquery_ui_css', CUSTOM_METADATA_MANAGER_URL . 'css/jquery-ui-smoothness.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION ); + wp_enqueue_style( 'select2', apply_filters( 'custom_metadata_manager_select2_css', CUSTOM_METADATA_MANAGER_URL . 'css/select2.css' ), array(), CUSTOM_METADATA_MANAGER_SELECT2_VERSION ); wp_enqueue_style( 'wp-color-picker' ); } function add_metadata_column_headers( $columns ) { - $object_type = $this->_get_object_type_context(); if ( $object_type ) { @@ -247,7 +338,7 @@ function add_metadata_column_headers( $columns ) { foreach ( $fields as $field_slug => $field ) { if ( $this->is_field_addable_to_columns( $field_slug, $field ) ) { - $columns[$field_slug] = is_string( $field->display_column ) ? $field->display_column : $field->label; + $columns[ $field_slug ] = is_string( $field->display_column ) ? $field->display_column : $field->label; } } } @@ -259,19 +350,19 @@ function add_user_metadata_column_content( $param, $name, $object_id ) { } function add_metadata_column_content( $name, $object_id, $column_content = '' ) { - $object_type = $this->_get_object_type_context(); - $field_slug = $name; + $field_slug = $name; if ( $this->is_registered_object_type( $object_type ) && $this->is_registered_field( $field_slug, null, $object_type ) ) { - $field = $this->get_field( $field_slug, null, $object_type ); + $field = $this->get_field( $field_slug, null, $object_type ); $column_content = $this->_metadata_column_content( $field_slug, $field, $object_type, $object_id ); } - if ( $column_content && ! in_array( $object_type, $this->_column_filter_object_types ) ) + if ( $column_content && ! in_array( $object_type, $this->_column_filter_object_types ) ) { echo $column_content; - else + } else { return $column_content; + } } function add_metadata_field( $field_slug, $object_types = array( 'post' ), $args = array() ) { @@ -279,135 +370,133 @@ function add_metadata_field( $field_slug, $object_types = array( 'post' ), $args if ( ! $localized_strings ) { $localized_strings = (object) array( - 'upload_modal_title' => __( 'Choose a file', 'custom-metadata' ), // upload modal title (for upload field only) - 'upload_modal_button_text' => __( 'Select this file', 'custom-metadata' ), // upload modal button text (for upload field only) - 'upload_clear_button_text' => __( 'Clear', 'custom-metadata' ), // upload clear field text (for upload field only) - 'link_modal_button_text' => __( 'Select', 'custom-metadata' ), // link field button text + 'upload_modal_title' => __( 'Choose a file', 'custom-metadata' ), // upload modal title (for upload field only). + 'upload_modal_button_text' => __( 'Select this file', 'custom-metadata' ), // upload modal button text (for upload field only). + 'upload_clear_button_text' => __( 'Clear', 'custom-metadata' ), // upload clear field text (for upload field only). + 'link_modal_button_text' => __( 'Select', 'custom-metadata' ), // link field button text. ); } $defaults = array( - 'group' => '', // To which meta_box the field should be added - 'multifield' => false, // which multifield does this field belong to, if any - 'field_type' => 'text', // The type of field; possibly values: text, checkbox, radio, select, image - 'label' => $field_slug, // Label for the field - 'slug' => $field_slug, // Slug for the field - 'description' => '', // Description of the field, displayed below the input - 'values' => array(), // values for select, checkbox, radio buttons - 'default_value' => '', // default value - 'placeholder' => '', - 'display_callback' => '', // function to custom render the input - 'sanitize_callback' => '', - 'display_column' => false, // Add the field to the columns when viewing all posts - 'display_column_callback' => '', - 'add_to_quick_edit' => false, // (post only) Add the field to Quick edit - 'required_cap' => false, // the cap required to view and edit the field - 'multiple' => false, // can the field be duplicated with a click of a button - 'readonly' => false, // makes the field be readonly - 'select2' => false, // applies select2.js (work on select and multi select field types) - 'min' => false, // a minimum value (for number field only) - 'max' => false, // a maximum value (for number field only) - 'upload_modal_title' => $localized_strings->upload_modal_title, + 'group' => '', // To which meta_box the field should be added. + 'multifield' => false, // which multifield does this field belong to, if any. + 'field_type' => 'text', // The type of field; possibly values: text, checkbox, radio, select, image. + 'label' => $field_slug, // Label for the field. + 'slug' => $field_slug, // Slug for the field. + 'description' => '', // Description of the field, displayed below the input. + 'values' => array(), // values for select, checkbox, radio buttons. + 'default_value' => '', // default value. + 'placeholder' => '', + 'display_callback' => '', // function to custom render the input. + 'sanitize_callback' => '', + 'display_column' => false, // Add the field to the columns when viewing all posts. + 'display_column_callback' => '', + 'add_to_quick_edit' => false, // (post only) Add the field to Quick edit. + 'required_cap' => false, // the cap required to view and edit the field. + 'multiple' => false, // can the field be duplicated with a click of a button. + 'readonly' => false, // makes the field be readonly. + 'select2' => false, // applies select2.js (work on select and multi select field types). + 'min' => false, // a minimum value (for number field only). + 'max' => false, // a maximum value (for number field only). + 'upload_modal_title' => $localized_strings->upload_modal_title, 'upload_modal_button_text' => $localized_strings->upload_modal_button_text, 'upload_clear_button_text' => $localized_strings->upload_clear_button_text, - 'link_modal_button_text' => $localized_strings->link_modal_button_text, + 'link_modal_button_text' => $localized_strings->link_modal_button_text, ); - // upload field is readonly by default (can be set explicitly to false though) - if ( ! empty( $args['field_type'] ) && in_array( $args['field_type'], $this->_field_types_that_are_read_only ) ) + // upload field is readonly by default (can be set explicitly to false though). + if ( ! empty( $args['field_type'] ) && in_array( $args['field_type'], $this->_field_types_that_are_read_only ) ) { $defaults['readonly'] = true; + } - // `chosen` arg is the same as `select2` arg + // `chosen` arg is the same as `select2` arg. if ( isset( $args['chosen'] ) ) { $args['select2'] = $args['chosen']; unset( $args['chosen'] ); } - // Merge defaults with args + // Merge defaults with args. $field = wp_parse_args( $args, $defaults ); $field = (object) $field; - // Sanitize slug + // Sanitize slug. $field_slug = sanitize_key( $field_slug ); $group_slug = sanitize_key( $field->group ); - // Check to see if the user should see this field - if ( ! empty( $field->required_cap ) && ! current_user_can( $field->required_cap ) ) + // Check to see if the user should see this field. + if ( ! empty( $field->required_cap ) && ! current_user_can( $field->required_cap ) ) { return; + } $field = apply_filters( 'custom_metadata_manager_add_metadata_field', $field, $field_slug, $group_slug, $object_types ); - if ( ! $this->_validate_metadata_field( $field_slug, $field, $group_slug, $object_types ) ) + if ( ! $this->_validate_metadata_field( $field_slug, $field, $group_slug, $object_types ) ) { return; + } -// $object_types = (array) $object_type; -// if ( $field->multifield && $this->_multifield_exists_for_group_object( $field->multifield, $group_slug, array_shift( $object_types ) ) ) { -// $this->add_field_to_multifield( $field_slug, $field, $group_slug, $object_types ); -// } else { - // add to group - $this->add_field_to_group( $field_slug, $field, $group_slug, $object_types ); -// } - + // add to group. + $this->add_field_to_group( $field_slug, $field, $group_slug, $object_types ); } function add_multifield( $slug, $object_types = array( 'post' ), $args = array() ) { - $defaults = array( - 'group' => '', // To which meta_box the multifield should be added - 'label' => $slug, // Label for the multifield - 'description' => '', // Description of the multifield, displayed below all the fields - 'required_cap' => false, // the cap required to view and edit the multifield + 'group' => '', // To which meta_box the multifield should be added. + 'label' => $slug, // Label for the multifield. + 'description' => '', // Description of the multifield, displayed below all the fields. + 'required_cap' => false, // the cap required to view and edit the multifield. ); - // Merge defaults with args - $multifield = wp_parse_args( $args, $defaults ); - $multifield['multifield'] = true; // force it - $multifield = (object) $multifield; + // Merge defaults with args. + $multifield = wp_parse_args( $args, $defaults ); + $multifield['multifield'] = true; // force it. + $multifield = (object) $multifield; - // Sanitize slug - $slug = sanitize_key( $slug ); + // Sanitize slug. + $slug = sanitize_key( $slug ); $group_slug = sanitize_key( $multifield->group ); - // Check to see if the user should see this field - if ( ! empty( $multifield->required_cap ) && ! current_user_can( $multifield->required_cap ) ) + // Check to see if the user should see this field. + if ( ! empty( $multifield->required_cap ) && ! current_user_can( $multifield->required_cap ) ) { return; + } $multifield = apply_filters( 'custom_metadata_manager_add_multifield', $multifield, $slug, $group_slug, $object_types ); - if ( ! $this->_validate_metadata_field( $slug, $multifield, $group_slug, $object_types ) ) + if ( ! $this->_validate_metadata_field( $slug, $multifield, $group_slug, $object_types ) ) { return; + } - // Add to group + // Add to group. $this->add_multifield_to_group( $slug, $multifield, $group_slug, $object_types ); - } function add_metadata_group( $group_slug, $object_types, $args = array() ) { - $defaults = array( - 'label' => $group_slug, // Label for the group - 'description' => '', // Description of the group - 'context' => 'normal', // (post only) - 'priority' => 'default', // (post only) - 'autosave' => false, // (post only) Should the group be saved in autosave? - 'required_cap' => false, // the cap required to view and edit the group + 'label' => $group_slug, // Label for the group. + 'description' => '', // Description of the group. + 'context' => 'normal', // (post only). + 'priority' => 'default', // (post only). + 'autosave' => false, // (post only) Should the group be saved in autosave? + 'required_cap' => false, // the cap required to view and edit the group. ); - // Merge defaults with args + // Merge defaults with args. $group = wp_parse_args( $args, $defaults ); $group = (object) $group; - // Sanitize slug + // Sanitize slug. $group_slug = sanitize_key( $group_slug ); $group = apply_filters( 'custom_metadata_manager_add_metadata_group', $group, $group_slug, $object_types ); - // Check to see if the user has caps to view/edit this group - if ( ! empty( $group->required_cap ) && ! current_user_can( $group->required_cap ) ) + // Check to see if the user has caps to view/edit this group. + if ( ! empty( $group->required_cap ) && ! current_user_can( $group->required_cap ) ) { return; + } - if ( !$this->_validate_metadata_group( $group_slug, $group, $object_types ) ) + if ( ! $this->_validate_metadata_group( $group_slug, $group, $object_types ) ) { return; + } $this->add_group_to_object_type( $group_slug, $group, $object_types ); } @@ -421,7 +510,7 @@ function add_field_to_group( $field_slug, $field, $group_slug, $object_types ) { $group_slug = sprintf( 'single-group-%1$s-%2$s', $object_type, $field_slug ); } - // If group doesn't exist, create group + // If group doesn't exist, create group. if ( ! $this->is_registered_group( $group_slug, $object_type ) ) { $this->add_metadata_group( $group_slug, $object_type, array( 'label' => ( ! empty( $field->label ) ) ? $field->label : $field_slug ) ); $field->group = $group_slug; @@ -439,7 +528,7 @@ function add_multifield_to_group( $slug, $multifield, $group_slug, $object_types $group_slug = sprintf( 'single-group-%1$s-%2$s', $object_type, $slug ); } - // If group doesn't exist, create group + // If group doesn't exist, create group. if ( ! $this->is_registered_group( $group_slug, $object_type ) ) { $this->add_metadata_group( $group_slug, $object_type, array( 'label' => ( ! empty( $multifield->label ) ) ? $multifield->label : $slug ) ); $multifield->group = $group_slug; @@ -506,7 +595,7 @@ function _validate_metadata_field( $field_slug, $field, $group_slug, $object_typ } function _add_registration_error( $field_slug, $error_message ) { - $this->errors[] = sprintf( __( '%1$s: %2$s', 'custom-metadata-manager' ), $field_slug, $error_message ); + $this->errors[] = sprintf( __( '%1$s: %2$s', 'custom-metadata' ), $field_slug, $error_message ); } function add_post_metadata_groups() { @@ -523,7 +612,7 @@ function add_post_metadata_groups() { $groups = $this->get_groups_in_object_type( $object_type ); - if ( $object_id && !empty( $groups ) ) { + if ( $object_id && ! empty( $groups ) ) { foreach ( $groups as $group_slug => $group ) { $this->add_post_metadata_group( $group_slug, $group, $object_type, $object_id ); } @@ -531,24 +620,36 @@ function add_post_metadata_groups() { } function add_post_metadata_group( $group_slug, $group, $object_type, $object_id ) { - $fields = $this->get_fields_in_group( $group_slug, $object_type ); if ( ! empty( $fields ) && $this->is_thing_added_to_object( $group_slug, $group, $object_type, $object_id ) ) { - add_meta_box( $group_slug, $group->label, array( $this, '_display_post_metadata_box' ), $object_type, $group->context, $group->priority, array( 'group' => $group, 'fields' => $fields ) ); + add_meta_box( + $group_slug, + $group->label, + array( $this, '_display_post_metadata_box' ), + $object_type, + $group->context, + $group->priority, + array( + 'group' => $group, + 'fields' => $fields, + ) + ); } } function add_user_metadata_groups() { global $user_id; - if ( !$user_id ) return; + if ( ! $user_id ) { + return; + } $object_type = 'user'; $groups = $this->get_groups_in_object_type( $object_type ); - if ( !empty( $groups ) ) { + if ( ! empty( $groups ) ) { foreach ( $groups as $group_slug => $group ) { $this->add_user_metadata_group( $group_slug, $group, $object_type, $user_id ); } @@ -558,14 +659,15 @@ function add_user_metadata_groups() { function add_user_metadata_group( $group_slug, $group, $object_type, $user_id ) { $fields = $this->get_fields_in_group( $group_slug, $object_type ); - if ( ! empty( $fields ) && $this->is_thing_added_to_object( $group_slug, $group, $object_type, $user_id ) ) + if ( ! empty( $fields ) && $this->is_thing_added_to_object( $group_slug, $group, $object_type, $user_id ) ) { $this->_display_user_metadata_box( $group_slug, $group, $object_type, $fields ); + } } function _display_user_metadata_box( $group_slug, $group, $object_type, $fields ) { global $user_id; -?> + ?>

label; ?>

@@ -585,19 +687,18 @@ function _display_user_metadata_box( $group_slug, $group, $object_type, $fields } function _display_post_metadata_box( $object, $meta_box ) { - - $group_slug = $meta_box['id']; - $group = $meta_box['args']['group']; - $fields = $meta_box['args']['fields']; + $group_slug = $meta_box['id']; + $group = $meta_box['args']['group']; + $fields = $meta_box['args']['fields']; $object_type = $this->_get_object_type_context(); // I really don't like using variable variables, but this is the path of least resistence. if ( isset( $object->{$object_type . '_ID'} ) ) { - $object_id = $object->{$object_type . '_ID'}; + $object_id = $object->{$object_type . '_ID'}; } elseif ( isset( $object->ID ) ) { $object_id = $object->ID; } else { - _e( 'Uh oh, something went wrong!', 'custom-metadata-manager' ); + _e( 'Uh oh, something went wrong!', 'custom-metadata' ); return; } @@ -605,7 +706,6 @@ function _display_post_metadata_box( $object, $meta_box ) { foreach ( $fields as $field_slug => $field ) { if ( $this->is_thing_added_to_object( $field_slug, $field, $object_type, $object_id ) ) { - if ( $this->_is_multifield( $field_slug ) ) { $this->_display_metadata_multifield( $field_slug, $field, $object_type, $object_id ); } elseif ( empty( $field->multifield ) ) { @@ -614,13 +714,14 @@ function _display_post_metadata_box( $object, $meta_box ) { } } - // Each group gets its own nonce + // Each group gets its own nonce. $this->_display_group_nonce( $group_slug, $object_type ); } function _display_group_description( $group ) { - if ( ! empty( $group->description ) ) + if ( ! empty( $group->description ) ) { printf( '
%s
', $group->description ); + } } function _display_group_nonce( $group_slug, $object_type ) { @@ -630,10 +731,11 @@ function _display_group_nonce( $group_slug, $object_type ) { function verify_group_nonce( $group_slug, $object_type ) { $nonce_key = $this->build_nonce_key( $group_slug, $object_type ); - if ( isset( $_POST[$nonce_key] ) ) - return wp_verify_nonce( $_POST[$nonce_key], 'save-metadata' ); - else + if ( isset( $_POST[ $nonce_key ] ) ) { + return wp_verify_nonce( $_POST[ $nonce_key ], 'save-metadata' ); + } else { return false; + } } function build_nonce_key( $group_slug, $object_type ) { @@ -642,7 +744,7 @@ function build_nonce_key( $group_slug, $object_type ) { function save_user_metadata( $user_id ) { $object_type = 'user'; - $groups = $this->get_groups_in_object_type( $object_type ); + $groups = $this->get_groups_in_object_type( $object_type ); foreach ( $groups as $group_slug => $group ) { $this->save_metadata_group( $group_slug, $group, $object_type, $user_id ); @@ -654,12 +756,13 @@ function save_post_metadata( $post_id ) { return; } $post_type = $this->_get_object_type_context(); - $groups = $this->get_groups_in_object_type( $post_type ); + $groups = $this->get_groups_in_object_type( $post_type ); foreach ( $groups as $group_slug => $group ) { - // TODO: Allow hook into autosave - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE && !$group->autosave ) + // TODO: Allow hook into autosave. + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE && ! $group->autosave ) { return $post_id; + } $this->save_metadata_group( $group_slug, $group, $post_type, $post_id ); } @@ -667,7 +770,7 @@ function save_post_metadata( $post_id ) { function save_comment_metadata( $comment_id ) { $object_type = 'comment'; - $groups = $this->get_groups_in_object_type( $object_type ); + $groups = $this->get_groups_in_object_type( $object_type ); foreach ( $groups as $group_slug => $group ) { $this->save_metadata_group( $group_slug, $group, $object_type, $comment_id ); @@ -675,7 +778,7 @@ function save_comment_metadata( $comment_id ) { } function save_metadata_group( $group_slug, $group, $object_type, $object_id ) { - if ( !$this->verify_group_nonce( $group_slug, $object_type ) ) { + if ( ! $this->verify_group_nonce( $group_slug, $object_type ) ) { return $object_id; } @@ -688,22 +791,20 @@ function save_metadata_group( $group_slug, $group, $object_type, $object_id ) { $this->save_metadata_field( $field_slug, $field, $object_type, $object_id ); } } - } function save_metadata_multifield( $slug, $multifield, $object_type, $object_id ) { - - if ( isset( $_POST[$slug] ) ) { + if ( isset( $_POST[ $slug ] ) ) { $multifield_value = array(); - $groupings = $_POST[$slug]; - $fields = $this->get_fields_in_multifield( $multifield->group, $slug, $object_type ); + $groupings = $_POST[ $slug ]; + $fields = $this->get_fields_in_multifield( $multifield->group, $slug, $object_type ); foreach ( $groupings as $grouping ) { $grouping_values = array(); foreach ( $fields as $field_slug => $field ) { - if ( ! empty( $grouping[$field_slug] ) ) { - $grouping_values[$field_slug] = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $grouping[$field_slug] ); + if ( ! empty( $grouping[ $field_slug ] ) ) { + $grouping_values[ $field_slug ] = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $grouping[ $field_slug ] ); } else { - $grouping_values[$field_slug] = ''; + $grouping_values[ $field_slug ] = ''; } } $multifield_value[] = $grouping_values; @@ -711,35 +812,39 @@ function save_metadata_multifield( $slug, $multifield, $object_type, $object_id $slug = sanitize_key( $slug ); - if ( ! in_array( $object_type, $this->_non_post_types ) ) + if ( ! in_array( $object_type, $this->_non_post_types ) ) { $object_type = 'post'; + } update_metadata( $object_type, $object_id, $slug, $multifield_value ); } else { $slug = sanitize_key( $slug ); - if ( ! in_array( $object_type, $this->_non_post_types ) ) + if ( ! in_array( $object_type, $this->_non_post_types ) ) { $object_type = 'post'; + } delete_metadata( $object_type, $object_id, $slug ); } } function save_metadata_field( $field_slug, $field, $object_type, $object_id ) { - if ( isset( $_POST[$field_slug] ) ) { - $value = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $_POST[$field_slug] ); + if ( isset( $_POST[ $field_slug ] ) ) { + $value = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $_POST[ $field_slug ] ); $this->_save_field_value( $field_slug, $field, $object_type, $object_id, $value ); - // save the attachment ID of the upload field as well - if ( $field->field_type == 'upload' && isset( $_POST[$field_slug . '_attachment_id'] ) ) - $this->_save_field_value( $field_slug . '_attachment_id', $field, $object_type, $object_id, absint( $_POST[$field_slug . '_attachment_id'] ) ); + // save the attachment ID of the upload field as well. + if ( $field->field_type == 'upload' && isset( $_POST[ $field_slug . '_attachment_id' ] ) ) { + $this->_save_field_value( $field_slug . '_attachment_id', $field, $object_type, $object_id, absint( $_POST[ $field_slug . '_attachment_id' ] ) ); + } } else { $this->_delete_field_value( $field_slug, $field, $object_type, $object_id ); - // delete the attachment ID of the upload field as well - if ( $field->field_type == 'upload' && isset( $_POST[$field_slug . '_attachment_id'] ) ) + // delete the attachment ID of the upload field as well. + if ( $field->field_type == 'upload' && isset( $_POST[ $field_slug . '_attachment_id' ] ) ) { $this->_delete_field_value( $field_slug . '_attachment_id', $field, $object_type, $object_id ); + } } } @@ -752,7 +857,7 @@ function get_metadata_field_value( $field_slug, $field, $object_type, $object_id } function is_registered_object_type( $object_type ) { - return array_key_exists( $object_type, $this->metadata ) /*&& is_array( $this->metadata[$object_type] )*/; + return array_key_exists( $object_type, $this->metadata ); /*&& is_array( $this->metadata[$object_type] )*/ } function is_registered_group( $group_slug, $object_type ) { @@ -760,10 +865,11 @@ function is_registered_group( $group_slug, $object_type ) { } function is_registered_field( $field_slug, $group_slug = '', $object_type ) { - if ( $group_slug ) + if ( $group_slug ) { return $this->is_registered_group( $group_slug, $object_type ) && array_key_exists( $field_slug, $this->get_fields_in_group( $group_slug, $object_type ) ); - else + } else { return array_key_exists( $field_slug, $this->get_fields_in_object_type( $object_type ) ); + } } function is_field_in_group( $field_slug, $group_slug, $object_type ) { @@ -792,7 +898,7 @@ function get_field( $field_slug, $group_slug, $object_type ) { function get_group( $group_slug, $object_type ) { if ( $this->is_registered_group( $group_slug, $object_type ) ) { $groups = $this->get_groups_in_object_type( $object_type ); - $group = $groups[$group_slug]; + $group = $groups[ $group_slug ]; return $group; } return null; @@ -803,35 +909,41 @@ function get_object_types() { } function get_groups_in_object_type( $object_type ) { - if ( $this->is_registered_object_type( $object_type ) ) - return $this->metadata[$object_type]; + if ( $this->is_registered_object_type( $object_type ) ) { + return $this->metadata[ $object_type ]; + } return array(); } function get_single_field_in_group( $field_slug, $group_slug, $object_type ) { $fields = $this->get_fields_in_group( $group_slug, $object_type ); - return isset( $fields[$field_slug] ) ? $fields[$field_slug] : null; + return isset( $fields[ $field_slug ] ) ? $fields[ $field_slug ] : null; } function get_fields_in_group( $group_slug, $object_type ) { $group = $this->get_group( $group_slug, $object_type ); - if ( $group ) return (array) $group->fields; + if ( $group ) { + return (array) $group->fields; + } return array(); } function get_fields_in_multifield( $group_slug, $multifield_slug, $object_type ) { - $group = $this->get_group( $group_slug, $object_type ); + $group = $this->get_group( $group_slug, $object_type ); $fields_in_multifield = array(); - if ( empty( $group ) || empty( $group->fields ) || empty( $group->fields[$multifield_slug] ) ) + if ( empty( $group ) || empty( $group->fields ) || empty( $group->fields[ $multifield_slug ] ) ) { return $fields_in_multifield; + } $_multifields = wp_list_pluck( $group->fields, 'multifield' ); foreach ( $_multifields as $_field_key => $_multifield ) { - if ( empty( $_multifield ) || true === $_multifield ) + if ( empty( $_multifield ) || true === $_multifield ) { continue; + } - if ( $multifield_slug == $_multifield || $multifield_slug == '_x_multifield_' . $_multifield ) - $fields_in_multifield[$_field_key] = $group->fields[$_field_key]; + if ( $multifield_slug == $_multifield || $multifield_slug == '_x_multifield_' . $_multifield ) { + $fields_in_multifield[ $_field_key ] = $group->fields[ $_field_key ]; + } } return $fields_in_multifield; @@ -839,7 +951,7 @@ function get_fields_in_multifield( $group_slug, $multifield_slug, $object_type ) function get_single_field_in_object_type( $field_slug, $object_type ) { $fields = $this->get_fields_in_object_type( $object_type ); - return isset( $fields[$field_slug] ) ? $fields[$field_slug] : null; + return isset( $fields[ $field_slug ] ) ? $fields[ $field_slug ] : null; } function get_fields_in_object_type( $object_type ) { @@ -851,24 +963,24 @@ function get_fields_in_object_type( $object_type ) { } function _push_group( $group_slug, $group, $object_type ) { - $this->metadata[$object_type][$group_slug] = $group; + $this->metadata[ $object_type ][ $group_slug ] = $group; } function _push_field( $field_slug, $field, $group_slug, $object_type ) { - $this->metadata[$object_type][$group_slug]->fields[$field_slug] = $field; + $this->metadata[ $object_type ][ $group_slug ]->fields[ $field_slug ] = $field; } function _push_multifield( $slug, $multifield, $group_slug, $object_type ) { - $this->metadata[$object_type][$group_slug]->fields['_x_multifield_' . $slug] = $multifield; + $this->metadata[ $object_type ][ $group_slug ]->fields[ '_x_multifield_' . $slug ] = $multifield; } function _multifield_exists_for_group_object( $slug, $group_slug, $object_type ) { $slug = '_x_multifield_' . $slug; return ( - ! empty( $this->metadata[$object_type] ) && - ! empty( $this->metadata[$object_type][$group_slug] ) && - ! empty( $this->metadata[$object_type][$group_slug]->fields ) && - array_key_exists( $slug, $this->metadata[$object_type][$group_slug]->fields ) + ! empty( $this->metadata[ $object_type ] ) && + ! empty( $this->metadata[ $object_type ][ $group_slug ] ) && + ! empty( $this->metadata[ $object_type ][ $group_slug ]->fields ) && + array_key_exists( $slug, $this->metadata[ $object_type ][ $group_slug ]->fields ) ); } @@ -877,17 +989,18 @@ function _is_multifield( $slug ) { } function is_thing_added_to_object( $thing_slug, $thing, $object_type, $object_id, $object_slug = '' ) { - if ( isset( $thing->exclude ) ) { - if ( is_callable( $thing->exclude ) ) + if ( is_callable( $thing->exclude ) ) { return ! (bool) call_user_func( $thing->exclude, $thing_slug, $thing, $object_type, $object_id, $object_slug ); + } return ! $this->does_id_array_match_object( $thing->exclude, $object_type, $object_id, $object_slug ); } if ( isset( $thing->include ) ) { - if ( is_callable( $thing->include ) ) + if ( is_callable( $thing->include ) ) { return (bool) call_user_func( $thing->include, $thing_slug, $thing, $object_type, $object_id, $object_slug ); - return $this->does_id_array_match_object( $thing->include, $object_type, $object_id, $object_slug ); + } + return $this->does_id_array_match_object( $thing->include, $object_type, $object_id, $object_slug ); } return true; @@ -895,16 +1008,16 @@ function is_thing_added_to_object( $thing_slug, $thing, $object_type, $object_id function does_id_array_match_object( $id_array, $object_type, $object_id, $object_slug = '' ) { if ( is_array( $id_array ) ) { - if ( isset( $id_array[$object_type] ) ) { - if ( is_array( $id_array[$object_type] ) ) { - // array( 'user' => array( 123, 'postname' ) ) - return $this->does_id_array_match_object( $id_array[$object_type], $object_type, $object_id, $object_slug ); + if ( isset( $id_array[ $object_type ] ) ) { + if ( is_array( $id_array[ $object_type ] ) ) { + // array( 'user' => array( 123, 'postname' ) ). + return $this->does_id_array_match_object( $id_array[ $object_type ], $object_type, $object_id, $object_slug ); } else { - // array( 'post' => 123 ) - return $this->does_id_match_object( $id_array[$object_type], $object_id, $object_slug ); + // array( 'post' => 123 ). + return $this->does_id_match_object( $id_array[ $object_type ], $object_id, $object_slug ); } } else { - // array( 123, 456, 'postname' ) + // array( 123, 456, 'postname' ). $match = false; foreach ( $id_array as $id ) { if ( $this->does_id_match_object( $id, $object_id, $object_slug ) ) { @@ -915,14 +1028,14 @@ function does_id_array_match_object( $id_array, $object_type, $object_id, $objec return $match; } } else { - // 123 || 'postname' || 'username' || 'comment-name'(?) + // 123 || 'postname' || 'username' || 'comment-name'(?). return $this->does_id_match_object( $id_array, $object_id, $object_slug ); } } function does_id_match_object( $id, $object_id, $object_slug = '' ) { if ( is_int( $id ) ) { - // 123 + // 123. return $id == $object_id; } elseif ( is_string( $id ) ) { // 'postname' || 'username' || 'comment-name' ?? @@ -932,25 +1045,25 @@ function does_id_match_object( $id, $object_id, $object_slug = '' ) { } function is_restricted_field( $field_slug, $object_type ) { - // TODO: Build this out + // TODO: Build this out. $post_restricted = array( 'post_title', 'post_author' ); - $page_restricted = array( ); - $user_restricted = array( ); + $page_restricted = array(); + $user_restricted = array(); switch ( $object_type ) { - case 'user': - return in_array( $field_slug, $user_restricted ); - case 'page': - return in_array( $field_slug, $page_restricted ) || in_array( $field_slug, $post_restricted ); - case 'post': - default: - return in_array( $field_slug, $post_restricted ); + case 'user': + return in_array( $field_slug, $user_restricted ); + case 'page': + return in_array( $field_slug, $page_restricted ) || in_array( $field_slug, $post_restricted ); + case 'post': + default: + return in_array( $field_slug, $post_restricted ); } return false; } function is_restricted_group( $group_slug, $object_type ) { - // TODO: Build this out + // TODO: Build this out. // Built-in metaboxes: title, custom-fields, revisions, author, etc. return false; } @@ -981,8 +1094,9 @@ function _get_object_type_context() { function _get_value_callback( $field, $object_type ) { $callback = isset( $field->value_callback ) ? $field->value_callback : ''; - if ( ! ( $callback && is_callable( $callback ) ) ) + if ( ! ( $callback && is_callable( $callback ) ) ) { $callback = ''; + } return apply_filters( 'custom_metadata_manager_get_value_callback', $callback, $field, $object_type ); } @@ -990,8 +1104,9 @@ function _get_value_callback( $field, $object_type ) { function _get_save_callback( $field, $object_type ) { $callback = isset( $field->save_callback ) ? $field->save_callback : ''; - if ( ! ( $callback && is_callable( $callback ) ) ) + if ( ! ( $callback && is_callable( $callback ) ) ) { $callback = ''; + } return apply_filters( 'custom_metadata_manager_get_save_callback', $callback, $field, $object_type ); } @@ -999,8 +1114,9 @@ function _get_save_callback( $field, $object_type ) { function get_sanitize_callback( $field, $object_type ) { $callback = $field->sanitize_callback; - if ( ! ( $callback && is_callable( $callback ) ) ) + if ( ! ( $callback && is_callable( $callback ) ) ) { $callback = ''; + } return apply_filters( 'custom_metadata_manager_get_sanitize_callback', $callback, $field, $object_type ); } @@ -1008,21 +1124,23 @@ function get_sanitize_callback( $field, $object_type ) { function get_display_column_callback( $field, $object_type ) { $callback = $field->display_column_callback; - if ( ! ( $callback && is_callable( $callback ) ) ) + if ( ! ( $callback && is_callable( $callback ) ) ) { $callback = ''; + } return apply_filters( 'custom_metadata_manager_get_display_column_callback', $callback, $field, $object_type ); } function _get_field_value( $field_slug, $field, $object_type, $object_id, $single = false ) { - $get_value_callback = $this->_get_value_callback( $field, $object_type ); - if ( $get_value_callback ) + if ( $get_value_callback ) { return call_user_func( $get_value_callback, $object_type, $object_id, $field_slug ); + } - if ( !in_array( $object_type, $this->_non_post_types ) ) + if ( ! in_array( $object_type, $this->_non_post_types ) ) { $object_type = 'post'; + } $value = get_metadata( $object_type, $object_id, $field_slug, $single ); @@ -1030,42 +1148,44 @@ function _get_field_value( $field_slug, $field, $object_type, $object_id, $singl } function _save_field_value( $field_slug, $field, $object_type, $object_id, $value ) { - $save_callback = $this->_get_save_callback( $field, $object_type ); - if ( $save_callback ) + if ( $save_callback ) { return call_user_func( $save_callback, $object_type, $object_id, $field_slug, $value ); + } - if ( ! in_array( $object_type, $this->_non_post_types ) ) + if ( ! in_array( $object_type, $this->_non_post_types ) ) { $object_type = 'post'; + } $field_slug = sanitize_key( $field_slug ); - // save the taxonomy as a taxonomy [as well as a custom field] - if ( in_array( $field->field_type, $this->_taxonomy_fields ) && !in_array( $object_type, $this->_non_post_types ) ) { + // save the taxonomy as a taxonomy [as well as a custom field]. + if ( in_array( $field->field_type, $this->_taxonomy_fields ) && ! in_array( $object_type, $this->_non_post_types ) ) { wp_set_object_terms( $object_id, $value, $field->taxonomy ); } if ( is_array( $value ) ) { - // multiple values - delete_metadata( $object_type, $object_id, $field_slug ); // delete the old values and add the new ones + // multiple values. + delete_metadata( $object_type, $object_id, $field_slug ); // delete the old values and add the new ones. foreach ( $value as $v ) { add_metadata( $object_type, $object_id, $field_slug, $v, false ); } } else { - // single value + // single value. update_metadata( $object_type, $object_id, $field_slug, $value ); } - // delete metadata entries if empty + // delete metadata entries if empty. if ( empty( $value ) ) { delete_metadata( $object_type, $object_id, $field_slug ); } } function _delete_field_value( $field_slug, $field, $object_type, $object_id, $value = false ) { - if ( ! in_array( $object_type, $this->_non_post_types ) ) + if ( ! in_array( $object_type, $this->_non_post_types ) ) { $object_type = 'post'; + } $field_slug = sanitize_key( $field_slug ); @@ -1077,13 +1197,14 @@ function _sanitize_field_value( $field_slug, $field, $object_type, $object_id, $ $sanitize_callback = $this->get_sanitize_callback( $field, $object_type ); - // convert date to unix timestamp + // convert date to unix timestamp. if ( in_array( $field->field_type, array( 'datepicker', 'datetimepicker', 'timepicker' ) ) ) { $new_value = strtotime( $original_value ); } - if ( $sanitize_callback ) + if ( $sanitize_callback ) { return call_user_func( $sanitize_callback, $field_slug, $field, $object_type, $object_id, $new_value, $original_value ); + } return $new_value; } @@ -1093,11 +1214,13 @@ function _metadata_column_content( $field_slug, $field, $object_type, $object_id $display_column_callback = $this->get_display_column_callback( $field, $object_type ); - if ( $display_column_callback ) + if ( $display_column_callback ) { return call_user_func( $display_column_callback, $field_slug, $field, $object_type, $object_id, $value ); + } - if ( is_array( $value ) ) + if ( is_array( $value ) ) { return implode( ', ', $value ); + } return $value; } @@ -1114,49 +1237,50 @@ function _display_metadata_multifield( $slug, $multifield, $object_type, $object $fields = $this->get_fields_in_multifield( $multifield->group, $slug, $object_type ); - // validate/weed out the fields that can't be part of mulitified + // validate/weed out the fields that can't be part of mulitified. foreach ( $fields as $field_slug => $field ) { if ( ! in_array( $field->field_type, $this->_field_types_that_support_multifield ) ) { - unset( $fields[$field_slug] ); + unset( $fields[ $field_slug ] ); } } - $_values = $this->get_metadata_mulitifield_value( $slug, $multifield, $object_type, $object_id ); - $_values = ( ! empty( $_values ) ) ? $_values : array( array() ); + $_values = $this->get_metadata_mulitifield_value( $slug, $multifield, $object_type, $object_id ); + $_values = ( ! empty( $_values ) ) ? $_values : array( array() ); $grouping_count = 0; foreach ( $_values as $grouping_of_values ) { $grouping_count++; $grouping_id = $slug . '-' . $grouping_count; printf( '
', esc_attr( $grouping_id ) ); - foreach ( $fields as $field_slug => $field ) { - $value = ( isset( $grouping_of_values[$field_slug] ) ) ? $grouping_of_values[$field_slug] : false; - $field_id = $slug . '[' . ( $grouping_count - 1 ) . ']' . '[' . $field_slug . ']'; - $display_field_slug = $field_slug . '-' . $grouping_count; - $this->_display_metadata_field( $display_field_slug, $field, $object_type, $object_id, $field_id, $value ); - } + foreach ( $fields as $field_slug => $field ) { + $value = ( isset( $grouping_of_values[ $field_slug ] ) ) ? $grouping_of_values[ $field_slug ] : false; + $field_id = $slug . '[' . ( $grouping_count - 1 ) . ']' . '[' . $field_slug . ']'; + $display_field_slug = $field_slug . '-' . $grouping_count; + $this->_display_metadata_field( $display_field_slug, $field, $object_type, $object_id, $field_id, $value ); + } echo '
'; - printf( '', __( 'duplicate this set of fields' ) ); + printf( '', esc_attr__( 'duplicate this set of fields', 'custom-metadata' ) ); if ( $grouping_count > 1 ) { - printf( '', __( 'remove this set of fields' ) ); + printf( '', esc_attr__( 'remove this set of fields', 'custom-metadata' ) ); } echo '
'; } echo ''; - } function _display_metadata_field( $field_slug, $field, $object_type, $object_id, $field_id = null, $value = null ) { - // this is a safety to prevent multifields from being displayed as a field - if ( true === $field->multifield ) + // this is a safety to prevent multifields from being displayed as a field. + if ( true === $field->multifield ) { return; + } - if ( null === $value ) + if ( null === $value ) { $value = $this->get_metadata_field_value( $field_slug, $field, $object_type, $object_id ); + } $callback = $field->display_callback; @@ -1165,73 +1289,76 @@ function _display_metadata_field( $field_slug, $field, $object_type, $object_id, return; } - echo '
'; - if ( ! in_array( $object_type, $this->_non_post_types ) ) + echo '