From 1c74652b80e2a3304afa8946607988ad845ff2eb Mon Sep 17 00:00:00 2001 From: Richa Ferry Setyawan <49858604+richaferry@users.noreply.github.com> Date: Sat, 21 Sep 2024 02:50:59 +0700 Subject: [PATCH] feature/#639_ACF_Integration_Support (#762) --- core/Legacy/Module.php | 47 +-- core/Legacy/Util.php | 31 +- modules/checklists/assets/css/admin-pages.css | 9 +- .../checklists/assets/js/global-checklists.js | 27 +- modules/checklists/checklists.php | 51 +-- .../templates/global-checklists.php | 166 +++++----- modules/settings/settings.php | 297 +++++++++--------- 7 files changed, 333 insertions(+), 295 deletions(-) diff --git a/core/Legacy/Module.php b/core/Legacy/Module.php index 1fe48442..6762df00 100644 --- a/core/Legacy/Module.php +++ b/core/Legacy/Module.php @@ -1,4 +1,5 @@ @@ -53,9 +54,9 @@ public function clearPostTypesOptions($modulePostTypes = [], $postTypeSupport = $allPostTypes = array_keys($this->getAllPostTypes()); foreach ($allPostTypes as $postType) { if ((isset($modulePostTypes[$postType]) && $modulePostTypes[$postType] == 'on') || post_type_supports( - $postType, - $postTypeSupport - )) { + $postType, + $postTypeSupport + )) { $normalizedPostTypeOptions[$postType] = 'on'; } else { $normalizedPostTypeOptions[$postType] = 'off'; @@ -222,8 +223,8 @@ public function printDefaultHeader($current_module, $custom_text = null) if ($message && isset($current_module->messages[$message])) { $display_text .= '

' . esc_html( - $current_module->messages[$message] - ) . '

'; + $current_module->messages[$message] + ) . '

'; } // If there's been an error, let's display it @@ -238,31 +239,31 @@ public function printDefaultHeader($current_module, $custom_text = null) } if ($error && isset($current_module->messages[$error])) { $display_text .= '

' . esc_html( - $current_module->messages[$error] - ) . '

'; + $current_module->messages[$error] + ) . '

'; } - ?> +?>
-
-

title); ?>

+
+

title); ?>

- - -

+ + +

- short_description && empty($custom_text)): ?> - short_description); ?> - + short_description && empty($custom_text)): ?> + short_description); ?> + - - - -

