diff --git a/src/modules/author-boxes/assets/js/author-boxes-editor.js b/src/modules/author-boxes/assets/js/author-boxes-editor.js index d0770ace..7795b8ff 100644 --- a/src/modules/author-boxes/assets/js/author-boxes-editor.js +++ b/src/modules/author-boxes/assets/js/author-boxes-editor.js @@ -367,7 +367,9 @@ 'author_recent_posts_order' ]; var name_refresh_trigger = [ - 'name_show' + 'name_show', + 'name_author_categories', + 'name_author_categories_divider' ]; var bio_refresh_trigger = [ 'author_bio_show', @@ -407,6 +409,8 @@ for (field_key in profile_fields) { var field_name = profile_fields[field_key]; profile_refresh_trigger.push('profile_fields_show_' + field_name); + profile_refresh_trigger.push('profile_fields_' + field_name + '_author_categories'); + profile_refresh_trigger.push('profile_fields_' + field_name + '_author_categories_divider'); profile_refresh_trigger.push('profile_fields_' + field_name + '_html_tag'); profile_refresh_trigger.push('profile_fields_' + field_name + '_value_prefix'); profile_refresh_trigger.push('profile_fields_' + field_name + '_display'); diff --git a/src/modules/author-boxes/author-boxes.php b/src/modules/author-boxes/author-boxes.php index 9207b620..81531012 100644 --- a/src/modules/author-boxes/author-boxes.php +++ b/src/modules/author-boxes/author-boxes.php @@ -141,6 +141,7 @@ public function init() add_filter('pp_multiple_authors_author_box_html', [$this, 'filterAuthorBoxHtml'], 9, 2); add_filter('pp_multiple_authors_authors_list_box_html', [$this, 'filterAuthorBoxHtml'], 9, 2); add_filter('bulk_actions-edit-' . self::POST_TYPE_BOXES . '', [$this, 'removeBulkActionEdit'], 11); + add_filter('parent_file', [$this, 'setParentFile']); add_action('wp_ajax_author_boxes_editor_get_preview', ['MultipleAuthorBoxes\AuthorBoxesAjax', 'handle_author_boxes_editor_get_preview']); @@ -150,6 +151,26 @@ public function init() $this->registerPostType(); } + /** + * Set authors menu as parent for post type so menu is shown + * as active when on post type edit screen. + * + * @param string $parent_file + * + * @return string + */ + public function setParentFile($parent_file) + { + global $current_screen; + + // Check if the current screen is the User Code page + if (!empty($current_screen->post_type) && $current_screen->post_type == self::POST_TYPE_BOXES) { + $parent_file = \MA_Multiple_Authors::MENU_SLUG; + } + + return $parent_file; + } + /** * @param $columns * @@ -668,6 +689,12 @@ public function filterAuthorBoxHtml($html, $args) return $html; } + $author_box_post = get_post($author_box_id); + if (empty($author_box_post) || !is_object($author_box_post) || !isset($author_box_post->ID)) { + // revert to default for deleted author box + $author_box_id = $this->getLegacyLayoutAuthorBoxId('boxed'); + } + $editor_data = $this->get_author_boxes_layout_meta_values($author_box_id); if (!is_array($editor_data)) { @@ -889,6 +916,11 @@ public static function get_fields_tabs($post) */ $fields_tabs = apply_filters('authors_boxes_editor_fields_tabs', $fields_tabs, $post); + + if (!Utils::isAuthorsProActive()) { + unset($fields_tabs['author_categories']); + } + return $fields_tabs; } @@ -1231,12 +1263,18 @@ public static function get_rendered_author_boxes_editor_preview($args) { $author_categories_title_html_tag = 'span'; $author_categories_title_prefix = ''; $author_categories_title_suffix = ''; + + $all_author_categories_data = $author_categories_data; + $author_categories = \MA_Author_Categories::get_author_categories(['category_status' => 1]); + if (!empty($author_categories)) { + $author_relations = \MA_Author_Categories::get_author_relations(['post_id' => $current_post_id]); + $admin_preview_arg = $admin_preview || !empty($args['ajax_preview']); + $all_author_categories_data = Post_Editor::group_category_authors($author_categories, $author_relations, $authors, $admin_preview_arg); + } + if (!empty($args['author_categories_group']['value'])) { - $author_categories = \MA_Author_Categories::get_author_categories(['category_status' => 1]); if (!empty($author_categories)) { - $author_relations = \MA_Author_Categories::get_author_relations(['post_id' => $current_post_id]); - $admin_preview_arg = $admin_preview || !empty($args['ajax_preview']); - $author_categories_data = Post_Editor::group_category_authors($author_categories, $author_relations, $authors, $admin_preview_arg); + $author_categories_data = $all_author_categories_data; $author_categories_group_option = !empty($args['author_categories_group_option']['value']) ? $args['author_categories_group_option']['value'] : 'inline'; $author_categories_title_option = !empty($args['author_categories_title_option']['value']) ? $args['author_categories_title_option']['value'] : ''; $author_categories_title_html_tag = !empty($args['author_categories_title_html_tag']['value']) ? $args['author_categories_title_html_tag']['value'] : 'span'; @@ -1316,6 +1354,9 @@ class="authors-select2 authors-user-slug-search" $author_recent_posts = []; endif; + + $current_author_category = \MA_Author_Categories::get_author_category($author, $all_author_categories_data); + //author fields item position $name_row_extra = ''; $bio_row_extra = ''; @@ -1354,6 +1395,29 @@ class="authors-select2 authors-user-slug-search" continue; } + $profile_author_category_content = ''; + if (!empty($args['profile_fields_' . $key . '_author_categories']['value'])) : + $profile_author_categories_divider = !empty($args['profile_fields_' . $key . '_author_categories_divider']['value']) ? $args['profile_fields_' . $key . '_author_categories_divider']['value'] : ''; + + if (!empty($current_author_category)) : + + $profile_author_category_prefix = ''; + $profile_author_category_suffix = ''; + if ($profile_author_categories_divider == 'colon') { + $profile_author_category_prefix = ': '; + } elseif ($profile_author_categories_divider == 'bracket') { + $profile_author_category_prefix = ' ('; + $profile_author_category_suffix = ') '; + } elseif ($profile_author_categories_divider == 'square_bracket') { + $profile_author_category_prefix = ' ['; + $profile_author_category_suffix = '] '; + } + $profile_author_category_content = '' . $profile_author_category_prefix . $current_author_category['singular_title'] . $profile_author_category_suffix . ''; + + endif; + + endif; + $display_field_value = ''; if ($profile_display === 'icon_prefix_value_suffix') { if (!empty($profile_display_icon)) { @@ -1364,6 +1428,7 @@ class="authors-select2 authors-user-slug-search" } if (!empty($field_value)) { $display_field_value .= $field_value . ' '; + $display_field_value .= $profile_author_category_content; } if (!empty($profile_display_suffix)) { $display_field_value .= esc_html($profile_display_suffix); @@ -1441,8 +1506,32 @@ class="authors-select2 authors-user-slug-search" < class="pp-author-boxes-avatar-details"> + ' . $name_author_category_prefix . $current_author_category['singular_title'] . $name_author_category_suffix . ''; + + endif; + + endif; + ?> < class="pp-author-boxes-name multiple-authors-name"> - ' . $author_categories_title_prefix . '' . $author_category_data['singular_title'] . '' . $author_categories_title_suffix . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> 1 && $index !== $author_counts - 1) { + ' . $author_categories_title_prefix . '' . $author_category_data['singular_title'] . '' . $author_categories_title_suffix . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> 1 && $index !== $author_counts - 1) { echo html_entity_decode($author_separator); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } ?> > diff --git a/src/modules/author-boxes/classes/AuthorBoxesEditorFields.php b/src/modules/author-boxes/classes/AuthorBoxesEditorFields.php index d375aeb8..6bd8fa90 100644 --- a/src/modules/author-boxes/classes/AuthorBoxesEditorFields.php +++ b/src/modules/author-boxes/classes/AuthorBoxesEditorFields.php @@ -10,7 +10,7 @@ namespace MultipleAuthorBoxes; -use MultipleAuthors\Classes\Author_Editor; +use MultipleAuthors\Classes\Utils; use MA_Author_Boxes; /** @@ -240,6 +240,24 @@ public static function getNameFields($fields, $post) 'sanitize' => 'absint', 'tab' => 'name', ]; + $fields['name_author_categories'] = [ + 'label' => esc_html__('Show Author Categories', 'publishpress-authors'), + 'type' => 'checkbox', + 'sanitize' => 'absint', + 'tab' => 'name', + ]; + $fields['name_author_categories_divider'] = [ + 'label' => esc_html__('Author Categories Divider', 'publishpress-authors'), + 'type' => 'select', + 'sanitize' => 'sanitize_text_field', + 'options' => [ + 'colon' => esc_html__(':', 'publishpress-authors'), + 'bracket' => esc_html__('()', 'publishpress-authors'), + 'square_bracket' => esc_html__('[]', 'publishpress-authors'), + 'none' => esc_html__('None', 'publishpress-authors'), + ], + 'tab' => 'name', + ]; $fields['name_size'] = [ 'label' => esc_html__('Size', 'publishpress-authors'), 'type' => 'number', @@ -513,6 +531,10 @@ public static function getMetaFields($fields, $post) public static function getAuthorCategories($fields, $post) { + if (!Utils::isAuthorsProActive()) { + return $fields; + } + $author_categories = \MA_Author_Categories::get_author_categories(['category_status' => 1]); if (empty($author_categories)) { @@ -701,6 +723,28 @@ public static function getProfileFields($fields, $post) 'tab_name' => $key, 'tab' => 'profile_fields', ]; + $fields['profile_fields_' . $key . '_author_categories'] = [ + 'label' => esc_html__('Show Author Categories', 'publishpress-authors'), + 'type' => 'checkbox', + 'sanitize' => 'absint', + 'tabbed' => 1, + 'tab_name' => $key, + 'tab' => 'profile_fields', + ]; + $fields['profile_fields_' . $key . '_author_categories_divider'] = [ + 'label' => esc_html__('Author Categories Divider', 'publishpress-authors'), + 'type' => 'select', + 'sanitize' => 'sanitize_text_field', + 'options' => [ + 'colon' => esc_html__(':', 'publishpress-authors'), + 'bracket' => esc_html__('()', 'publishpress-authors'), + 'square_bracket' => esc_html__('[]', 'publishpress-authors'), + 'none' => esc_html__('None', 'publishpress-authors'), + ], + 'tabbed' => 1, + 'tab_name' => $key, + 'tab' => 'profile_fields', + ]; $fields['profile_fields_' . $key . '_display_position'] = [ 'label' => esc_html__('Show After', 'publishpress-authors'), 'type' => 'select', diff --git a/src/modules/author-boxes/classes/AuthorBoxesStyles.php b/src/modules/author-boxes/classes/AuthorBoxesStyles.php index edfc7450..69d7e2f2 100644 --- a/src/modules/author-boxes/classes/AuthorBoxesStyles.php +++ b/src/modules/author-boxes/classes/AuthorBoxesStyles.php @@ -10,7 +10,7 @@ namespace MultipleAuthorBoxes; -use MultipleAuthors\Classes\Author_Editor; +use MultipleAuthors\Classes\Utils; use MA_Author_Boxes; /** @@ -218,6 +218,10 @@ public static function getMetaFieldStyles($args, $custom_styles) { */ public static function getAuthorCategoriesFieldStyles($args, $custom_styles) { + if (!Utils::isAuthorsProActive()) { + return $custom_styles; + } + if (!empty($args['author_categories_group_display_style_laptop']['value'])) { $custom_styles .= '.pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap { display: '. $args['author_categories_group_display_style_laptop']['value'] .' !important; } '; if ($args['author_categories_group_display_style_laptop']['value'] == 'flex') { @@ -230,16 +234,16 @@ public static function getAuthorCategoriesFieldStyles($args, $custom_styles) { $custom_styles .= ' @media screen and (max-width: 768px) { .pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap .ppma-category-group { flex: 1; } } '; } } - if ($args['author_categories_bottom_space']['value']) { + if (isset($args['author_categories_bottom_space']['value'])) { $custom_styles .= '.pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap .ppma-category-group { margin-bottom: '. $args['author_categories_bottom_space']['value'] .'px !important; } '; } - if ($args['author_categories_right_space']['value']) { + if (isset($args['author_categories_right_space']['value'])) { $custom_styles .= '.pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap .ppma-category-group { margin-right: '. $args['author_categories_right_space']['value'] .'px !important; } '; } - if ($args['author_categories_font_size']['value']) { + if (isset($args['author_categories_font_size']['value'])) { $custom_styles .= '.pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap { font-size: '. $args['author_categories_font_size']['value'] .'px !important; } '; } - if ($args['author_categories_title_font_weight']['value']) { + if (isset($args['author_categories_title_font_weight']['value'])) { $custom_styles .= '.pp-multiple-authors-boxes-wrapper.box-post-id-'.$args['post_id'].'.'.$args['additional_class'].'.box-instance-id-'.$args['instance_id'].' .ppma-author-category-wrap .ppma-category-group-title { font-weight: '. $args['author_categories_title_font_weight']['value'] .' !important; } '; } diff --git a/src/modules/author-categories/author-categories.php b/src/modules/author-categories/author-categories.php index fa72e604..98fb6933 100644 --- a/src/modules/author-categories/author-categories.php +++ b/src/modules/author-categories/author-categories.php @@ -631,7 +631,7 @@ public static function get_author_relations($args = []) { $sql = "SELECT * FROM $table_name WHERE 1=1"; - if (!empty($post_id)) { + if ($post_id !== '') { $sql .= $wpdb->prepare(" AND post_id = %d", $post_id); } @@ -644,6 +644,31 @@ public static function get_author_relations($args = []) { return $results; } + /** + * Get author category + * + * @param object $author + * @param array $author_categories_data + * + * @return array + */ + public static function get_author_category($author, $author_categories_data) { + + $author_category = []; + + foreach ($author_categories_data as $author_category_data) { + if (!empty($author_category_data['singular_title']) && !empty($author_category_data['authors'])) { + $author_term_id = array_column($author_category_data['authors'], 'term_id'); + if (in_array($author->term_id, $author_term_id)) { + $author_category = $author_category_data; + } + } + + } + + return $author_category; + } + /** diff --git a/src/modules/author-custom-fields/author-custom-fields.php b/src/modules/author-custom-fields/author-custom-fields.php index 1ee85394..72a77915 100644 --- a/src/modules/author-custom-fields/author-custom-fields.php +++ b/src/modules/author-custom-fields/author-custom-fields.php @@ -126,10 +126,31 @@ public function init() add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); add_action('wp_ajax_author_custom_fields_save_order', [$this, 'handle_ajax_update_field_order']); add_action('pre_get_posts', [$this, 'author_custom_fields_default_sort']); + add_filter('parent_file', [$this, 'setParentFile']); $this->registerPostType(); } + /** + * Set authors menu as parent for post type so menu is shown + * as active when on post type edit screen. + * + * @param string $parent_file + * + * @return string + */ + public function setParentFile($parent_file) + { + global $current_screen; + + // Check if the current screen is the User Code page + if (!empty($current_screen->post_type) && $current_screen->post_type == self::POST_TYPE_CUSTOM_FIELDS) { + $parent_file = \MA_Multiple_Authors::MENU_SLUG; + } + + return $parent_file; + } + /** * Register the post types. */