'; echo $before_widget; // Display the widget if( $title ) { echo wp_kses_post($before_title . $title . $after_title); } // Check if text is set $args = array( 'posts_per_page' => $posts_per_page, 'post_type' => 'post', 'post_status' => 'publish', 'meta_key' => 'mashsb_shares', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'date_query' => array( array( 'after' => $period . ' days ago', // or '-7 days' 'inclusive' => true, ), ), ); $wpq = $this->get_qry_from_cache($args); if( $wpq->have_posts() ) : echo ''; endif; wp_reset_postdata(); echo $after_widget; echo ''; } /** * Cut characters of the title * * @param string $string * @param int $int * @return string */ private function limit_title($string, $int){ if (empty($string) || !is_numeric( $int)){ return $string; } $newstring = substr($string, 0, $int) . '...'; return $newstring; } /** * Cut characters of the excerpt * * @param string $excerpt * @param int $int * @return string */ private function limit_excerpt($excerpt, $int){ if (empty($excerpt) || !is_numeric( $int)){ return $excerpt; } return substr($excerpt, 0, $int); } /** * 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 ) )); } 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; } } } /* Register Widget */ function mashsb_register_widget() { register_widget( 'mashsb_mostshared_posts_widget' ); } add_action( 'widgets_init', 'mashsb_register_widget', 1 );