+ + + + -
- + @@ -31,9 +32,11 @@ public static function getCurrentPostType() $post_type = $current_screen->post_type; } elseif (isset($_REQUEST['post_type'])) { $post_type = sanitize_key($_REQUEST['post_type']); - } elseif ('post.php' == $pagenow + } elseif ( + 'post.php' == $pagenow && $post_id - && !empty(get_post($post_id)->post_type)) { + && !empty(get_post($post_id)->post_type) + ) { $post_type = get_post($post_id)->post_type; } elseif ('edit.php' == $pagenow && empty($_REQUEST['post_type'])) { $post_type = 'post'; @@ -167,7 +170,7 @@ private static function getDirectorySeparator() */ public static function ppch_pro_sidebar() { - ?> +?>
roles; - if(is_array($all_roles) && !empty($all_roles)) { + if (is_array($all_roles) && !empty($all_roles)) { foreach ($all_roles as $role => $details) { $role = get_role($role); if ($role->has_cap('manage_options') || $role->name === 'administrator') { @@ -358,7 +359,7 @@ public function filter_post_type_requirements($requirements, $post_type) $taxonomies_map = [ 'category' => [ - '\\PublishPress\\Checklists\\Core\\Requirement\\Categories_count', + '\\PublishPress\\Checklists\\Core\\Requirement\\Categories_count', '\\PublishPress\\Checklists\\Core\\Requirement\\Required_categories', '\\PublishPress\\Checklists\\Core\\Requirement\\Prohibited_categories', ], @@ -459,7 +460,7 @@ public function init() add_filter('publishpress_checklists_rules_list', [$this, 'filterRulesList']); add_filter('publishpress_checklists_requirement_list', [$this, 'filterRequirementsRule'], 1000); - + // Redirect on plugin activation add_action('admin_init', [$this, 'redirect_on_activate'], 2000); } @@ -469,18 +470,14 @@ public function init() * * @since 0.7 */ - public function install() - { - } + public function install() {} /** * Upgrade our data in case we need to * * @since 0.7 */ - public function upgrade($previous_version) - { - } + public function upgrade($previous_version) {} /** * Generate a link to one of the editorial metadata actions @@ -781,8 +778,8 @@ public function display_meta_box($post) 'disable_publish_button' => Base_requirement::VALUE_YES === $legacyPlugin->settings->module->options->disable_publish_button, 'title_warning_icon' => esc_html__('One or more items in the checklist are not completed'), 'is_gutenberg_active' => $this->is_gutenberg_active(), - 'user_can_manage_options' => current_user_can( 'manage_options' ), - 'configure_url' => esc_url( $this->get_admin_link() ), + 'user_can_manage_options' => current_user_can('manage_options'), + 'configure_url' => esc_url($this->get_admin_link()), ] ); @@ -872,15 +869,18 @@ public function save_post_meta_box($id, $post) { // Authentication checks: make sure data came from our meta box and that the current user is allowed to edit the post // TODO: switch to using check_admin_referrer? See core (e.g. edit.php) for usage - if (!isset($_POST[self::METADATA_TAXONOMY . "_nonce"]) - || !wp_verify_nonce(sanitize_key($_POST[self::METADATA_TAXONOMY . "_nonce"]), __FILE__)) { + if ( + !isset($_POST[self::METADATA_TAXONOMY . "_nonce"]) + || !wp_verify_nonce(sanitize_key($_POST[self::METADATA_TAXONOMY . "_nonce"]), __FILE__) + ) { return $id; } if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || !array_key_exists($post->post_type, $this->getSelectedPostTypes()) || $post->post_type == 'post' && !current_user_can('edit_post', $id) - || $post->post_type == 'page' && !current_user_can('edit_page', $id)) { + || $post->post_type == 'page' && !current_user_can('edit_page', $id) + ) { return $id; } @@ -1151,7 +1151,7 @@ protected function instantiate_custom_items_to_validate_settings($new_options) } } } - + if (isset($new_options['openai_items']) && !empty($new_options['openai_items'])) { foreach ($new_options['openai_items'] as $id) { if (isset($new_options[$id . '_title'])) { @@ -1179,10 +1179,10 @@ protected function sanitize_checklists_options($new_options) //option value is an array of keys => $value pair $sanitized_value = []; - foreach($option_value as $option_value_key => $option_value_value){ + foreach ($option_value as $option_value_key => $option_value_value) { $sanitized_value[sanitize_key($option_value_key)] = is_array($option_value_value) ? array_map('sanitize_text_field', $option_value_value) : sanitize_text_field($option_value_value); } - + //unset original option sanitize_key can potentially change key value if they are manipulated ? unset($new_options[$option_key]); @@ -1192,7 +1192,7 @@ protected function sanitize_checklists_options($new_options) return $new_options; } - + /** * Rearrange the requirements array by custom order * @@ -1229,7 +1229,7 @@ protected function rearrange_requirement_array($requirements, $is_on_metabox = t return $new_requirements_array; } - + /** * Redirect user on plugin activation @@ -1242,7 +1242,7 @@ public function redirect_on_activate() delete_option('ppch_activated'); wp_redirect(admin_url("admin.php?page=ppch-checklists")); exit; - } + } } /** @@ -1252,8 +1252,17 @@ public function retrieveFieldTabs() { // Get the singleton instance $fieldsTabs = FieldsTabs::getInstance(); + $postTypes = $this->get_post_types(); + $allFieldsTabs = $fieldsTabs->getFieldsTabs(); + $filteredFieldsTabs = array_filter($allFieldsTabs, function ($_, $key) { + return !in_array($key, ['advanced-custom-fields', 'woocommerce']); + }, ARRAY_FILTER_USE_BOTH); + $result = []; + foreach ($postTypes as $key => $postType) { + $result[$key] = $filteredFieldsTabs; + } - $this->field_tabs = $fieldsTabs->getFieldsTabs(); + $this->field_tabs = apply_filters('publishpress_checklists_filter_field_tabs', $result, $allFieldsTabs); } } } diff --git a/modules/checklists/templates/global-checklists.php b/modules/checklists/templates/global-checklists.php index e23ab9d2..8ddf0f0d 100644 --- a/modules/checklists/templates/global-checklists.php +++ b/modules/checklists/templates/global-checklists.php @@ -3,7 +3,7 @@
+ value="">
- +
-
-
    - $args) { ; ?> -
  • - - - - -
  • +
    + $post_type_label) { ?> +
      + foreach ($context['tabs'][$post_type_key] as $key => $args) { ?> +
    • + + + + +
    • +
    -
    -
    - - - - - - - - - - - - $post_type_requirements) : ?> - - + + +
    +
    + + + + + + + + + + $post_type_requirements) : ?> + + - - + + + + - - + + - - -
    - + + +
- +
+ + @@ -109,7 +113,7 @@ class="pp-checklists-requirement-row ppch-grou - @@ -137,5 +141,5 @@ class="pp-checklists-requirement-row ppch-grou
+ @@ -128,7 +132,7 @@ class="pp-checklists-requirement-row ppch-grou - +

- + value=""> + \ No newline at end of file diff --git a/modules/settings/settings.php b/modules/settings/settings.php index d0785405..dafa4737 100644 --- a/modules/settings/settings.php +++ b/modules/settings/settings.php @@ -1,4 +1,5 @@ get_post_types()); if (in_array($post_type, $post_types)) : - ?> +?> - get_post_types()); if (in_array($post_type, $post_types)) : - ?> + ?> - var ma_admin_url = ''; - + ?>
- - '; - $i = 0; - foreach ($tabs as $tabLink => $tabLabel) { - echo ''; - $i++; - } - echo ''; - } - ?> + action="module->settings_slug, false)); ?>" method="post"> + + '; + $i = 0; + foreach ($tabs as $tabLink => $tabLabel) { + echo ''; + $i++; + } + echo ''; + } + ?> - module->options_group_name); ?> - module->options_group_name); ?> + module->options_group_name); ?> + module->options_group_name); ?> - class_names as $slug => $class_name) { - $mod_data = $legacyPlugin->$slug->module; + class_names as $slug => $class_name) { + $mod_data = $legacyPlugin->$slug->module; - if ($mod_data->autoload - || $mod_data->slug === $this->module->slug - || !isset($mod_data->general_options) - || $mod_data->options->enabled != 'on') { - continue; - } + if ( + $mod_data->autoload + || $mod_data->slug === $this->module->slug + || !isset($mod_data->general_options) + || $mod_data->options->enabled != 'on' + ) { + continue; + } - echo sprintf('

%s

', esc_html($mod_data->title)); - echo sprintf('

%s

', esc_html($mod_data->short_description)); + echo sprintf('

%s

', esc_html($mod_data->title)); + echo sprintf('

%s

', esc_html($mod_data->short_description)); - echo ''; + echo ''; - $legacyPlugin->$slug->print_configure_view(); - } + $legacyPlugin->$slug->print_configure_view(); + } - // Check if we have any feature user can toggle. - $featuresCount = 0; + // Check if we have any feature user can toggle. + $featuresCount = 0; - foreach ($legacyPlugin->modules as $mod_name => $mod_data) { - if (!$mod_data->autoload && $mod_data->slug !== $this->module->slug) { - $featuresCount++; - } - } - ?> + foreach ($legacyPlugin->modules as $mod_name => $mod_data) { + if (!$mod_data->autoload && $mod_data->slug !== $this->module->slug) { + $featuresCount++; + } + } + ?> - 0) : ?> -
-

-

- - - - - - - - -
0) : ?> +
+

+

- modules as $mod_name => $mod_data) : ?> - - autoload || $mod_data->slug === $this->module->slug) { - continue; - } ?> - - -
- -
- - module->name - ) . '" />'; ?> -
- - - -
+ ); ?>

