Skip to content

Adds New Time Ranges: Today, This Week, and This Month #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/blocks/block-wpp-widget.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '942d471458588c607ed1');
<?php return array('dependencies' => array(), 'version' => 'cbd713bf860812786509');
2 changes: 1 addition & 1 deletion assets/js/blocks/block-wpp-widget.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Block/Widget/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function render(array $attributes)

// possible values for "Time Range" and "Order by"
$time_units = ['minute', 'hour', 'day', 'week', 'month'];
$range_values = ['daily', 'last24hours', 'weekly', 'last7days', 'monthly', 'last30days', 'all', 'custom'];
$range_values = ['daily', 'last24hours', 'today', 'weekly', 'last7days', 'thisweek', 'monthly', 'last30days', 'thismonth', 'all', 'custom'];
$order_by_values = ['comments', 'views', 'avg'];

$theme_data = $this->themer->get_theme($theme);
Expand Down
3 changes: 3 additions & 0 deletions src/Block/Widget/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ export class WPPWidgetBlockEdit extends Component
value={attributes.range}
options={[
{label: __('Last 24 Hours', 'wordpress-popular-posts'), value: 'last24hours'},
{label: __('Today', 'wordpress-popular-posts'), value: 'today'},
{label: __('Last 7 days', 'wordpress-popular-posts'), value: 'last7days'},
{label: __('This week', 'wordpress-popular-posts'), value: 'thisweek'},
{label: __('Last 30 days', 'wordpress-popular-posts'), value: 'last30days'},
{label: __('This month', 'wordpress-popular-posts'), value: 'thismonth'},
{label: __('All-time', 'wordpress-popular-posts'), value: 'all'},
{label: __('Custom', 'wordpress-popular-posts'), value: 'custom'},
]}
Expand Down
22 changes: 22 additions & 0 deletions src/Compatibility/Elementor/Elementor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function init()
add_action('elementor/editor/after_enqueue_scripts', [$this, 'elementor_icon_css']);
// Registers WPP widget
add_action('elementor/widgets/register', [$this, 'register_widget']);
// Disable [wpp] shortcode AJAX loading in editor mode
add_filter('shortcode_atts_wpp', [$this, 'disable_ajax_in_editor'], 10, 4);
}
}

Expand Down Expand Up @@ -95,4 +97,24 @@ public function register_widget($widgets_manager) {
)
);
}

