From afba9484e1fd16d6c9bdbd7002d50961296ebd35 Mon Sep 17 00:00:00 2001 From: zEvilz Date: Sun, 3 Jun 2018 16:32:21 +0500 Subject: [PATCH 1/3] Added support for post type archives in association field --- core/Field/Association_Field.php | 88 +++++++++++++++++++++++++------- 1 file changed, 69 insertions(+), 19 deletions(-) diff --git a/core/Field/Association_Field.php b/core/Field/Association_Field.php index df3f4f43b..b2364de45 100644 --- a/core/Field/Association_Field.php +++ b/core/Field/Association_Field.php @@ -138,7 +138,7 @@ protected function value_string_to_property_array( $value_string ) { Value_Set::VALUE_PROPERTY => $value_string, 'type' => $type, 'subtype' => $subtype, - 'id' => intval( $id ), + 'id' => ( $type == 'post_type_archive' ) ? $id : intval( $id ), ); return $property_array; } @@ -179,7 +179,7 @@ protected function value_string_array_to_value_set( $value_string_array ) { * * Can be overriden or extended by the `carbon_association_title` filter. * - * @param int $id The database ID of the item. + * @param int|string $id The database ID of the item or post type name if type is "post_type_archive". * @param string $type Item type (post, term, user, comment, or a custom one). * @param string $subtype The subtype - "page", "post", "category", etc. * @return string $title The title of the item. @@ -200,16 +200,25 @@ protected function get_title_by_type( $id, $type, $subtype = '' ) { } } + if ( $type === 'post_type_archive' ) { + $post_type_object = get_post_type_object($id); + $title = $post_type_object->label; + } + /** * Filter the title of the association item. * - * @param string $title The unfiltered item title. - * @param string $name Name of the association field. - * @param int $id The database ID of the item. - * @param string $type Item type (post, term, user, comment, or a custom one). - * @param string $subtype Subtype - "page", "post", "category", etc. + * @param string $title The unfiltered item title. + * @param string $name Name of the association field. + * @param int|string $id The database ID of the item or post type name if type is "post_type_archive". + * @param string $type Item type (post, term, user, comment, or a custom one). + * @param string $subtype Subtype - "page", "post", "category", etc. */ - $title = apply_filters( 'carbon_fields_association_field_title', $title, $this->get_base_name(), $id, $type, $subtype ); + if ( $type == 'post_type_archive' ) { + $title = apply_filters( 'carbon_fields_association_field_title', $title, $type, $id, $type, $subtype ); + } else { + $title = apply_filters( 'carbon_fields_association_field_title', $title, $this->get_base_name(), $id, $type, $subtype ); + } if ( ! $title ) { $title = '(no title) - ID: ' . $id; @@ -223,9 +232,9 @@ protected function get_title_by_type( $id, $type, $subtype = '' ) { * * Can be overriden or extended by the `carbon_association_item_label` filter. * - * @param int $id The database ID of the item. - * @param string $type Item type (post, term, user, comment, or a custom one). - * @param string $subtype Subtype - "page", "post", "category", etc. + * @param int|string $id The database ID of the item or post type name if type is "post_type_archive". + * @param string $type Item type (post, term, user, comment, or a custom one). + * @param string $subtype Subtype - "page", "post", "category", etc. * @return string $label The label of the item. */ protected function get_item_label( $id, $type, $subtype = '' ) { @@ -237,18 +246,24 @@ protected function get_item_label( $id, $type, $subtype = '' ) { } elseif ( $type === 'term' ) { $taxonomy_object = get_taxonomy( $subtype ); $label = $taxonomy_object->labels->singular_name; + } elseif ( $type === 'post_type_archive' ) { + $label = __('Post Type Archive'); } /** * Filter the label of the association item. * - * @param string $label The unfiltered item label. - * @param string $name Name of the association field. - * @param int $id The database ID of the item. - * @param string $type Item type (post, term, user, comment, or a custom one). - * @param string $subtype Subtype - "page", "post", "category", etc. + * @param string $label The unfiltered item label. + * @param string $name Name of the association field. + * @param int|string $id The database ID of the item or post type name if type is "post_type_archive". + * @param string $type Item type (post, term, user, comment, or a custom one). + * @param string $subtype Subtype - "page", "post", "category", etc. */ - return apply_filters( 'carbon_fields_association_field_item_label', $label, $this->get_base_name(), $id, $type, $subtype ); + if ( $type == 'post_type_archive' ) { + return apply_filters( 'carbon_fields_association_field_item_label', $label, $type, $id, $type, $subtype ); + } else { + return apply_filters( 'carbon_fields_association_field_item_label', $label, $this->get_base_name(), $id, $type, $subtype ); + } } /** @@ -286,6 +301,41 @@ protected function get_post_options( $type ) { return $posts; } + /** + * Get post type archive options + * + * @return array $options + */ + protected function get_post_type_archive_options( $type ) { + /** + * Filter the default query when fetching post types for a particular field. + * + * @param array $args The parameters, passed to get_post_types(). + */ + $filter_name = 'carbon_fields_association_field_options_' . $this->get_base_name() . '_' . $type['type']; + $args = apply_filters( $filter_name, array( + 'public' => true, + 'has_archive' => true, + ) ); + + // fetch and prepare post types archives as association items + $post_types = get_post_types($args, 'names'); + foreach ( $post_types as &$p ) { + $post_type_object = get_post_type_object($p); + $p = array( + 'id' => $post_type_object->name, + 'title' => $this->get_title_by_type( $p, $type['type'] ), + 'type' => $type['type'], + 'subtype' => 'post_type_archive', + 'label' => $this->get_item_label( $p, $type['type'] ), + 'is_trashed' => false, + 'edit_link' => false, + ); + } + $post_type_archives = array_values($post_types); + return $post_type_archives; + } + /** * Get term options * @@ -538,7 +588,7 @@ public function allow_duplicates( $allow = true ) { * where the value of each array item contains: * - Type of data (post, term, user or comment) * - Subtype of data (the particular post type or taxonomy) - * - ID of the item (the database ID of the item) + * - ID of the item (the database ID of the item) or post type name if type is "post_type_archive" */ protected function value_to_json() { $value_set = $this->get_value(); @@ -547,7 +597,7 @@ protected function value_to_json() { $item = array( 'type' => $entry['type'], 'subtype' => $entry['subtype'], - 'id' => intval( $entry['id'] ), + 'id' => ( $entry['type'] == 'post_type_archive' ) ? $entry['id'] : intval( $entry['id'] ), 'title' => $this->get_title_by_type( $entry['id'], $entry['type'], $entry['subtype'] ), 'label' => $this->get_item_label( $entry['id'], $entry['type'], $entry['subtype'] ), 'is_trashed' => ( $entry['type'] == 'post' && get_post_status( $entry['id'] ) === 'trash' ), From 5b4ef602eee97c83a3dc7667f84a1fb5c105ac16 Mon Sep 17 00:00:00 2001 From: zEvilz Date: Sun, 3 Jun 2018 16:56:24 +0500 Subject: [PATCH 2/3] updated --- core/Field/Association_Field.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/Field/Association_Field.php b/core/Field/Association_Field.php index b2364de45..08ba33ffd 100644 --- a/core/Field/Association_Field.php +++ b/core/Field/Association_Field.php @@ -321,9 +321,9 @@ protected function get_post_type_archive_options( $type ) { // fetch and prepare post types archives as association items $post_types = get_post_types($args, 'names'); foreach ( $post_types as &$p ) { - $post_type_object = get_post_type_object($p); + $post_type = $p; $p = array( - 'id' => $post_type_object->name, + 'id' => $post_type, 'title' => $this->get_title_by_type( $p, $type['type'] ), 'type' => $type['type'], 'subtype' => 'post_type_archive', From 0b364eb34bfbdee6599962ab66f5e0e71312eb22 Mon Sep 17 00:00:00 2001 From: zEvilz Date: Sat, 9 Jun 2018 14:32:45 +0500 Subject: [PATCH 3/3] fixed post type archive hooks names --- core/Field/Association_Field.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/core/Field/Association_Field.php b/core/Field/Association_Field.php index 08ba33ffd..4609a84cb 100644 --- a/core/Field/Association_Field.php +++ b/core/Field/Association_Field.php @@ -214,11 +214,7 @@ protected function get_title_by_type( $id, $type, $subtype = '' ) { * @param string $type Item type (post, term, user, comment, or a custom one). * @param string $subtype Subtype - "page", "post", "category", etc. */ - if ( $type == 'post_type_archive' ) { - $title = apply_filters( 'carbon_fields_association_field_title', $title, $type, $id, $type, $subtype ); - } else { - $title = apply_filters( 'carbon_fields_association_field_title', $title, $this->get_base_name(), $id, $type, $subtype ); - } + $title = apply_filters( 'carbon_fields_association_field_title', $title, $this->get_base_name(), $id, $type, $subtype ); if ( ! $title ) { $title = '(no title) - ID: ' . $id; @@ -259,11 +255,7 @@ protected function get_item_label( $id, $type, $subtype = '' ) { * @param string $type Item type (post, term, user, comment, or a custom one). * @param string $subtype Subtype - "page", "post", "category", etc. */ - if ( $type == 'post_type_archive' ) { - return apply_filters( 'carbon_fields_association_field_item_label', $label, $type, $id, $type, $subtype ); - } else { - return apply_filters( 'carbon_fields_association_field_item_label', $label, $this->get_base_name(), $id, $type, $subtype ); - } + return apply_filters( 'carbon_fields_association_field_item_label', $label, $this->get_base_name(), $id, $type, $subtype ); } /**