+ + + + + + + + +
+ modules as $mod_name => $mod_data) : ?> + + autoload || $mod_data->slug === $this->module->slug) { + continue; + } ?> + + +
+ +
+ + module->name + ) . '" />'; ?> +
+ + + +
- +
print_modules(); ?> @@ -353,9 +356,9 @@ public function print_modules() if (empty($legacyPlugin->modules)) { echo '
' . esc_html__( - 'There are no PublishPress modules registered', - 'publishpress-checklists' - ) . '
'; + 'There are no PublishPress modules registered', + 'publishpress-checklists' + ) . '
'; } else { foreach ($legacyPlugin->modules as $mod_name => $mod_data) { $add_menu = isset($mod_data->add_menu) && $mod_data->add_menu === true; @@ -412,7 +415,7 @@ public function helper_print_error_or_description($field, $description)

- post_type_support)) { echo '   ' . sprintf( esc_html__( - 'Disabled because add_post_type_support(\'%1$s\', \'%2$s\') is included in a loaded file.', - 'publishpress-checklists' - ), - esc_html($post_type), - esc_html($module->post_type_support) - ) . ''; + 'Disabled because add_post_type_support(\'%1$s\', \'%2$s\') is included in a loaded file.', + 'publishpress-checklists' + ), + esc_html($post_type), + esc_html($module->post_type_support) + ) . ''; } echo '
'; } @@ -468,22 +471,22 @@ public function helper_option_custom_post_type($module, $post_types = []) */ public function helper_settings_validate_and_save() { - if (!isset($_POST['action'], $_POST['_wpnonce'], $_POST['option_page'], $_POST['_wp_http_referer'], $_POST['submit']) || !is_admin( - )) { + if (!isset($_POST['action'], $_POST['_wpnonce'], $_POST['option_page'], $_POST['_wp_http_referer'], $_POST['submit']) || !is_admin()) { return false; } - if ($_POST['action'] != 'update' + if ( + $_POST['action'] != 'update' || !isset($_GET['page']) || (isset($_GET['page']) && $_GET['page'] != 'ppch-settings') - ) { + ) { return false; } if (!current_user_can('manage_options') || !wp_verify_nonce( sanitize_key($_POST['_wpnonce']), - 'edit-publishpress-settings' - )) { + 'edit-publishpress-settings' + )) { wp_die(esc_html__('Cheatin’ uh?', 'publishpress-checklists')); } @@ -498,8 +501,10 @@ public function helper_settings_validate_and_save() // Run through all the modules updating their statuses foreach ($legacyPlugin->modules as $mod_data) { - if ($mod_data->autoload - || $mod_data->slug === $this->module->slug) { + if ( + $mod_data->autoload + || $mod_data->slug === $this->module->slug + ) { continue; } @@ -585,7 +590,7 @@ protected function sanitize_module_options($module_options) */ protected function is_associative_array($array) { - if(!is_array($array)){ + if (!is_array($array)) { return false; } @@ -594,7 +599,7 @@ protected function is_associative_array($array) } return array_keys($array) !== range(0, count($array) - 1); } - + public function validate_module_settings($new_options) { @@ -606,11 +611,11 @@ public function validate_module_settings($new_options) $new_options['show_warning_icon_submit'] = Base_requirement::VALUE_NO; } - if (!isset ($new_options['disable_quick_edit_publish'])) { + if (!isset($new_options['disable_quick_edit_publish'])) { $new_options['disable_quick_edit_publish'] = Base_requirement::VALUE_NO; } - if (!isset ($new_options['disable_quick_edit_completely'])) { + if (!isset($new_options['disable_quick_edit_completely'])) { $new_options['disable_quick_edit_completely'] = Base_requirement::VALUE_NO; } @@ -641,8 +646,8 @@ public function options_page_controller() } if ($message && isset($requested_module->messages[$message])) { $display_text .= '

