Пример #1
0
 /**
  * Checks whether all requirements to save meta values for a term are met.
  *
  * @since 0.6.0
  * @param integer $term_id the term ID to save
  * @param WP_Term $term the term object
  * @return bool whether the meta values can be saved
  */
 protected function can_save_meta($term_id, $term)
 {
     if (!wpptd_supports_termmeta()) {
         return false;
     }
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return false;
     }
     if (wpptd_get_taxonomy($term_id) != $this->slug) {
         return false;
     }
     $taxonomy_obj = get_taxonomy($this->slug);
     if (!current_user_can($taxonomy_obj->cap->edit_terms)) {
         return false;
     }
     return true;
 }
Пример #2
0
 /**
  * Hooks in all functions related to a taxonomy.
  *
  * @since 0.5.0
  */
 protected function add_taxonomy_hooks()
 {
     if (0 > version_compare(get_bloginfo('version'), '4.5')) {
         add_action('load-edit-tags.php', array($this, 'add_term_or_term_list_help'));
     } else {
         add_action('load-edit-tags.php', array($this, 'add_term_list_help'));
         add_action('load-term.php', array($this, 'add_term_help'));
     }
     add_filter('term_updated_messages', array($this, 'get_term_updated_messages'));
     if (wpptd_supports_termmeta()) {
         $taxonomies = ComponentManager::get('*.*.*', 'WPDLib\\Components\\Menu.WPPTD\\Components\\PostType.WPPTD\\Components\\Taxonomy');
         $edit_form_top_hook_suffix = '_term_edit_form_top';
         $edit_form_top_method_suffix = '';
         if (0 > version_compare(get_bloginfo('version'), '4.5')) {
             // so hacky (luckily only in WordPress < 4.5)
             $edit_form_top_hook_suffix = '_term_edit_form_tag';
             $edit_form_top_method_suffix = '_hack';
         }
         foreach ($taxonomies as $taxonomy) {
             add_action($taxonomy->slug . $edit_form_top_hook_suffix, array($this, 'display_term_meta_errors' . $edit_form_top_method_suffix), 9998);
             add_action($taxonomy->slug . $edit_form_top_hook_suffix, array($this, 'wrap_term_ui_top' . $edit_form_top_method_suffix), 9999);
             add_action($taxonomy->slug . '_edit_form', array($this, 'wrap_term_ui_bottom'), 9999, 2);
         }
         add_action('load-edit-tags.php', array($this, 'initialize_term_ui'));
         add_action('edit_term', array($this, 'save_term_meta'), 10, 3);
     }
 }
