diff --git a/includes/widgets.php b/includes/class-mashsb-shared-posts-widget.php
similarity index 72%
rename from includes/widgets.php
rename to includes/class-mashsb-shared-posts-widget.php
index c4badce6..c688b5e3 100644
--- a/includes/widgets.php
+++ b/includes/class-mashsb-shared-posts-widget.php
@@ -19,8 +19,9 @@ public function form( $instance ) {
$count = esc_attr( $instance['count'] );
$showShares = esc_textarea( $instance['showShares'] );
$countLabel = esc_textarea( $instance['countLabel'] );
- //$separator = esc_textarea( $instance['separator'] ); // Maybe use this later when there is need
+ //$separator = esc_textarea( $instance['separator'] ); // Maybe use this later if there is need
$wrapShares = esc_textarea( $instance['wrapShares'] );
+ $period = esc_textarea( $instance['period'] );
} else {
$title = 'Most Shared Posts';
$count = '10';
@@ -28,6 +29,7 @@ public function form( $instance ) {
$countLabel = 'Shares';
//$separator = '|';
$wrapShares = 'false';
+ $period = '7';
}
?>
@@ -56,12 +58,23 @@ public function form( $instance ) {
//-->
-
+
+
+
+
+
' : '';
@@ -104,11 +119,20 @@ public function widget( $args, $instance ) {
$args = array(
'posts_per_page' => $count,
'post_type' => 'post',
+ 'post_status' => 'publish',
'meta_key' => 'mashsb_shares',
'orderby' => 'meta_value_num',
- 'order' => 'DESC'
+ 'order' => 'DESC',
+ 'date_query' => array(
+ array(
+ 'after' => $period . ' days ago', // or '-7 days'
+ 'inclusive' => true,
+ ),
+ ),
);
- $wpq = new WP_Query( $args );
+ //$wpq = new WP_Query( $args );
+ $wpq = $this->get_qry_from_cache($args);
+ //var_dump($wpq);
if( $wpq->have_posts() ) :
echo '';
while ( $wpq->have_posts() ):
@@ -126,6 +150,28 @@ public function widget( $args, $instance ) {
echo $after_widget;
echo '';
}
+
+ /**
+ * Get and store query from transient
+ *
+ * @param array $args
+ * @return \WP_Query
+ */
+ public function get_qry_from_cache( $args ) {
+ $expiration = mashsb_get_expiration();
+
+ if (!MASHSB_DEBUG){
+ delete_transient('mashwidget_' . md5( json_encode( $args ) )); // debug
+ }
+
+ if( false === ( $qry = get_transient( 'mashwidget_' . md5( json_encode( $args ) ) ) ) ) {
+ $wpq = new WP_Query( $args );
+ set_transient( 'mashwidget_' . md5( json_encode( $args ) ), $wpq, $expiration );
+ return $wpq;
+ } else {
+ return $qry;
+ }
+ }
}
diff --git a/mashshare.php b/mashshare.php
index abb44dd9..94584442 100644
--- a/mashshare.php
+++ b/mashshare.php
@@ -189,7 +189,7 @@ private function includes() {
require_once MASHSB_PLUGIN_DIR . 'includes/logger.php';
require_once MASHSB_PLUGIN_DIR . 'includes/actions.php';
require_once MASHSB_PLUGIN_DIR . 'includes/helper.php';
- require_once MASHSB_PLUGIN_DIR . 'includes/widgets.php';
+ require_once MASHSB_PLUGIN_DIR . 'includes/class-mashsb-shared-posts-widget.php';
require_once MASHSB_PLUGIN_DIR . 'includes/admin/settings/metabox-settings.php'; /* move into is_admin */
require_once MASHSB_PLUGIN_DIR . 'includes/admin/meta-box/meta-box.php';
require_once MASHSB_PLUGIN_DIR . 'includes/header-meta-tags.php';
diff --git a/readme.txt b/readme.txt
index dbfe5e6e..feb84d76 100644
--- a/readme.txt
+++ b/readme.txt
@@ -314,6 +314,7 @@ Read here more FAQ: [https://www.mashshare.net/faq/](https://www.mashshare.net/f
== Changelog ==
= 3.0.1 =
+* New: Most Shared Post Widget -> Select posts by date of publish like 14 days, 1 month ago and so on.
* Fix: When twitter card or open graph tags are disabled mashshare must not disable yoast open graph and twitter cards data
= 3.0.0 =