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; ?>