' . esc_html( - $requested_module->messages[$message] - ) . '

'; + $requested_module->messages[$message] + ) . '

'; } // If there's been an error, let's display it @@ -657,8 +662,8 @@ public function options_page_controller() } if ($error && isset($requested_module->messages[$error])) { $display_text .= '

' . esc_html( - $requested_module->messages[$error] - ) . '

'; + $requested_module->messages[$error] + ) . '

'; } $this->printDefaultHeader($requested_module); @@ -818,9 +823,9 @@ public function settings_show_warning_icon_submit_option($args = []) echo ''; echo '   ' . esc_html__( - 'This will display a warning icon in the "Publish" box.', - 'publishpress-checklists' - ); + 'This will display a warning icon in the "Publish" box.', + 'publishpress-checklists' + ); echo ''; } @@ -839,9 +844,9 @@ public function settings_disable_quick_edit_publish_option($args = []) echo ''; echo '   ' . esc_html__( - 'Disabling the "Status" option is recommended because it can be used to avoid using the Checklists requirements.', - 'publishpress-checklists' - ); + 'Disabling the "Status" option is recommended because it can be used to avoid using the Checklists requirements.', + 'publishpress-checklists' + ); echo ''; } @@ -860,9 +865,9 @@ public function settings_disable_quick_edit_completely_option($args = []) echo ''; echo '   ' . esc_html__( - 'This will disable "Quick Edit" for all users except those with the "manage_options" capability.', - 'publishpress-checklists' - ); + 'This will disable "Quick Edit" for all users except those with the "manage_options" capability.', + 'publishpress-checklists' + ); echo ''; } @@ -881,9 +886,9 @@ public function settings_disable_publish_button_option($args = []) echo ''; echo '   ' . esc_html__( - 'This will disable the "Publish" button when checklist requirements are not met.', - 'publishpress-checklists' - ); + 'This will disable the "Publish" button when checklist requirements are not met.', + 'publishpress-checklists' + ); echo ''; } @@ -898,11 +903,11 @@ public function settings_openai_api_key_option($args = []) $value = isset($this->module->options->openai_api_key) ? $this->module->options->openai_api_key : ''; echo ''; } @@ -930,7 +935,7 @@ public function filter_settings_validate($new_options, $module_name) $this->module->post_type_support ); - if (!isset ($new_options['show_warning_icon_submit'])) { + if (!isset($new_options['show_warning_icon_submit'])) { $new_options['show_warning_icon_submit'] = Base_requirement::VALUE_NO; } $new_options['show_warning_icon_submit'] = Base_requirement::VALUE_YES === $new_options['show_warning_icon_submit'] ? Base_requirement::VALUE_YES : Base_requirement::VALUE_NO;