diff --git a/modules/calendar/calendar.php b/modules/calendar/calendar.php
index 08c806ec..2a983d58 100644
--- a/modules/calendar/calendar.php
+++ b/modules/calendar/calendar.php
@@ -91,6 +91,8 @@ class PP_Calendar extends PP_Module
* @var [type]
*/
public $module;
+
+ public $module_url;
/**
* [$start_date description]
@@ -157,6 +159,42 @@ class PP_Calendar extends PP_Module
* @var \PublishPress\Utility\Date
*/
private $dateUtil;
+
+ /**
+ * @var array
+ */
+ public $filters;
+
+ /**
+ * @var array
+ */
+ public $form_filters = [];
+
+ /**
+ * @var array
+ */
+ public $form_filter_list = [];
+
+ /**
+ * [$user_filters description]
+ *
+ * @var [type]
+ */
+ public $user_filters;
+
+ /**
+ * Custom methods
+ *
+ * @var array
+ */
+ private $terms_options = [];
+
+ /**
+ * [$content_calendar_datas description]
+ *
+ * @var [type]
+ */
+ public $content_calendar_datas;
/**
* Construct the PP_Calendar class
@@ -176,15 +214,16 @@ public function __construct()
'post_type_support' => 'pp_calendar',
'default_options' => [
'enabled' => 'on',
- 'post_types' => [
- 'post' => 'on',
- 'page' => 'off',
- ], 'ics_subscription' => 'on',
+ 'post_types' => $this->pre_select_all_post_types(),
+ 'ics_subscription' => 'on',
'ics_subscription_public_visibility' => 'off',
'ics_secret_key' => wp_generate_password(),
'show_posts_publish_time' => ['publish' => 'on', 'future' => 'on'],
'default_publish_time' => '',
'show_calendar_posts_full_title' => 'off',
+ // Leave default as non array to confirm if user save settings or not
+ 'content_calendar_filters' => '',
+ 'content_calendar_custom_filters' => '',
],
'messages' => [
'post-date-updated' => __('Post date updated.', 'publishpress'),
@@ -648,65 +687,9 @@ public function enqueue_admin_scripts()
/*
* Filters
*/
- $userFilters = $this->get_filters();
- $calendar_request_args = [];
- $calendar_request_filter = [];
-
- if (isset($userFilters['post_status'])) {
- $postStatus = sanitize_text_field($userFilters['post_status']);
-
- if (! empty($postStatus)) {
- $calendar_request_args['post_status'] = $postStatus;
- $calendar_request_filter['post_status'] = $postStatus;
- }
- }
-
- if (isset($userFilters['cat']) && !empty($userFilters['cat'])) {
- $category = (int) $userFilters['cat'];
- $categoryData = get_term_by('ID', $category, 'category');
-
- if (isset($categoryData->slug)) {
- $calendar_request_args = $this->addTaxQueryToArgs('category', $categoryData->slug, $calendar_request_args);
- $calendar_request_filter['category'] = ['value' => $categoryData->slug, 'text' => $categoryData->name];
- }
- }
-
- if (isset($userFilters['tag']) && !empty($userFilters['tag'])) {
- $postTag = (int) $userFilters['tag'];
- $tagData = get_term_by('ID', $postTag, 'post_tag');
-
- if (isset($tagData->slug)) {
- $calendar_request_args = $this->addTaxQueryToArgs('post_tag', $tagData->slug, $calendar_request_args);
- $calendar_request_filter['post_tag'] = ['value' => $tagData->slug, 'text' => $tagData->name];
- }
- }
-
- if (isset($userFilters['author']) && !empty($userFilters['author'])) {
- $postAuthor = (int)$userFilters['author'];
- $authorData = get_user_by('ID', $postAuthor);
-
- if (isset($authorData->ID)) {
- $calendar_request_args['author'] = $authorData->ID;
- $calendar_request_filter['post_author'] = ['value' => $authorData->ID, 'text' => $authorData->display_name];
- }
- }
-
- if (isset($userFilters['cpt'])) {
- $postType = sanitize_key($userFilters['cpt']);
-
- if (!empty($postType)) {
- $calendar_request_args['post_type'] = $postType;
- $calendar_request_filter['post_type'] = $postType;
- }
- }
-
- if (isset($userFilters['weeks'])) {
- $weeks = sanitize_key($userFilters['weeks']);
-
- if (! empty($weeks)) {
- $calendar_request_filter['weeks'] = $weeks;
- }
- }
+ $userFilters = $this->get_filters();
+ $calendar_request_args = $userFilters;
+ $calendar_request_filter = $userFilters;
$maxVisibleItemsOption = isset($this->module->options->max_visible_posts_per_date) && ! empty($this->default_max_visible_posts_per_date) ?
(int)$this->module->options->max_visible_posts_per_date : $this->default_max_visible_posts_per_date;
@@ -1203,6 +1186,7 @@ public function get_filters()
'tag' => '',
'author' => '',
'start_date' => date('Y-m-d', current_time('timestamp')),
+ 'me_mode' => '',
];
$old_filters = array_merge($default_filters, (array)$old_filters);
@@ -1226,6 +1210,10 @@ public function get_filters()
// Set the start date as the beginning of the week, according to blog settings
$filters['start_date'] = $this->get_beginning_of_week($filters['start_date']);
+ if (!empty($filters['me_mode'])) {
+ $filters['author'] = $current_user->ID;
+ }
+
$filters = apply_filters('pp_calendar_filter_values', $filters, $old_filters);
$this->update_user_meta($current_user->ID, self::USERMETA_KEY_PREFIX . 'filters', $filters);
@@ -1274,6 +1262,920 @@ protected function get_selected_post_types()
return $return;
}
+ public function content_calendar_filters()
+ {
+ $select_filter_names = [];
+
+ $editorial_metadata = $this->terms_options;
+
+ foreach ($this->filters as $filter_key => $filter_label) {
+ if (array_key_exists($filter_key, $editorial_metadata) && $editorial_metadata[$filter_key]['type'] === 'date') {
+ $select_filter_names[$filter_key . '_start'] = $filter_key . '_start';
+ $select_filter_names[$filter_key . '_end'] = $filter_key . '_end';
+ $select_filter_names[$filter_key . '_start_hidden'] = $filter_key . '_start_hidden';
+ $select_filter_names[$filter_key . '_end_hidden'] = $filter_key . '_end_hidden';
+ }
+ $select_filter_names[$filter_key] = $filter_key;
+ }
+
+ return apply_filters('PP_Content_Calendar_filter_names', $select_filter_names);
+ }
+
+ public function content_calendar_filter_options($select_id, $select_name, $filters)
+ {
+
+ if (array_key_exists($select_id, $this->terms_options)) {
+ $select_id = 'metadata_key';
+ }
+
+ if (array_key_exists($select_id, $this->content_calendar_datas['taxonomies']) && taxonomy_exists($select_id)) {
+ $select_id = 'taxonomy';
+ }
+
+ $filter_label = '';
+ $selected_value = '';
+
+ ob_start();
+
+ switch ($select_id) {
+ case 'post_status':
+ $post_statuses = $this->get_post_statuses();
+ $filter_label = esc_html__('Post Status', 'publishpress');
+ ?>
+
+ label);
+ ?>
+
+
+
+
+
+
+
+ terms_options[$select_name];
+
+ $metadata_type = $metadata_term['type'];
+ $selected_value = $metadata_value;
+ $filter_label = $metadata_term['name'];
+
+ ?>
+
+
+
+
+
+
+
+
+ 'filter-submit']); ?>
+
+
+
+
+
+
+
+
+
+ ',
+ esc_attr($metadata_start_name),
+ esc_attr($metadata_start_value),
+ esc_attr(pp_convert_date_format_to_jqueryui_datepicker('Y-m-d')),
+ ''
+ );
+ printf(
+ '
',
+ esc_attr($metadata_start_name),
+ esc_attr($metadata_start_value_hidden)
+ );
+ ?>
+
+ ',
+ esc_attr($metadata_end_name),
+ esc_attr($metadata_end_value),
+ esc_attr(pp_convert_date_format_to_jqueryui_datepicker('Y-m-d')),
+ ''
+ );
+ printf(
+ '
',
+ esc_attr($metadata_end_name),
+ esc_attr($metadata_end_value_hidden)
+ );
+ ?>
+
+ 'filter-submit']); ?>
+
+ display_name;
+ }
+ }
+ $user_dropdown_args = [
+ 'show_option_all' => $metadata_term['name'],
+ 'name' => $select_name,
+ 'selected' => $metadata_value,
+ 'class' => 'pp-custom-select2'
+ ];
+ $user_dropdown_args = apply_filters('pp_editorial_metadata_user_dropdown_args', $user_dropdown_args);
+ wp_dropdown_users($user_dropdown_args);
+ } elseif ($metadata_type === 'checkbox') {
+ if ($metadata_value == '1') {
+ $selected_value = esc_html__('Checked', 'publishpress');
+ } else {
+ $selected_value = '';
+ }
+ ?>
+
+
+ />
+
+ 'filter-submit']); ?>
+
+
';
+ break;
+
+ default:
+ if (array_key_exists($select_name, $this->form_filter_list)) {
+ $selected_value_meta = isset($filters[$select_name]) ? sanitize_text_field($filters[$select_name]) : '';
+ $filter_label = $this->filters[$select_name];
+ $selected_value = $selected_value_meta;
+
+ if (strpos($select_name, "ppch_co_checklist_") === 0) {
+ ?>
+
+
+
+ $selected_value, 'filter_label' => $filter_label, 'html' => ob_get_clean()];
+ }
+
+ private function meta_query_operator_label($operator = false) {
+ $operators = [
+ 'equals' => 'Equals (=)',
+ 'not_equals' => 'Does not equal (!=)',
+ 'greater_than' => 'Greater than (>)',
+ 'greater_than_or_equals' => 'Greater than or equals (>=)',
+ 'less_than' => 'Less than (<)',
+ 'less_than_or_equals' => 'Less than or equals (<=)',
+ 'like' => 'Like/Contains',
+ 'not_like' => 'Not Like',
+ 'not_exists' => 'Not Exists/Empty',
+ ];
+
+ if ($operator) {
+ $return = array_key_exists($operator, $operators) ? $operators[$operator] : $operator;
+ } else {
+ $return = $operators;
+ }
+
+ return $return;
+ }
+
+ private function meta_query_operator_symbol($operator = false) {
+ $operators = [
+ 'equals' => '=',
+ 'not_equals' => '!=',
+ 'greater_than' => '>',
+ 'greater_than_or_equals' => '>=',
+ 'less_than' => '<',
+ 'less_than_or_equals' => '<=',
+ 'like' => 'LIKE',
+ 'not_like' => 'NOT LIKE',
+ 'not_exists' => 'NOT EXISTS',
+ ];
+
+ if ($operator) {
+ $return = array_key_exists($operator, $operators) ? $operators[$operator] : $operator;
+ } else {
+ $return = $operators;
+ }
+
+ return $return;
+ }
+
+ /**
+ * Return calendar filters
+ * @return string
+ */
+ public function get_calendar_filters() {
+ ob_start();
+ ?>
+
+
+ user_filters['me_mode'];
+ $active_me_mode = !empty($me_mode) ? 'active-filter' : '';
+ ?>
+
+
+
+
+
+
+
+
+
+ ×
+ content_calendar_customize_filter_form(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
+
+
+
+
+
+
+
+ 'co-searchbox-search-submit']); ?>
+
+
+
+
+
+
+
+
+ update_module_option($this->module->name, 'content_calendar_filters', $content_calendar_filters);
+ $publishpress->update_module_option($this->module->name, 'content_calendar_custom_filters', $content_calendar_custom_filters);
+
+ // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
+ echo pp_planner_admin_notice(esc_html__('Filter updated successfully.', 'publishpress'));
+ }
+ }
+
+ /**
+ * Retrieve wordpress registered taxonomy
+ *
+ * Private taxonomy are excluded
+ */
+ public function get_all_taxonomies()
+ {
+
+ //category and post tag are not included in public taxonomy
+ $category = get_taxonomies(['name' => 'category'], 'objects');
+ $post_tag = get_taxonomies(['name' => 'post_tag'], 'objects');
+
+ $public = get_taxonomies(['_builtin' => false, 'public' => true], 'objects');
+
+ $taxonomies = array_merge($category, $post_tag, $public);
+
+ return $taxonomies;
+ }
+
+ /**
+ * Get content calendar data that's required on the
+ * content calendar page
+ *
+ * @return array
+ */
+ public function get_content_calendar_datas() {
+ global $wpdb;
+
+ if (is_array($this->content_calendar_datas)) {
+ return $this->content_calendar_datas;
+ }
+
+ $datas = [];
+
+ // add all meta keys
+ $datas['meta_keys'] = $wpdb->get_col("SELECT DISTINCT meta_key FROM $wpdb->postmeta WHERE 1=1 ORDER BY meta_key ASC");
+
+ // add editorial fields
+ if (class_exists('PP_Editorial_Metadata')) {
+ $additional_terms = get_terms(
+ [
+ 'taxonomy' => PP_Editorial_Metadata::metadata_taxonomy,
+ 'orderby' => 'name',
+ 'order' => 'asc',
+ 'hide_empty' => 0,
+ 'parent' => 0,
+ 'fields' => 'all',
+ ]
+ );
+
+ $metadatas = [];
+ foreach ($additional_terms as $term) {
+ if (! is_object($term) || $term->taxonomy !== PP_Editorial_Metadata::metadata_taxonomy) {
+ continue;
+ }
+ $metadatas[$term->slug] = $term->name;
+
+ $term_options = $this->get_unencoded_description($term->description);
+ $term_options['name'] = $term->name;
+ $term_options['slug'] = $term->slug;
+ $this->terms_options[$term->slug] = $term_options;
+ }
+
+ $datas['editorial_metadata'] = $metadatas;
+ }
+
+ // add taxononomies
+ $taxonomies = $this->get_all_taxonomies();
+ $all_taxonomies = [];
+ foreach ($taxonomies as $taxonomy) {
+ if (in_array($taxonomy->name, ['post_status', 'post_status_core_wp_pp', 'post_visibility_pp'])) {
+ continue;
+ }
+ $all_taxonomies[$taxonomy->name] = $taxonomy->label;// . ' (' . $taxonomy->name . ')';
+ }
+ $datas['taxonomies'] = $all_taxonomies;
+
+ // Add content calendar filters content
+ $content_calendar_filters = $this->module->options->content_calendar_filters;
+ $content_calendar_custom_filters = $this->module->options->content_calendar_custom_filters;
+
+ $datas['content_calendar_filters'] = is_array($content_calendar_filters) ? $content_calendar_filters : [
+ 'post_status' => esc_html__('Status', 'publishpress'),
+ 'author' => esc_html__('Author', 'publishpress'),
+ 'cpt' => esc_html__('Post Type', 'publishpress')
+ ];
+ $datas['content_calendar_custom_filters'] = is_array($content_calendar_custom_filters) ? $content_calendar_custom_filters : [];
+
+ /**
+ * @param array $datas
+ *
+ * @return $datas
+ */
+ $datas = apply_filters('publishpress_content_calendar_datas', $datas);
+
+ $this->content_calendar_datas = $datas;
+
+ return $datas;
+ }
+
+
+ /**
+ * Get content calendar form filters
+ *
+ * @return array
+ */
+ public function get_content_calendar_form_filters() {
+
+ if (!empty($this->form_filters)) {
+ return $this->form_filters;
+ }
+
+ $content_calendar_datas = $this->content_calendar_datas;
+
+ $filters = [];
+ // custom filters
+ $filters['custom'] = [
+ 'title' => esc_html__('Custom filters', 'publishpress'),
+ 'message' => esc_html__('Click the "Add New" button to create new filters.', 'publishpress'),
+ 'filters' => $content_calendar_datas['content_calendar_custom_filters']
+ ];
+
+ // default filters
+ $filters['default'] = [
+ 'title' => esc_html__('Inbuilt filters', 'publishpress'),
+ 'filters' => [
+ 'post_status' => esc_html__('Post Status', 'publishpress'),
+ 'author' => esc_html__('Author', 'publishpress'),
+ 'cpt' => esc_html__('Post Type', 'publishpress')
+ ]
+ ];
+
+ // editorial fields filters
+ if (isset($content_calendar_datas['editorial_metadata'])) {
+ $filters['editorial_metadata'] = [
+ 'title' => esc_html__('Editorial Fields', 'publishpress'),
+ 'message' => esc_html__('You do not have any editorial fields enabled', 'publishpress'),
+ 'filters' => $content_calendar_datas['editorial_metadata']
+ ];
+ }
+
+ $filters['taxonomies'] = [
+ 'title' => esc_html__('Taxonomies', 'publishpress'),
+ 'message' => esc_html__('You do not have any public taxonomies', 'publishpress'),
+ 'filters' => $content_calendar_datas['taxonomies']
+ ];
+
+ /**
+ * @param array $filters
+ * @param array $content_calendar_datas
+ *
+ * @return $filters
+ */
+ $filters = apply_filters('publishpress_content_calendar_form_filters', $filters, $content_calendar_datas);
+
+ $this->form_filters = $filters;
+
+ return $filters;
+ }
+
+
+ public function content_calendar_customize_filter_form() {
+
+ ob_start();
+
+ $content_calendar_datas = $this->content_calendar_datas;
+ $enabled_filters = array_keys($content_calendar_datas['content_calendar_filters']);
+ $filters = $this->form_filters;
+ $meta_keys = $content_calendar_datas['meta_keys'];
+
+ $all_filters = [];
+ ?>
+
+ get_post_types_for_module($this->module);
+ // update content calendar form action
+ $this->update_content_calendar_form_action();
+
+ // Get content calendar data
+ $this->content_calendar_datas = $this->get_content_calendar_datas();
+
+ $filters = $this->content_calendar_datas['content_calendar_filters'];
+ /**
+ * @param array $filters
+ *
+ * @return array
+ */
+ $this->filters = apply_filters('publishpress_content_calendar_filters', $filters);
+
+ $this->form_filters = $this->get_content_calendar_form_filters();
+ $this->form_filter_list = array_merge(...array_values(array_column($this->form_filters, 'filters')));
// Get filters either from $_GET or from user settings
- $filters = $this->get_filters();
+ $this->user_filters = $this->get_filters();
// Total number of weeks to display on the calendar. Run it through a filter in case we want to override the
// user's standard
- $this->total_weeks = empty($filters['weeks']) ? self::DEFAULT_NUM_WEEKS : $filters['weeks'];
+ $this->total_weeks = empty($this->user_filters['weeks']) ? self::DEFAULT_NUM_WEEKS : $this->user_filters['weeks'];
- $this->start_date = $filters['start_date'];
+ $this->start_date = $this->user_filters['start_date'];
// Get the custom description for this page
$description = '';
@@ -1466,6 +2383,10 @@ class="button-primary">
echo '
';
} ?>
+
+ get_calendar_filters(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
+
+