Пример #3
0
 /**
  * This function is a low-level function to get related objects for another specific object.
  *
  * The base object can either be a post or a term (specified by an ID) while the related objects
  * can either be posts, terms or users.
  *
  * The function looks through the registered meta fields of the base object that possibly
  * contain related objects. It is also possible to only return related objects that are stored
  * in a specific meta field or that have a specific type (that is post type, taxonomy or user role).
  *
  * @see wpptd_get_post_related_posts()
  * @see wpptd_get_post_related_terms()
  * @see wpptd_get_post_related_users()
  * @see wpptd_get_term_related_posts()
  * @see wpptd_get_term_related_terms()
  * @see wpptd_get_term_related_users()
  * @since 0.6.0
  * @param string $mode mode of the ID specified (for the base object), either 'post' or 'term'
  * @param integer $id a post ID or a term ID (depending the $mode parameter)
  * @param string $objects_mode mode of the related objects to find, either 'posts', 'terms' or 'users'
  * @param string $meta_key an optional meta key to only return objects that are stored in a meta field of that name (default is empty)
  * @param string $object_type an optional type to only return objects of that type, either a specific post type, taxonomy or user role depending on the $objects_mode parameter
  * @param boolean $single whether to only return a single object (default is false)
  * @return WP_Post|WP_Term|WP_User|array|null either an object or null (if $single is true) or an array of objects or empty array otherwise
  */
 public static function get_related_objects($mode, $id, $objects_mode, $meta_key = '', $object_type = '', $single = false)
 {
     // specify variables depending on the base mode
     switch ($mode) {
         case 'post':
             $get_func = 'get_post';
             $type_field = 'post_type';
             $component_path = '*.[TYPE]';
             $class_path = 'WPDLib\\Components\\Menu.WPPTD\\Components\\PostType';
             $meta_func = 'wpptd_get_post_meta_value';
             break;
         case 'term':
             if (!wpptd_supports_termmeta()) {
                 if ($single) {
                     return null;
                 }
                 return array();
             }
             $get_func = 'get_term';
             $type_field = 'taxonomy';
             $component_path = '*.*.[TYPE]';
             $class_path = 'WPDLib\\Components\\Menu.WPPTD\\Components\\PostType.WPPTD\\Components\\Taxonomy';
             $meta_func = 'wpptd_get_term_meta_value';
             break;
         default:
             if ($single) {
                 return null;
             }
             return array();
     }
     // specify variables depending on the objects mode
     switch ($objects_mode) {
         case 'posts':
             $objects_related_field = 'related_posts_fields';
             $objects_get_func = 'get_post';
             $objects_type_field = 'post_type';
             break;
         case 'terms':
             $objects_related_field = 'related_terms_fields';
             $objects_get_func = 'get_term';
             $objects_type_field = 'taxonomy';
             break;
         case 'users':
             $objects_related_field = 'related_users_fields';
             $objects_get_func = 'get_user_by';
             $objects_get_func_first_param = 'id';
             $objects_type_field = 'roles';
             break;
         default:
             if ($single) {
                 return null;
             }
             return array();
     }
     // get the base object
     $obj = call_user_func($get_func, $id);
     if (!$obj) {
         if ($single) {
             return null;
         }
         return array();
     }
     // get the component for the base object
     $component = ComponentManager::get(str_replace('[TYPE]', $obj->{$type_field}, $component_path), $class_path, true);
     if (!$component) {
         if ($single) {
             return null;
         }
         return array();
     }
     // get the necessary meta fields depending on the $meta_key and $object_type parameter
     $all_fields = $component->{$objects_related_field};
     $fields = array();
     if (!empty($meta_key) && !empty($object_type)) {
         if (isset($all_fields[$meta_key]) && (0 === count($all_fields[$meta_key]) || in_array($object_type, $all_fields[$meta_key], true))) {
             $fields[] = $meta_key;
         }
     } elseif (!empty($meta_key)) {
         if (isset($all_fields[$meta_key])) {
             $fields[] = $meta_key;
         }
     } elseif (!empty($object_type)) {
         foreach ($all_fields as $field_name => $types) {
             if (0 === count($types) || in_array($object_type, $types, true)) {
                 $fields[] = $field_name;
             }
         }
     } else {
         $fields = array_keys($all_fields);
     }
     if (0 === count($fields)) {
         if ($single) {
             return null;
         }
         return array();
     }
     // get the IDs of the related objects
     $object_ids = array();
     foreach ($fields as $field) {
         $val = call_user_func($meta_func, $id, $field);
         if (!$val) {
             continue;
         } elseif (is_array($val)) {
             $object_ids = array_merge($object_ids, $val);
         } else {
             $object_ids[] = $val;
         }
     }
     $object_ids = array_filter(array_map('absint', $object_ids));
     if (0 === count($object_ids)) {
         if ($single) {
             return null;
         }
         return array();
     }
     // get the related objects, also considering the $object_type variable if applicable
     $objects = array();
     foreach ($object_ids as $object_id) {
         if (isset($objects_get_func_first_param)) {
             $object = call_user_func($objects_get_func, $objects_get_func_first_param, $object_id);
         } else {
             $object = call_user_func($objects_get_func, $object_id);
         }
         if (!$object) {
             continue;
         }
         if (!empty($object_type)) {
             $type = $object->{$objects_type_field};
             if (is_array($type) && !in_array($object_type, $type, true)) {
                 continue;
             } elseif (!is_array($type) && $object_type !== $type) {
                 continue;
             }
         }
         $objects[] = $object;
     }
     if (0 === count($objects)) {
         if ($single) {
             return null;
         }
         return array();
     }
     if ($single) {
         return $objects[0];
     }
     return $objects;
 }
 /**
  * Returns a single specified meta value for a term.
  *
  * This function is basically a wrapper for the WordPress core function `get_term_meta()`
  * when calling it with specification of a meta key. If the required field meta value is not available,
  * the function will automatically return its default value.
  *
  * Furthermore the $single parameter can be used to force an array or no array to be returned for the
  * meta field. You should generally leave it set to null.
  *
  * @since 0.6.0
  * @param integer $id the term ID to get the meta value for
  * @param string $meta_key the meta key (field slug) to get the meta value for
  * @param null|boolean $single whether to force an array or no array being returned (default is not to force anything)
  * @param boolean $formatted whether to return an automatically formatted value, ready for output (default is false)
  * @return mixed the meta value
  */
 function wpptd_get_term_meta_value($id, $meta_key, $single = null, $formatted = false)
 {
     if (!wpptd_supports_termmeta()) {
         if ($single) {
             return null;
         }
         return array();
     }
     $_meta_value = get_term_meta($id, $meta_key, false);
     if (doing_action('wpptd') || !did_action('wpptd')) {
         if ($single) {
             if (count($_meta_value) > 0) {
                 return $_meta_value[0];
             }
             return null;
         } else {
             return $_meta_value;
         }
     }
     $meta_value = null;
     $field = \WPDLib\Components\Manager::get('*.*.' . wpptd_get_taxonomy($id) . '.*.' . $meta_key, 'WPDLib\\Components\\Menu.WPPTD\\Components\\PostType.WPPTD\\Components\\Taxonomy.WPPTD\\Components\\TermMetabox', true);
     if ($field) {
         $meta_value = \WPPTD\Utility::parse_meta_value($_meta_value, $field, $single, $formatted);
     }
     return $meta_value;
 }
