Skip to content

Commit

Permalink
* New: Most Shared Post Widget -> Select posts by date of publish lik…
Browse files Browse the repository at this point in the history
…e 14 days, 1 month ago and so on.
  • Loading branch information
rene-hermenau committed Jun 1, 2016
1 parent 516905d commit 0b88e23
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ 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';
$showShares = 'true';
$countLabel = 'Shares';
//$separator = '|';
$wrapShares = 'false';
$period = '7';
}
?>

Expand Down Expand Up @@ -56,12 +58,23 @@ public function form( $instance ) {
<input class="widefat" id="<?php //echo $this->get_field_id( 'separator' ); ?>" name="<?php //echo $this->get_field_name( 'separator' ); ?>" type="text" value="<?php //echo $separator; ?>" />
</p>//-->
<p>
<label for="<?php echo $this->get_field_id( 'wrapShares' ); ?>"><?php _e( 'Show shares below post title?', 'mashsb' ); ?></label>
<label for="<?php echo $this->get_field_id( 'wrapShares' ); ?>"><?php _e( 'Show shares below post title', 'mashsb' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'wrapShares' ); ?>" name="<?php echo $this->get_field_name( 'wrapShares' ); ?>">
<option value="true" <?php if( $wrapShares === 'true' ) echo 'selected'; ?>>Yes</option>
<option value="false" <?php if( $wrapShares === 'false' ) echo 'selected'; ?>>No</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'period' ); ?>"><?php _e( 'Time period and age of posts', 'mashsb' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'period' ); ?>" name="<?php echo $this->get_field_name( 'period' ); ?>">
<option value="7" <?php if( $period === '7' ) echo 'selected'; ?>>7 Days</option>
<option value="7" <?php if( $period === '14' ) echo 'selected'; ?>>14 Days</option>
<option value="30" <?php if( $period === '30' ) echo 'selected'; ?>>1 Month</option>
<option value="90" <?php if( $period === '90' ) echo 'selected'; ?>>3 Months</option>
<option value="180" <?php if( $period === '180' ) echo 'selected'; ?>>6 Months</option>
<option value="365" <?php if( $period === '365' ) echo 'selected'; ?>>1 Year</option>
</select>
</p>

<?php
}
Expand All @@ -75,6 +88,7 @@ public function update( $new_instance, $old_instance ) {
$instance['countLabel'] = strip_tags( $new_instance['countLabel'] );
//$instance['separator'] = strip_tags( $new_instance['separator'] );
$instance['wrapShares'] = strip_tags( $new_instance['wrapShares'] );
$instance['period'] = strip_tags( $new_instance['period'] );
return $instance;
}

Expand All @@ -89,6 +103,7 @@ public function widget( $args, $instance ) {
$countLabel = $instance['countLabel'];
//$separator = $instance['separator'];
$wrapShares = $instance['wrapShares'];
$period = $instance['period'];

$break = $wrapShares === 'true' ? '</br>' : '';

Expand All @@ -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 '<ul>';
while ( $wpq->have_posts() ):
Expand All @@ -126,6 +150,28 @@ public function widget( $args, $instance ) {
echo $after_widget;
echo '<!-- MashShare Most Popular Widget End //-->';
}

/**
* 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;
}
}

}

Expand Down
2 changes: 1 addition & 1 deletion mashshare.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 0b88e23

Please sign in to comment.