Skip to content

Instantly share code, notes, and snippets.

@newsapkota
Forked from danielpataki/auto-add.php
Created February 7, 2018 13:05
Show Gist options
  • Save newsapkota/56ace1ed6124f1f3b3a37cfc96bdc046 to your computer and use it in GitHub Desktop.
Save newsapkota/56ace1ed6124f1f3b3a37cfc96bdc046 to your computer and use it in GitHub Desktop.
OOP plugin
/*
Plugin Name: Auto Add Read Posts
Description: A plugin that adds read posts to the user's read list - requires the Unread Posts plugin
Version: 1.0.0
Author: Daniel Pataki
Author URI: http://danielpataki.com/
License: GPLv2 or later
*/
add_action( 'wp', 'auto_add_post_to_read_list' );
function auto_add_post_to_read_list() {
global $up_unread_posts;
if( is_singular() ) {
global $post;
$up_unread_posts->set_posts_as_read( $post->ID );
}
}
class UP_Unread_Posts_Handler_DB implements UP_Unread_Posts_Handler_Interface {
}
interface UP_Unread_Posts_Handler_Interface {
/**
* Set posts as read
*
* @param array|int $posts post/posts to set as read
* @author Daniel Pataki
* @since 1.0.0
*
*/
public function set_posts_as_read( $posts );
/**
* Sets posts as unread
*
* @param array|int $posts post/posts to set as unread
* @author Daniel Pataki
* @since 1.0.0
*
*/
public function set_posts_as_unread( $posts );
/**
* Get read posts
*
* @author Daniel Pataki
* @since 1.0.0
*
*/
public function get_read_posts();
/**
* Get unread posts
*
* @author Daniel Pataki
* @since 1.0.0
*
*/
public function get_unread_posts();
/**
* Delete read posts
*
* @author Daniel Pataki
* @since 1.0.0
*
*/
public function delete_read_posts();
}
function set_posts_as_read( $posts ) {
if( is_numeric( $posts ) ) {
$posts = array( $posts );
}
if( array_intersect( $this->read_posts, $posts ) == $posts ) {
return ;
}
$this->read_posts = array_unique( array_merge( $this->read_posts, $posts ) );
update_user_meta( $this->user_id, $this->meta_field, $this->read_posts );
}
class UP_Unread_Posts_Handler_DB extends UP_Unread_Posts_Handler implements UP_Unread_Posts_Handler_Interface {
public $meta_field;
public $user_id;
protected $read_posts;
function __construct() {
$this->set_meta_field();
$this->set_user_id();
$this->set_read_posts();
}
protected function set_meta_field() {
$this->meta_field = apply_filters( 'up/meta_field', 'up_read_posts' );
}
protected function set_user_id() {
$current_user = wp_get_current_user();
$this->user_id = $current_user->ID;
}
protected function set_read_posts() {
$read_posts = get_user_meta( $this->user_id, $this->meta_field, true );
$read_posts = ( empty( $read_posts ) ) ? array() : $read_posts;
$this->read_posts = $read_posts;
}
}
function set_posts_as_unread( $posts ) {
if( is_numeric( $posts ) ) {
$posts = array( $posts );
}
$this->read_posts = array_diff( $this->read_posts, $posts );
update_user_meta( $this->user_id, $this->meta_field, $this->read_posts );
}
function delete_read_posts() {
delete_user_meta( $this->user_id, $this->meta_field );
}
$type = 'cookie';
if( is_user_logged_in() $$ function_exists( 'some_api_plugins_function' ) ) {
$type = 'someapi';
}
elseif( is_user_logged_in() ) {
$type = 'db';
}
call_user_func( 'delete_read_posts_' . $type );
function get_read_posts() {
return $this->read_posts;
}
function get_unread_posts( $args = array() ) {
$defaults = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => $this->get_read_posts(),
'fields' => 'ids',
'posts_per_page' => -1,
'orderby' => 'DESC'
);
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'up/unread_query_args', $args );
$unread = new WP_Query( $args );
if( $unread->found_posts == 0 ) {
return array();
}
else {
return $unread;
}
}
class UP_Unread_Posts_Handler {
function get_unread_posts( $args = array() ) {}
function get_read_posts() {}
}
class UP_Unread_Posts_Handler_DB extends UP_Unread_Posts_Handler implements UP_Unread_Posts_Handler_Interface {
function set_posts_as_read( $posts ) {}
function set_posts_as_unread( $posts ) {}
function delete_read_posts() {}
}
class UP_Unread_Posts_Handler_Cookie extends UP_Unread_Posts_Handler implements UP_Unread_Posts_Handler_Interface {
function set_posts_as_read( $posts ) {}
function set_posts_as_unread( $posts ) {}
function delete_read_posts() {}
}
if( is_user_logged_in() ) {
$read_posts = UP_Unread_Posts_Handler_DB;
}
else {
$read_posts = UP_Unread_Posts_Handler_Cookie;
}
$read_posts->set_posts_as_read(12);
add_action( 'plugins_loaded', 'up_initialize_plugin' );
function up_initialize_plugin() {
global $up_unread_posts;
$up_unread_posts_handler = ( is_user_logged_in() ) ? "UP_Unread_Posts_Handler_DB" : "UP_Unread_Posts_Handler_Cookie";
add_filter( 'up/handler', $up_unread_posts_handler );
$up_unread = new UP_Unread_Posts( new $up_unread_posts_handler );
$up_unread_posts = $up_unread->handler;
}
class UP_Unread_Posts {
public $handler;
function __construct( UP_Unread_Posts_Handler_Interface $handler = null ) {
$this->handler = $handler ?: new UP_Unread_Posts_Handler_Cookie;
}
}
function get_read_posts() {
if( is_user_logged_in() ) {
// Get from DB
}
else {
// Get from Cookie
}
}
function delete_read_posts() {
if( is_user_logged_in() ) {
// Delete from DB
}
else {
// Delete the Cookie
}
}
/*
Plugin Name: Unread Posts Widget
Description: A plugin displays a list of unread posts
Version: 1.0.0
Author: Daniel Pataki
Author URI: http://danielpataki.com/
License: GPLv2 or later
*/
add_action( 'widgets_init', 'unread_posts_widget_init' )
function unread_posts_widget_init() {
register_widget( 'UP_Unread_Posts_Widget' );
}
class UP_Unread_Posts_Widget extends WP_Widget {
public function __construct() {
$widget_details = array(
'classname' => 'up-unread-posts-widget',
'description' => __( 'A customizable widget that displays unread posts', 'unread-posts' )
);
parent::__construct( 'up-unread-posts', __( 'Unread Posts', 'unread-posts' ), $widget_details );
}
public function form( $instance ) {
$title = ( !empty( $instance['title'] ) ) ? $instance['title'] : '';
?>
<div class='unread-posts'>
<p>
<label for="<?php echo $this->get_field_name( 'title' ); ?>"><?php _e( 'Title:', 'unread-posts' ) ?> </label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
</div>
<?php
}
public function widget( $args, $instance ) {
global $up_unread_posts;
$query_args = array(
'posts_per_page' => 5,
'fields' => 'all'
);
$unread = $up_unread_posts->get_unread_posts( $query_args );
if( $unread->found_posts == 0 ) {
return;
}
echo $args['before_widget'];
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
$output = '<ul>';
while( $unread->have_posts() ) {
$unread->the_post();
$output .= '<li><a href="' . get_permalink( get_the_ID() ) . '">' . the_title( '','', false) . '</a></li>';
}
$output .= '</ul>';
echo $args['after_widget'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment