From 5c279533920bf120eff91196bff7a0d4b01c6496 Mon Sep 17 00:00:00 2001 From: Kailey Lampert Date: Thu, 24 Jun 2021 16:55:26 -0700 Subject: [PATCH 1/4] phpcbf for custom_metadata.php --- custom_metadata.php | 545 ++++++++++++++++++++++++-------------------- 1 file changed, 298 insertions(+), 247 deletions(-) diff --git a/custom_metadata.php b/custom_metadata.php index 2c6a7c1..49e86ad 100644 --- a/custom_metadata.php +++ b/custom_metadata.php @@ -30,11 +30,13 @@ /** * 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'; +} class custom_metadata_manager { @@ -51,7 +53,7 @@ class custom_metadata_manager { 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' ); + 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 $_cloneable_field_types = array( 'text', 'textarea', 'upload', 'password', 'number', 'email', 'tel' ); @@ -91,10 +93,11 @@ class custom_metadata_manager { private static $instance; 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; } @@ -110,24 +113,24 @@ 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 ); + $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 ); + $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(); @@ -137,16 +140,18 @@ function admin_init() { } // Hook into admin_notices to show errors - if ( current_user_can( 'manage_options' ) ) + 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() { @@ -161,8 +166,9 @@ function init_metadata() { 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 add_action( 'edit_user_profile', array( $this, 'add_user_metadata_groups' ) ); @@ -170,11 +176,10 @@ function init_metadata() { // 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" ) ); + add_action( 'add_meta_boxes', array( $this, 'add_post_metadata_groups' ) ); // Hook in to save add_action( 'save_post', array( $this, 'save_post_metadata' ) ); @@ -187,15 +192,14 @@ 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 @@ -207,15 +211,16 @@ function init_columns() { // 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 ); - + } } function enqueue_scripts() { @@ -223,23 +228,22 @@ function enqueue_scripts() { 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_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' ); } 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 +251,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 +263,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,43 +283,44 @@ 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_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 + '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 ) ) + 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 if ( isset( $args['chosen'] ) ) { @@ -332,64 +337,64 @@ function add_metadata_field( $field_slug, $object_types = array( 'post' ), $args $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 ) ) + 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 { + // $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 ); -// } - + // } } 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 + '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 = wp_parse_args( $args, $defaults ); $multifield['multifield'] = true; // force it - $multifield = (object) $multifield; + $multifield = (object) $multifield; // Sanitize slug - $slug = sanitize_key( $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 ) ) + 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 $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? + '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 ); @@ -403,11 +408,13 @@ function add_metadata_group( $group_slug, $object_types, $args = array() ) { $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 ) ) + 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 ); } @@ -523,7 +530,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 +538,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 +577,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,15 +605,14 @@ 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 { @@ -605,7 +624,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 ) ) { @@ -619,8 +637,9 @@ function _display_post_metadata_box( $object, $meta_box ) { } 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 +649,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 +662,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 +674,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 ) + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE && ! $group->autosave ) { return $post_id; + } $this->save_metadata_group( $group_slug, $group, $post_type, $post_id ); } @@ -667,7 +688,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 +696,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 +709,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 +730,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'] ) ); + 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'] ) ) + 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 +775,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 +783,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 +816,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 +827,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 +869,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 +881,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 +907,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,13 +926,13 @@ 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] ) ) { + 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 ); + 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 ); + return $this->does_id_match_object( $id_array[ $object_type ], $object_id, $object_slug ); } } else { // array( 123, 456, 'postname' ) @@ -934,17 +965,17 @@ function does_id_match_object( $id, $object_id, $object_slug = '' ) { function is_restricted_field( $field_slug, $object_type ) { // 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; } @@ -981,8 +1012,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 +1022,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 +1032,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 +1042,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,19 +1066,20 @@ 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 ) ) { + 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 ); } @@ -1064,8 +1101,9 @@ function _save_field_value( $field_slug, $field, $object_type, $object_id, $valu } 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 ); @@ -1082,8 +1120,9 @@ function _sanitize_field_value( $field_slug, $field, $object_type, $object_id, $ $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 +1132,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; } @@ -1117,24 +1158,24 @@ function _display_metadata_multifield( $slug, $multifield, $object_type, $object // 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' ) ); @@ -1146,17 +1187,18 @@ function _display_metadata_multifield( $slug, $multifield, $object_type, $object } 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 ) + 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,11 +1207,12 @@ function _display_metadata_field( $field_slug, $field, $object_type, $object_id, return; } - echo '
'; - if ( ! in_array( $object_type, $this->_non_post_types ) ) + echo '