Пример #5
0
 /**
  * Adds taxonomies.
  *
  * @internal
  * @since 0.5.0
  * @param array $taxonomies the taxonomies to add as $taxonomy_slug => $taxonomy_args
  * @param WPPTD\Components\PostType $post_type the post type to add the taxonomies to
  */
 protected function add_taxonomies($taxonomies, $post_type)
 {
     foreach ($taxonomies as $taxonomy_slug => $taxonomy_args) {
         if (is_array($taxonomy_args)) {
             $taxonomy = $post_type->add(new Taxonomy($taxonomy_slug, $taxonomy_args));
             if (is_wp_error($taxonomy)) {
                 self::doing_it_wrong(__METHOD__, $taxonomy->get_error_message(), '0.5.0');
             } else {
                 if (wpptd_supports_termmeta()) {
                     if (isset($taxonomy_args['metaboxes']) && is_array($taxonomy_args['metaboxes'])) {
                         $this->add_term_metaboxes($taxonomy_args['metaboxes'], $taxonomy);
                     }
                 }
                 if (isset($this->taxonomies_temp[$taxonomy_slug]) && is_array($this->taxonomies_temp[$taxonomy_slug])) {
                     foreach ($this->taxonomies_temp[$taxonomy_slug] as $_post_type) {
                         $_post_type->add($taxonomy);
                     }
                 }
                 $this->taxonomies_temp[$taxonomy_slug] = $taxonomy;
             }
         } else {
             if (isset($this->taxonomies_temp[$taxonomy_slug]) && is_object($this->taxonomies_temp[$taxonomy_slug])) {
                 $taxonomy = $post_type->add($this->taxonomies_temp[$taxonomy_slug]);
             } else {
                 if (!isset($this->taxonomies_temp[$taxonomy_slug])) {
                     $this->taxonomies_temp[$taxonomy_slug] = array();
                 }
                 $this->taxonomies_temp[$taxonomy_slug][] = $post_type;
             }
         }
     }
 }