/**
* Disables [wpp] AJAX loading in editor mode.
*
* @since 7.4.0
* @param array $out The output array of shortcode attributes.
* @param array $pairs The supported attributes and their defaults.
* @param array $atts The user defined shortcode attributes.
* @param string $shortcode The shortcode name.
*/
public function disable_ajax_in_editor(array $out, array $pairs, array $atts, string $shortcode) {
if (
'wpp' === $shortcode &&
\Elementor\Plugin::$instance->editor->is_edit_mode()
) {
$out['ajaxify'] = 0;
}

return $out;
}
}
3 changes: 3 additions & 0 deletions src/Compatibility/Elementor/widgets/widget-controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@
'default' => 'last24hours',
'options' => [
'last24hours' => esc_html__('Last 24 Hours', 'wordpress-popular-posts'),
'today' => esc_html__('Today', 'wordpress-popular-posts'),
'last7days' => esc_html__('Last 7 days', 'wordpress-popular-posts'),
'thisweek' => esc_html__('This week', 'wordpress-popular-posts'),
'last30days' => esc_html__('Last 30 days', 'wordpress-popular-posts'),
'thismonth' => esc_html__('This month', 'wordpress-popular-posts'),
'all' => esc_html__('All-time', 'wordpress-popular-posts'),
'custom' => esc_html__('Custom', 'wordpress-popular-posts')
]
Expand Down
5 changes: 0 additions & 5 deletions src/Compatibility/Elementor/widgets/widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ protected function render(): void {
$widget_id = $this->get_id();
$settings = $this->parse_settings();

/** We're in Edit mode, disable AJAX loading and display widget ID */
if ( $is_edit_mode ) {
$settings['ajaxify'] = '0';
}

/**
* Allows to modify settings passed to wpp_get_mostpopular()
*
Expand Down
38 changes: 38 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,42 @@ public static function sanitize_html(string $html, array $options)

return wp_kses($html, $allowed_tags);
}

/**
* Returns the name of the first day of the week as per
* WordPress settings.
*
* @since 7.4.0
* @return string
*/
public static function get_start_of_week()
{
$start_of_week = absint(get_option('start_of_week', 0));

switch( $start_of_week ) {
case 1;
$day = 'Monday';
break;
case 2;
$day = 'Tuesday';
break;
case 3;
$day = 'Wednesday';
break;
case 4;
$day = 'Thurdsday';
break;
case 5;
$day = 'Friday';
break;
case 6;
$day = 'Saturday';
break;
default:
$day = 'Sunday';
break;
}

return $day;
}
}
23 changes: 23 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,41 @@ private function build_query()
$start_datetime = $start_date->format('Y-m-d H:i:s');
$views_time_range = "view_datetime >= '{$start_datetime}'";
break;
case 'today':
$start_datetime = $start_date->format('Y-m-d 00:00:00');
$end_datetime = $start_date->format('Y-m-d 23:59:55');
$views_time_range = "view_date >= '{$start_datetime}' AND view_date <= '{$end_datetime}'";
break;
case 'last7days':
case 'weekly':
$start_date = $start_date->sub(new \DateInterval('P6D'));
$start_datetime = $start_date->format('Y-m-d');
$views_time_range = "view_date >= '{$start_datetime}'";
break;
case 'thisweek':
$start_of_week = Helper::get_start_of_week();
$today_weekday = $now->format('l');
$datetime = ( $today_weekday === $start_of_week ) ? 'today' : 'last ' . $start_of_week;
$start_date = new \DateTime($datetime, wp_timezone());
$start_datetime = $start_date->format('Y-m-d 00:00:00');
$start_date->add(new \DateInterval('P6D'));
$end_datetime = $start_date->format('Y-m-d 23:59:59');
$views_time_range = "view_date >= '{$start_datetime}' AND view_date <= '{$end_datetime}'";
break;
case 'last30days':
case 'monthly':
$start_date = $start_date->sub(new \DateInterval('P29D'));
$start_datetime = $start_date->format('Y-m-d');
$views_time_range = "view_date >= '{$start_datetime}'";
break;
case 'thismonth':
$start_date = new \DateTime('First day of this month', wp_timezone());
$start_datetime = $start_date->format('Y-m-d 00:00:00');
$end_date = new \DateTime('Last day of this month', wp_timezone());
$end_datetime = $end_date->format('Y-m-d 23:59:59');
$views_time_range = "view_date >= '{$start_datetime}' AND view_date <= '{$end_datetime}'";
break;
break;
case 'custom':
$time_units = ['MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH'];

Expand Down
2 changes: 1 addition & 1 deletion src/Rest/PostsEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function get_collection_params()
'range' => [
'description' => __('Return popular posts from a specified time range.'),
'type' => 'string',
'enum' => ['last24hours', 'last7days', 'last30days', 'all', 'custom'],
'enum' => ['last24hours', 'today', 'last7days', 'thisweek', 'last30days', 'thismonth', 'all', 'custom'],
'default' => 'last24hours',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
Expand Down
6 changes: 3 additions & 3 deletions src/Shortcode/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function handle($attributes = []) : string

// possible values for "Time Range" and "Order by"
$time_units = ['minute', 'hour', 'day', 'week', 'month'];
$range_values = ['daily', 'last24hours', 'weekly', 'last7days', 'monthly', 'last30days', 'all', 'custom'];
$range_values = ['daily', 'last24hours', 'today', 'weekly', 'last7days', 'thisweek', 'monthly', 'last30days', 'thismonth', 'all', 'custom'];
$order_by_values = ['comments', 'views', 'avg'];

$shortcode_ops = [
Expand Down Expand Up @@ -229,8 +229,8 @@ public function handle($attributes = []) : string

$load_via_ajax = $this->config['tools']['ajax'];

if ( isset($attributes['ajaxify']) && is_numeric($attributes['ajaxify']) ) {
$load_via_ajax = (bool) absint($attributes['ajaxify']);
if ( is_numeric($ajaxify) ) {
$load_via_ajax = (bool) absint($ajaxify);
}

if ( $load_via_ajax && ! is_customize_preview() && ! $isAdmin ) {
Expand Down