initial commit
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Widget to display the overall count.
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Widget to display the overall count.
|
||||
*
|
||||
* @extends WP_Widget
|
||||
*/
|
||||
class Top_Ten_Count_Widget extends WP_Widget {
|
||||
|
||||
/**
|
||||
* Register widget with WordPress.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'widget_tptn_count', // Base ID.
|
||||
__( 'Overall count [Top 10]', 'top-10' ), // Name.
|
||||
array(
|
||||
'description' => __( 'Display overall count', 'where-did-they-go-from-here' ),
|
||||
'customize_selective_refresh' => true,
|
||||
'classname' => 'tptn_posts_count_widget',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Back-end widget form.
|
||||
*
|
||||
* @see WP_Widget::form()
|
||||
*
|
||||
* @param array $instance Previously saved values from database.
|
||||
*/
|
||||
public function form( $instance ) {
|
||||
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
|
||||
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
|
||||
<?php esc_html_e( 'Title', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Fires after Top 10 widget options.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $instance Widget options array
|
||||
*/
|
||||
do_action( 'tptn_widget_options_after', $instance );
|
||||
?>
|
||||
|
||||
<?php
|
||||
} //ending form creation
|
||||
|
||||
/**
|
||||
* Sanitize widget form values as they are saved.
|
||||
*
|
||||
* @see WP_Widget::update()
|
||||
*
|
||||
* @param array $new_instance Values just sent to be saved.
|
||||
* @param array $old_instance Previously saved values from database.
|
||||
*
|
||||
* @return array Updated safe values to be saved.
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
$instance = $old_instance;
|
||||
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
|
||||
|
||||
/**
|
||||
* Filters Update widget options array.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $instance Widget options array
|
||||
*/
|
||||
return apply_filters( 'tptn_widget_options_update', $instance );
|
||||
} //ending update
|
||||
|
||||
/**
|
||||
* Front-end display of widget.
|
||||
*
|
||||
* @see WP_Widget::widget()
|
||||
*
|
||||
* @param array $args Widget arguments.
|
||||
* @param array $instance Saved values from database.
|
||||
*/
|
||||
public function widget( $args, $instance ) {
|
||||
|
||||
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'] );
|
||||
|
||||
$output = $args['before_widget'];
|
||||
$output .= $args['before_title'] . $title . $args['after_title'];
|
||||
|
||||
$output .= get_tptn_post_count_only( 1, 'overall', 0 );
|
||||
|
||||
$output .= $args['after_widget'];
|
||||
|
||||
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
} //ending function widget
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the widget.
|
||||
*/
|
||||
function tptn_register_count_widget() {
|
||||
register_widget( 'Top_Ten_Count_Widget' );
|
||||
}
|
||||
add_action( 'widgets_init', 'tptn_register_count_widget', 1 );
|
||||
|
@@ -0,0 +1,334 @@
|
||||
<?php
|
||||
/**
|
||||
* This class adds REST routes to update the count and return the list of popular posts.
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query API: Top_Ten_Query class.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
class Top_Ten_REST_API extends \WP_REST_Controller {
|
||||
|
||||
/**
|
||||
* Main constructor.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->namespace = 'top-10/v1';
|
||||
$this->posts_route = 'popular-posts';
|
||||
$this->tracker_route = 'tracker';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the Top 10 REST API adding the necessary routes.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->posts_route,
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'permissions_check' ),
|
||||
'args' => $this->get_items_params(),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->posts_route . '/(?P<id>[\d]+)',
|
||||
array(
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'permissions_check' ),
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Post ID.', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->tracker_route,
|
||||
array(
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => array( $this, 'update_post_count' ),
|
||||
'permission_callback' => array( $this, 'permissions_check' ),
|
||||
'args' => $this->get_tracker_params(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to get items
|
||||
*
|
||||
* @param WP_REST_Request $request Full data about the request.
|
||||
*
|
||||
* @return WP_Error|bool
|
||||
*/
|
||||
public function permissions_check( $request ) {
|
||||
return apply_filters( 'top_ten_rest_api_permissions_check', true, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get popular posts.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_REST_Request $request WP Rest request.
|
||||
* @return mixed|\WP_REST_Response Array of post objects or post IDs.
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$popular_posts = array();
|
||||
|
||||
$args = $request->get_params();
|
||||
|
||||
/**
|
||||
* Filter the REST API arguments before they passed to get_tptn_posts().
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param array $args Arguments array.
|
||||
* @param WP_REST_Request $request WP Rest request.
|
||||
*/
|
||||
$args = apply_filters( 'top_ten_rest_api_get_tptn_posts_args', $args, $request );
|
||||
|
||||
$results = get_tptn_posts( $args );
|
||||
|
||||
if ( is_array( $results ) && ! empty( $results ) ) {
|
||||
foreach ( $results as $popular_post ) {
|
||||
if ( ! $this->check_read_permission( $popular_post ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$popular_posts[] = $this->prepare_item( $popular_post, $request );
|
||||
}
|
||||
}
|
||||
return rest_ensure_response( $popular_posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a popular post by ID. Also includes the number of views.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_REST_Request $request WP Rest request.
|
||||
* @return mixed|\WP_REST_Response Array of post objects or post IDs.
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
|
||||
$id = $request->get_param( 'id' );
|
||||
|
||||
$error = new WP_Error(
|
||||
'rest_post_invalid_id',
|
||||
__( 'Invalid post ID.', 'top-10' ),
|
||||
array( 'status' => 404 )
|
||||
);
|
||||
|
||||
if ( (int) $id <= 0 ) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
$post = get_post( (int) $id );
|
||||
if ( empty( $post ) || empty( $post->ID ) || ! $this->check_read_permission( $post ) ) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
$post = $this->prepare_item( $post, $request );
|
||||
|
||||
return rest_ensure_response( $post );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a popular post by ID. Also includes the number of views.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_Post $popular_post Popular Post object.
|
||||
* @param WP_REST_Request $request WP Rest request.
|
||||
* @return array|mixed The formatted Popular Post object.
|
||||
*/
|
||||
public function prepare_item( $popular_post, $request ) {
|
||||
|
||||
// Need to prepare items for the rest response.
|
||||
$posts_controller = new \WP_REST_Posts_Controller( $popular_post->post_type, $request );
|
||||
$data = $posts_controller->prepare_item_for_response( $popular_post, $request );
|
||||
|
||||
// Add pageviews from popular_post object to response.
|
||||
$visits = isset( $popular_post->visits ) ? $popular_post->visits : get_tptn_post_count_only( $popular_post->ID );
|
||||
$data->data['visits'] = absint( $visits );
|
||||
|
||||
return $this->prepare_response_for_collection( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update post count.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_REST_Request $request WP Rest request.
|
||||
* @return mixed|\WP_REST_Response Array of post objects or post IDs.
|
||||
*/
|
||||
public function update_post_count( $request ) {
|
||||
|
||||
$id = absint( $request->get_param( 'top_ten_id' ) );
|
||||
$blog_id = absint( $request->get_param( 'top_ten_blog_id' ) );
|
||||
$activate_counter = absint( $request->get_param( 'activate_counter' ) );
|
||||
$top_ten_debug = absint( $request->get_param( 'top_ten_debug' ) );
|
||||
|
||||
$str = tptn_update_count( $id, $blog_id, $activate_counter );
|
||||
|
||||
if ( 1 === $top_ten_debug ) {
|
||||
return rest_ensure_response( $str );
|
||||
} else {
|
||||
$response = new \WP_REST_Response( '', 204 );
|
||||
$response->header( 'Cache-Control', 'max-age=15, s-maxage=0' );
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the arguments for fetching the popular posts.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @return array Top 10 REST API popular posts arguments.
|
||||
*/
|
||||
public function get_items_params() {
|
||||
$args = array(
|
||||
'limit' => array(
|
||||
'description' => esc_html__( 'Number of posts', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'post_types' => array(
|
||||
'description' => esc_html__( 'Post types', 'top-10' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
);
|
||||
|
||||
return apply_filters( 'top_ten_rest_api_get_items_params', $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the arguments for tracking posts.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @return array Top 10 REST API popular posts arguments.
|
||||
*/
|
||||
public function get_tracker_params() {
|
||||
$args = array(
|
||||
'top_ten_id' => array(
|
||||
'description' => esc_html__( 'ID of the post.', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'top_ten_blog_id' => array(
|
||||
'description' => esc_html__( 'Blog ID of the post.', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'activate_counter' => array(
|
||||
'description' => esc_html__( 'Activate counter flag.', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'top_ten_debug' => array(
|
||||
'description' => esc_html__( 'Debug flag.', 'top-10' ),
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
);
|
||||
|
||||
return apply_filters( 'top_ten_rest_api_get_tracker_params', $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given post type can be viewed or managed.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_Post_Type|string $post_type Post type name or object.
|
||||
* @return bool Whether the post type is allowed in REST.
|
||||
*/
|
||||
protected function check_is_post_type_allowed( $post_type ) {
|
||||
if ( ! is_object( $post_type ) ) {
|
||||
$post_type = get_post_type_object( $post_type );
|
||||
}
|
||||
|
||||
if ( ! empty( $post_type ) && ! empty( $post_type->show_in_rest ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a post can be read.
|
||||
*
|
||||
* Correctly handles posts with the inherit status.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @return bool Whether the post can be read.
|
||||
*/
|
||||
public function check_read_permission( $post ) {
|
||||
$post_type = get_post_type_object( $post->post_type );
|
||||
if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is the post readable?
|
||||
if ( 'publish' === $post->post_status || current_user_can( 'read_post', $post->ID ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$post_status_obj = get_post_status_object( $post->post_status );
|
||||
if ( $post_status_obj && $post_status_obj->public ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Can we read the parent if we're inheriting?
|
||||
if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
|
||||
$parent = get_post( $post->post_parent );
|
||||
if ( $parent ) {
|
||||
return $this->check_read_permission( $parent );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If there isn't a parent, but the status is set to inherit, assume
|
||||
* it's published (as per get_post_status()).
|
||||
*/
|
||||
if ( 'inherit' === $post->post_status ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to register our new routes from the controller.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
function tptn_register_rest_routes() {
|
||||
$controller = new Top_Ten_REST_API();
|
||||
$controller->register_routes();
|
||||
}
|
||||
add_action( 'rest_api_init', 'tptn_register_rest_routes' );
|
@@ -0,0 +1,387 @@
|
||||
<?php
|
||||
/**
|
||||
* Widget class.
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Top 10 Widget.
|
||||
*
|
||||
* @extends WP_Widget
|
||||
*/
|
||||
class Top_Ten_Widget extends WP_Widget {
|
||||
|
||||
/**
|
||||
* Register widget with WordPress.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'widget_tptn_pop', // Base ID.
|
||||
__( 'Popular Posts [Top 10]', 'top-10' ), // Name.
|
||||
array(
|
||||
'description' => __( 'Display popular posts', 'where-did-they-go-from-here' ),
|
||||
'customize_selective_refresh' => true,
|
||||
'classname' => 'tptn_posts_list_widget',
|
||||
)
|
||||
);
|
||||
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'front_end_styles' ) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Back-end widget form.
|
||||
*
|
||||
* @see WP_Widget::form()
|
||||
*
|
||||
* @param array $instance Previously saved values from database.
|
||||
*/
|
||||
public function form( $instance ) {
|
||||
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
|
||||
$limit = isset( $instance['limit'] ) ? esc_attr( $instance['limit'] ) : '';
|
||||
$offset = isset( $instance['offset'] ) ? esc_attr( $instance['offset'] ) : '';
|
||||
$disp_list_count = isset( $instance['disp_list_count'] ) ? esc_attr( $instance['disp_list_count'] ) : '';
|
||||
$show_excerpt = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : '';
|
||||
$show_author = isset( $instance['show_author'] ) ? esc_attr( $instance['show_author'] ) : '';
|
||||
$show_date = isset( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
|
||||
$post_thumb_op = isset( $instance['post_thumb_op'] ) ? esc_attr( $instance['post_thumb_op'] ) : 'text_only';
|
||||
$thumb_height = isset( $instance['thumb_height'] ) ? esc_attr( $instance['thumb_height'] ) : '';
|
||||
$thumb_width = isset( $instance['thumb_width'] ) ? esc_attr( $instance['thumb_width'] ) : '';
|
||||
$daily = isset( $instance['daily'] ) ? esc_attr( $instance['daily'] ) : 'overall';
|
||||
$daily_range = isset( $instance['daily_range'] ) ? esc_attr( $instance['daily_range'] ) : '';
|
||||
$hour_range = isset( $instance['hour_range'] ) ? esc_attr( $instance['hour_range'] ) : '';
|
||||
$include_categories = isset( $instance['include_categories'] ) ? esc_attr( $instance['include_categories'] ) : '';
|
||||
$include_cat_ids = isset( $instance['include_cat_ids'] ) ? esc_attr( $instance['include_cat_ids'] ) : '';
|
||||
$include_post_ids = isset( $instance['include_post_ids'] ) ? esc_attr( $instance['include_post_ids'] ) : '';
|
||||
|
||||
// Parse the Post types.
|
||||
$post_types = array();
|
||||
if ( isset( $instance['post_types'] ) ) {
|
||||
$post_types = $instance['post_types'];
|
||||
parse_str( $post_types, $post_types ); // Save post types in $post_types variable.
|
||||
}
|
||||
$wp_post_types = get_post_types(
|
||||
array(
|
||||
'public' => true,
|
||||
)
|
||||
);
|
||||
$posts_types_inc = array_intersect( $wp_post_types, $post_types );
|
||||
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
|
||||
<?php esc_html_e( 'Title', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>">
|
||||
<?php esc_html_e( 'No. of posts', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" type="text" value="<?php echo esc_attr( $limit ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>">
|
||||
<?php esc_html_e( 'Offset', 'top-10' ); ?>: <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'offset' ) ); ?>" type="text" value="<?php echo esc_attr( $offset ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'daily' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'daily' ) ); ?>">
|
||||
<option value="overall" <?php selected( 'overall', $daily, true ); ?>><?php esc_html_e( 'Overall', 'top-10' ); ?></option>
|
||||
<option value="daily" <?php selected( 'daily', $daily, true ); ?>><?php esc_html_e( 'Custom time period (Enter below)', 'top-10' ); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<?php esc_html_e( 'In days and hours (applies only to custom option above)', 'top-10' ); ?>:
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'daily_range' ) ); ?>">
|
||||
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'daily_range' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'daily_range' ) ); ?>" type="text" value="<?php echo esc_attr( $daily_range ); ?>" /> <?php esc_html_e( 'days', 'top-10' ); ?>
|
||||
</label>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'hour_range' ) ); ?>">
|
||||
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hour_range' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hour_range' ) ); ?>" type="text" value="<?php echo esc_attr( $hour_range ); ?>" /> <?php esc_html_e( 'hours', 'top-10' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'disp_list_count' ) ); ?>">
|
||||
<input id="<?php echo esc_attr( $this->get_field_id( 'disp_list_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'disp_list_count' ) ); ?>" type="checkbox" <?php checked( true, $disp_list_count, true ); ?> /> <?php esc_html_e( 'Show count?', 'top-10' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>">
|
||||
<input id="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_excerpt' ) ); ?>" type="checkbox" <?php checked( true, $show_excerpt, true ); ?> /> <?php esc_html_e( 'Show excerpt?', 'top-10' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>">
|
||||
<input id="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" type="checkbox" <?php checked( true, $show_author, true ); ?> /> <?php esc_html_e( 'Show author?', 'top-10' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>">
|
||||
<input id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" type="checkbox" <?php checked( true, $show_date, true ); ?> /> <?php esc_html_e( 'Show date?', 'top-10' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<?php esc_html_e( 'Thumbnail options', 'top-10' ); ?>: <br />
|
||||
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'post_thumb_op' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_thumb_op' ) ); ?>">
|
||||
<option value="inline" <?php selected( 'inline', $post_thumb_op, true ); ?>><?php esc_html_e( 'Thumbnails inline, before title', 'top-10' ); ?></option>
|
||||
<option value="after" <?php selected( 'after', $post_thumb_op, true ); ?>><?php esc_html_e( 'Thumbnails inline, after title', 'top-10' ); ?></option>
|
||||
<option value="thumbs_only" <?php selected( 'thumbs_only', $post_thumb_op, true ); ?>><?php esc_html_e( 'Only thumbnails, no text', 'top-10' ); ?></option>
|
||||
<option value="text_only" <?php selected( 'text_only', $post_thumb_op, true ); ?>><?php esc_html_e( 'No thumbnails, only text.', 'top-10' ); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>">
|
||||
<?php esc_html_e( 'Thumbnail height', 'top-10' ); ?>:
|
||||
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_height' ) ); ?>" type="text" value="<?php echo esc_attr( $thumb_height ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>">
|
||||
<?php esc_html_e( 'Thumbnail width', 'top-10' ); ?>:
|
||||
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_width' ) ); ?>" type="text" value="<?php echo esc_attr( $thumb_width ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'include_categories' ) ); ?>">
|
||||
<?php esc_html_e( 'Only from categories', 'top-10' ); ?>:
|
||||
<input class="widefat category_autocomplete" id="<?php echo esc_attr( $this->get_field_id( 'include_categories' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'include_categories' ) ); ?>" type="text" value="<?php echo esc_attr( $include_categories ); ?>" />
|
||||
</label>
|
||||
<input type="hidden" id="<?php echo esc_attr( $this->get_field_id( 'include_cat_ids' ) ); ?>" name="<?php echo esc_attr( $this->get_field_id( 'include_cat_ids' ) ); ?>" value="<?php echo esc_attr( $include_cat_ids ); ?>" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'include_post_ids' ) ); ?>">
|
||||
<?php esc_html_e( 'Include IDs', 'top-10' ); ?>:
|
||||
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'include_post_ids' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'include_post_ids' ) ); ?>" type="text" value="<?php echo esc_attr( $include_post_ids ); ?>" />
|
||||
</label>
|
||||
</p>
|
||||
<p><?php esc_html_e( 'Post types to include:', 'top-10' ); ?><br />
|
||||
|
||||
<?php foreach ( $wp_post_types as $wp_post_type ) { ?>
|
||||
|
||||
<label>
|
||||
<input id="<?php echo esc_attr( $this->get_field_id( 'post_types' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]" type="checkbox" value="<?php echo esc_attr( $wp_post_type ); ?>" <?php checked( true, in_array( $wp_post_type, $posts_types_inc, true ), true ); ?> />
|
||||
<?php echo esc_attr( $wp_post_type ); ?>
|
||||
</label>
|
||||
<br />
|
||||
|
||||
<?php } ?>
|
||||
</p>
|
||||
|
||||
<?php
|
||||
/**
|
||||
* Fires after Top 10 widget options.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $instance Widget options array
|
||||
*/
|
||||
do_action( 'tptn_widget_options_after', $instance );
|
||||
?>
|
||||
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize widget form values as they are saved.
|
||||
*
|
||||
* @see WP_Widget::update()
|
||||
*
|
||||
* @param array $new_instance Values just sent to be saved.
|
||||
* @param array $old_instance Previously saved values from database.
|
||||
*
|
||||
* @return array Updated safe values to be saved.
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
$instance = $old_instance;
|
||||
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
|
||||
$instance['limit'] = $new_instance['limit'];
|
||||
$instance['offset'] = $new_instance['offset'];
|
||||
$instance['daily'] = $new_instance['daily'];
|
||||
$instance['daily_range'] = $new_instance['daily_range'];
|
||||
$instance['hour_range'] = $new_instance['hour_range'];
|
||||
$instance['disp_list_count'] = isset( $new_instance['disp_list_count'] ) ? true : false;
|
||||
$instance['show_excerpt'] = isset( $new_instance['show_excerpt'] ) ? true : false;
|
||||
$instance['show_author'] = isset( $new_instance['show_author'] ) ? true : false;
|
||||
$instance['show_date'] = isset( $new_instance['show_date'] ) ? true : false;
|
||||
$instance['post_thumb_op'] = $new_instance['post_thumb_op'];
|
||||
$instance['thumb_height'] = $new_instance['thumb_height'];
|
||||
$instance['thumb_width'] = $new_instance['thumb_width'];
|
||||
$instance['include_post_ids'] = implode( ',', array_filter( array_map( 'absint', explode( ',', $new_instance['include_post_ids'] ) ) ) );
|
||||
|
||||
// Process post types to be selected.
|
||||
$wp_post_types = get_post_types(
|
||||
array(
|
||||
'public' => true,
|
||||
)
|
||||
);
|
||||
$post_types = ( isset( $new_instance['post_types'] ) ) ? $new_instance['post_types'] : array();
|
||||
$post_types = array_intersect( $wp_post_types, $post_types );
|
||||
$instance['post_types'] = http_build_query( $post_types, '', '&' );
|
||||
|
||||
// Save include_categories.
|
||||
$include_categories = array_unique( str_getcsv( $new_instance['include_categories'] ) );
|
||||
|
||||
foreach ( $include_categories as $cat_name ) {
|
||||
$cat = get_term_by( 'name', $cat_name, 'category' );
|
||||
|
||||
if ( isset( $cat->term_taxonomy_id ) ) {
|
||||
$include_cat_ids[] = $cat->term_taxonomy_id;
|
||||
$include_cat_names[] = $cat->name;
|
||||
}
|
||||
}
|
||||
$instance['include_cat_ids'] = isset( $include_cat_ids ) ? join( ',', $include_cat_ids ) : '';
|
||||
$instance['include_categories'] = isset( $include_cat_names ) ? tptn_str_putcsv( $include_cat_names ) : '';
|
||||
|
||||
/**
|
||||
* Filters Update widget options array.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $instance Widget options array
|
||||
*/
|
||||
return apply_filters( 'tptn_widget_options_update', $instance );
|
||||
}
|
||||
|
||||
/**
|
||||
* Front-end display of widget.
|
||||
*
|
||||
* @see WP_Widget::widget()
|
||||
*
|
||||
* @param array $args Widget arguments.
|
||||
* @param array $instance Saved values from database.
|
||||
*/
|
||||
public function widget( $args, $instance ) {
|
||||
global $post;
|
||||
|
||||
// Get the post meta.
|
||||
if ( isset( $post ) ) {
|
||||
$tptn_post_meta = get_post_meta( $post->ID, 'tptn_post_meta', true );
|
||||
|
||||
if ( isset( $tptn_post_meta['disable_here'] ) && ( $tptn_post_meta['disable_here'] ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? wp_strip_all_tags( tptn_get_option( 'title' ) ) : $instance['title'] );
|
||||
|
||||
$limit = isset( $instance['limit'] ) ? $instance['limit'] : tptn_get_option( 'limit' );
|
||||
if ( empty( $limit ) ) {
|
||||
$limit = tptn_get_option( 'limit' );
|
||||
}
|
||||
|
||||
$offset = isset( $instance['offset'] ) ? $instance['offset'] : 0;
|
||||
$daily_range = ( empty( $instance['daily_range'] ) ) ? tptn_get_option( 'daily_range' ) : $instance['daily_range'];
|
||||
$hour_range = ( empty( $instance['hour_range'] ) ) ? tptn_get_option( 'hour_range' ) : $instance['hour_range'];
|
||||
|
||||
$daily = ( isset( $instance['daily'] ) && ( 'daily' === $instance['daily'] ) ) ? true : false;
|
||||
|
||||
$output = $args['before_widget'];
|
||||
$output .= $args['before_title'] . $title . $args['after_title'];
|
||||
|
||||
$post_thumb_op = isset( $instance['post_thumb_op'] ) ? esc_attr( $instance['post_thumb_op'] ) : 'text_only';
|
||||
|
||||
$thumb_height = ( isset( $instance['thumb_height'] ) && '' !== $instance['thumb_height'] ) ? absint( $instance['thumb_height'] ) : tptn_get_option( 'thumb_height' );
|
||||
$thumb_width = ( isset( $instance['thumb_width'] ) && '' !== $instance['thumb_width'] ) ? absint( $instance['thumb_width'] ) : tptn_get_option( 'thumb_width' );
|
||||
|
||||
$disp_list_count = isset( $instance['disp_list_count'] ) ? esc_attr( $instance['disp_list_count'] ) : '';
|
||||
$show_excerpt = isset( $instance['show_excerpt'] ) ? esc_attr( $instance['show_excerpt'] ) : '';
|
||||
$show_author = isset( $instance['show_author'] ) ? esc_attr( $instance['show_author'] ) : '';
|
||||
$show_date = isset( $instance['show_date'] ) ? esc_attr( $instance['show_date'] ) : '';
|
||||
$post_types = isset( $instance['post_types'] ) ? $instance['post_types'] : tptn_get_option( 'post_types' );
|
||||
$include_cat_ids = isset( $instance['include_cat_ids'] ) ? esc_attr( $instance['include_cat_ids'] ) : '';
|
||||
$include_post_ids = isset( $instance['include_post_ids'] ) ? esc_attr( $instance['include_post_ids'] ) : '';
|
||||
|
||||
$arguments = array(
|
||||
'is_widget' => 1,
|
||||
'instance_id' => $this->number,
|
||||
'heading' => 0,
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
'daily' => $daily,
|
||||
'daily_range' => $daily_range,
|
||||
'hour_range' => $hour_range,
|
||||
'show_excerpt' => $show_excerpt,
|
||||
'show_author' => $show_author,
|
||||
'show_date' => $show_date,
|
||||
'post_thumb_op' => $post_thumb_op,
|
||||
'thumb_height' => $thumb_height,
|
||||
'thumb_width' => $thumb_width,
|
||||
'disp_list_count' => $disp_list_count,
|
||||
'post_types' => $post_types,
|
||||
'include_cat_ids' => $include_cat_ids,
|
||||
'include_post_ids' => $include_post_ids,
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters arguments passed to tptn_pop_posts for the widget.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param array $arguments Widget options array
|
||||
*/
|
||||
$arguments = apply_filters( 'tptn_widget_options', $arguments );
|
||||
|
||||
$output .= tptn_pop_posts( $arguments );
|
||||
|
||||
$output .= $args['after_widget'];
|
||||
|
||||
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add styles to the front end if the widget is active.
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function front_end_styles() {
|
||||
|
||||
if ( ! 'left_thumbs' === tptn_get_option( 'tptn_styles' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We need to process all instances because this function gets to run only once.
|
||||
$widget_settings = get_option( $this->option_name );
|
||||
|
||||
foreach ( (array) $widget_settings as $instance => $options ) {
|
||||
|
||||
// Identify instance.
|
||||
$widget_id = "{$this->id_base}-{$instance}";
|
||||
|
||||
// Check if it's our instance.
|
||||
if ( ! is_active_widget( false, $widget_id, $this->id_base, true ) ) {
|
||||
continue; // Not active.
|
||||
}
|
||||
|
||||
$thumb_height = ( isset( $options['thumb_height'] ) && '' !== $options['thumb_height'] ) ? absint( $options['thumb_height'] ) : tptn_get_option( 'thumb_height' );
|
||||
$thumb_width = ( isset( $options['thumb_width'] ) && '' !== $options['thumb_width'] ) ? absint( $options['thumb_width'] ) : tptn_get_option( 'thumb_width' );
|
||||
|
||||
// Enqueue the custom css for the thumb width and height for this specific widget.
|
||||
$custom_css = "
|
||||
.tptn_posts_widget{$instance} img.tptn_thumb {
|
||||
width: {$thumb_width}px !important;
|
||||
height: {$thumb_height}px !important;
|
||||
}
|
||||
";
|
||||
|
||||
wp_add_inline_style( 'tptn-style-left-thumbs', $custom_css );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the widget.
|
||||
*/
|
||||
function tptn_register_widget() {
|
||||
register_widget( 'Top_Ten_Widget' );
|
||||
}
|
||||
add_action( 'widgets_init', 'tptn_register_widget', 1 );
|
||||
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Exclusion modules
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
/**
|
||||
* Add additional post IDs to exclude. Filters `tptn_exclude_post_ids`.
|
||||
*
|
||||
* @since 2.2.0
|
||||
*
|
||||
* @param int[] $exclude_post_ids Original excluded post IDs.
|
||||
* @return int[] Updated excluded post IDs array.
|
||||
*/
|
||||
function tptn_exclude_post_ids( $exclude_post_ids ) {
|
||||
global $wpdb;
|
||||
|
||||
$exclude_post_ids = (array) $exclude_post_ids;
|
||||
|
||||
// Find all posts that have `exclude_this_post` set.
|
||||
$tptn_post_metas = $wpdb->get_results( "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE `meta_key` = 'tptn_post_meta'", ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
|
||||
|
||||
foreach ( $tptn_post_metas as $tptn_post_meta ) {
|
||||
$meta_value = maybe_unserialize( $tptn_post_meta['meta_value'] );
|
||||
|
||||
if ( $meta_value['exclude_this_post'] ) {
|
||||
$exclude_post_ids[] = $tptn_post_meta['post_id'];
|
||||
}
|
||||
}
|
||||
|
||||
// Exclude page_on_front and page_for_posts.
|
||||
if ( 'page' === get_option( 'show_on_front' ) && tptn_get_option( 'exclude_front' ) ) {
|
||||
$page_on_front = get_option( 'page_on_front' );
|
||||
$page_for_posts = get_option( 'page_for_posts' );
|
||||
if ( $page_on_front > 0 ) {
|
||||
$exclude_post_ids[] = $page_on_front;
|
||||
}
|
||||
if ( $page_for_posts > 0 ) {
|
||||
$exclude_post_ids[] = $page_for_posts;
|
||||
}
|
||||
}
|
||||
|
||||
return $exclude_post_ids;
|
||||
}
|
||||
add_filter( 'tptn_exclude_post_ids', 'tptn_exclude_post_ids' );
|
||||
|
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden
|
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Shortcode module
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a shortcode [tptn_list limit="5" heading="1" daily="0"].
|
||||
*
|
||||
* @since 1.9.9
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @param string $content Content.
|
||||
* @return string Formatted list of posts generated by tptn_pop_posts
|
||||
*/
|
||||
function tptn_shortcode( $atts, $content = null ) {
|
||||
global $tptn_settings;
|
||||
|
||||
$atts = shortcode_atts(
|
||||
array_merge(
|
||||
$tptn_settings,
|
||||
array(
|
||||
'heading' => 1,
|
||||
'daily' => 0,
|
||||
'is_shortcode' => 1,
|
||||
'offset' => 0,
|
||||
'include_cat_ids' => '',
|
||||
)
|
||||
),
|
||||
$atts,
|
||||
'top-10'
|
||||
);
|
||||
|
||||
return tptn_pop_posts( $atts );
|
||||
}
|
||||
add_shortcode( 'tptn_list', 'tptn_shortcode' );
|
||||
|
||||
|
||||
/**
|
||||
* Creates a shortcode [tptn_views daily="0"].
|
||||
*
|
||||
* @since 1.9.9
|
||||
* @param array $atts Shortcode attributes.
|
||||
* @param string $content Content.
|
||||
* @return string Views of the post
|
||||
*/
|
||||
function tptn_shortcode_views( $atts, $content = null ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
|
||||
$a = shortcode_atts(
|
||||
array(
|
||||
'daily' => '0',
|
||||
'count' => 'total',
|
||||
),
|
||||
$atts
|
||||
);
|
||||
|
||||
// If daily is explicitly set to 1, then pass daily, else pass count.
|
||||
$count = $a['daily'] ? 'daily' : $a['count'];
|
||||
|
||||
return get_tptn_post_count_only( get_the_ID(), $count );
|
||||
}
|
||||
add_shortcode( 'tptn_views', 'tptn_shortcode_views' );
|
||||
|
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Taxonomies control module
|
||||
*
|
||||
* @package Top_Ten
|
||||
*/
|
||||
|
||||
// If this file is called directly, abort.
|
||||
if ( ! defined( 'WPINC' ) ) {
|
||||
die;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter WHERE clause of tptn query to exclude posts belonging to certain categories.
|
||||
*
|
||||
* @since 2.2.0
|
||||
*
|
||||
* @param mixed $where WHERE clause.
|
||||
* @return string Filtered WHERE clause
|
||||
*/
|
||||
function tptn_exclude_categories_where( $where ) {
|
||||
global $wpdb, $tptn_settings;
|
||||
|
||||
if ( '' === tptn_get_option( 'exclude_categories' ) ) {
|
||||
return $where;
|
||||
} else {
|
||||
|
||||
$terms = tptn_get_option( 'exclude_categories' );
|
||||
|
||||
$sql = $where;
|
||||
|
||||
$sql .= " AND $wpdb->posts.ID NOT IN (
|
||||
SELECT object_id
|
||||
FROM $wpdb->term_relationships
|
||||
WHERE term_taxonomy_id IN ($terms)
|
||||
)";
|
||||
|
||||
return $sql;
|
||||
}
|
||||
|
||||
}
|
||||
add_filter( 'tptn_posts_where', 'tptn_exclude_categories_where' );
|
||||
|
Reference in New Issue
Block a user