diff --git a/featured-post.php b/featured-post.php index 019e909..22a47fd 100644 --- a/featured-post.php +++ b/featured-post.php @@ -1,12 +1,12 @@ . -**************************************************************************/ + **************************************************************************/ class Featured_Post { - var $db = NULL; - public $post_types = array(); - function __construct() { + public $post_types = array(); + private static $instance = null; + + public function __construct() + { add_action('init', array(&$this, - 'init' + 'init', )); add_action('admin_init', array(&$this, - 'admin_init' + 'admin_init', )); add_action('wp_ajax_toggle-featured-post', array(&$this, - 'admin_ajax' + 'admin_ajax', )); - add_action( 'plugins_loaded', array(&$this, - 'load_featured_textdomain' + add_action('plugins_loaded', array(&$this, + 'load_featured_textdomain', )); } - function init() { + public static function get_instance() + { + if (null == self::$instance) { + self::$instance = new self(); + } + return self::$instance; + } + public function init() + { add_filter('query_vars', array(&$this, - 'query_vars' + 'query_vars', )); add_action('pre_get_posts', array(&$this, - 'pre_get_posts' + 'pre_get_posts', )); } - function admin_init() { - /* Set the new post to 'featured=no' when it's created */ + public function admin_init() + { + /* Set the new post to 'featured=no' when it's created */ add_action('new_to_publish', array(&$this, - 'set_not_featured' + 'set_not_featured', ), 1, 2); add_action('draft_to_publish', array(&$this, - 'set_not_featured' + 'set_not_featured', ), 1, 2); add_action('pending_to_publish', array(&$this, - 'set_not_featured' + 'set_not_featured', ), 1, 2); add_filter('current_screen', array(&$this, - 'my_current_screen' + 'my_current_screen', )); add_action('admin_head-edit.php', array(&$this, - 'admin_head' + 'admin_head', )); add_filter('pre_get_posts', array(&$this, - 'admin_pre_get_posts' - ) , 1); + 'admin_pre_get_posts', + ), 1); $this->post_types = get_post_types(array( '_builtin' => false, - ) , 'names', 'or'); + ), 'names', 'or'); $this->post_types['post'] = 'post'; $this->post_types['page'] = 'page'; ksort($this->post_types); + $this->post_types = apply_filters('featured_post_types', $this->post_types); + foreach ($this->post_types as $key => $val) { add_filter('manage_edit-' . $key . '_columns', array(&$this, - 'manage_posts_columns' + 'manage_posts_columns', )); add_action('manage_' . $key . '_posts_custom_column', array(&$this, - 'manage_posts_custom_column' - ) , 10, 2); + 'manage_posts_custom_column', + ), 10, 2); } - add_action( 'post_submitbox_misc_actions', array(&$this, - 'edit_screen_featured_ui' + add_action('post_submitbox_misc_actions', array(&$this, + 'edit_screen_featured_ui', )); - add_action( 'save_post', array(&$this, - 'edit_screen_featured_save' + add_action('save_post', array(&$this, + 'edit_screen_featured_save', )); } - function add_views_link($views) { - $post_type = ((isset($_GET['post_type']) && $_GET['post_type'] != "") ? $_GET['post_type'] : 'post'); - $count = $this->total_featured($post_type); - $class = (isset($_GET['post_status']) && $_GET['post_status'] == 'featured') ? "current" : ''; - $views['featured'] = "" . __( 'Featured', 'featured-post' ) . "({$count})"; + public function add_views_link($views) + { + $post_type = ((isset($_GET['post_type']) && $_GET['post_type'] != "") ? $_GET['post_type'] : 'post'); + $count = $this->total_featured($post_type); + $class = (isset($_GET['post_status']) && $_GET['post_status'] == 'featured') ? "current" : ''; + $views['featured'] = "" . __('Featured', 'featured-post') . "({$count})"; return $views; } - function total_featured($post_type = "post") { + public function total_featured($post_type = "post") + { $rowQ = new WP_Query(array( - 'post_type' => $post_type, - 'meta_query' => array( + 'post_type' => $post_type, + 'meta_query' => array( array( - 'key' => '_is_featured', - 'value' => 'yes' - ) - ) , - 'posts_per_page' => 1 + 'key' => '_is_featured', + 'value' => 'yes', + ), + ), + 'posts_per_page' => 1, )); wp_reset_postdata(); wp_reset_query(); @@ -112,47 +127,51 @@ function total_featured($post_type = "post") { unset($rowQ); return $rows; } - function my_current_screen($screen) { + public function my_current_screen($screen) + { if (defined('DOING_AJAX') && DOING_AJAX) { return $screen; } $this->post_types = get_post_types(array( '_builtin' => false, - ) , 'names', 'or'); + ), 'names', 'or'); $this->post_types['post'] = 'post'; $this->post_types['page'] = 'page'; ksort($this->post_types); foreach ($this->post_types as $key => $val) { add_filter('views_edit-' . $key, array(&$this, - 'add_views_link' + 'add_views_link', )); } return $screen; } - function manage_posts_columns($columns) { + public function manage_posts_columns($columns) + { global $current_user; get_currentuserinfo(); if (current_user_can('edit_posts', $user_id)) { - $columns['featured'] = __('Featured', 'featured-post' ); + $columns['featured'] = __('Featured', 'featured-post'); } return $columns; } - function manage_posts_custom_column($column_name, $post_id) { + public function manage_posts_custom_column($column_name, $post_id) + { //echo "here"; if ($column_name == 'featured') { $is_featured = get_post_meta($post_id, '_is_featured', true); - $class = "dashicons"; - $text = ""; + $class = "dashicons"; + $text = ""; if ($is_featured == "yes") { - $class.= " dashicons-star-filled"; + $class .= " dashicons-star-filled"; $text = ""; } else { - $class.= " dashicons-star-empty"; + $class .= " dashicons-star-empty"; } echo "$text"; } } - function admin_head() { + public function admin_head() + { echo ''; } - function admin_ajax() { + public function admin_ajax() + { header('Content-Type: application/json'); - $post_id = $_POST['post_id']; + $post_id = $_POST['post_id']; $is_featured = get_post_meta($post_id, '_is_featured', true); - $newStatus = $is_featured == 'yes' ? 'no' : 'yes'; + $newStatus = $is_featured == 'yes' ? 'no' : 'yes'; delete_post_meta($post_id, '_is_featured'); add_post_meta($post_id, '_is_featured', $newStatus); echo json_encode(array( - 'ID' => $post_id, - 'new_status' => $newStatus, - 'total_featured' => $this->total_featured(get_post_type($post_id)) + 'ID' => $post_id, + 'new_status' => $newStatus, + 'total_featured' => $this->total_featured(get_post_type($post_id)), )); die(); - } + } /** * set_not_featured() * * Sets the value of 'featured' to 'no' right after the post creation */ - function set_not_featured($post_id) { + public function set_not_featured($post_id) + { add_post_meta($post_id, '_is_featured', 'no'); } - function admin_pre_get_posts($query) { + public function admin_pre_get_posts($query) + { global $wp_query; if (is_admin() && isset($_GET['post_status']) && $_GET['post_status'] == 'featured') { $query->set('meta_key', '_is_featured'); @@ -206,11 +228,13 @@ function admin_pre_get_posts($query) { } return $query; } - function query_vars($public_query_vars) { + public function query_vars($public_query_vars) + { $public_query_vars[] = 'featured'; return $public_query_vars; } - function pre_get_posts($query) { + public function pre_get_posts($query) + { if (!is_admin()) { if ($query->get('featured') == 'yes') { $query->set('meta_key', '_is_featured'); @@ -220,49 +244,55 @@ function pre_get_posts($query) { return $query; } - function edit_screen_featured_ui() { - // global $typenow; - if ( is_admin() ) { //Post types could be defined here ( $typenow == 'post' ) - echo '
' . "\n"; - echo '
' . "\n"; - } + public function edit_screen_featured_ui() + { + // global $typenow; + if (is_admin()) { + //Post types could be defined here ( $typenow == 'post' ) + echo '
' . "\n"; + echo '
' . "\n"; + } } - function edit_screen_featured_save( $post_id ) { - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + public function edit_screen_featured_save($post_id) + { + if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } - if ( !current_user_can( 'edit_post', $post_id ) ) { + if (!current_user_can('edit_post', $post_id)) { return; } - if ( isset( $_POST['featured'] ) ) { - update_post_meta( $post_id, '_is_featured', esc_attr( $_POST['featured'] ) ); + if (isset($_POST['featured'])) { + update_post_meta($post_id, '_is_featured', esc_attr($_POST['featured'])); } } - function load_featured_textdomain() { - load_plugin_textdomain( 'featured-post', false, dirname( plugin_basename( __FILE__ ) ) . '/langs/' ); + public function load_featured_textdomain() + { + load_plugin_textdomain('featured-post', false, dirname(plugin_basename(__FILE__)) . '/langs/'); } } +Featured_Post::get_instance(); + class Featured_Post_Widget extends WP_Widget { private $post_types = array(); - function __construct() { - parent::WP_Widget(false, $name = __( 'Featured Post', 'featured-post' ) ); + public function __construct() + { + parent::__construct(false, $name = __('Featured Post', 'featured-post')); } - function form($instance) { - $title = esc_attr($instance['title']); - $type = esc_attr($instance['post_type']); - $num = (int)esc_attr($instance['num']); - $this->post_types = get_post_types(array( - '_builtin' => false, - ) , 'names', 'or'); - $this->post_types['post'] = 'post'; - $this->post_types['page'] = 'page'; + public function form($instance) + { + $Featured_Post=Featured_Post::get_instance(); + $title = esc_attr($instance['title']); + $type = esc_attr($instance['post_type']); + $num = (int) esc_attr($instance['num']); + + $this->post_types = $Featured_Post->post_types; ksort($this->post_types); echo "

"; echo ""; echo "get_field_id('title') . "\" name=\"" . $this->get_field_name('title') . "\" type=\"text\" value=\"" . $title . "\" />"; echo "

"; @@ -283,17 +313,19 @@ function form($instance) { echo "get_field_id('num') . "\" class = \"widefat\" name = \"" . $this->get_field_name('num') . "\" type=\"text\" value =\"" . $num . "\" / >"; echo "

"; } - function update($new_instance, $old_instance) { - $instance = $old_instance; - $instance['title'] = strip_tags($new_instance['title']); - $instance['num'] = (int)strip_tags($new_instance['num']); + public function update($new_instance, $old_instance) + { + $instance = $old_instance; + $instance['title'] = strip_tags($new_instance['title']); + $instance['num'] = (int) strip_tags($new_instance['num']); $instance['post_type'] = strip_tags($new_instance['post_type']); if ($instance['num'] < 1) { $instance['num'] = 10; } return $instance; } - function widget($args, $instance) { + public function widget($args, $instance) + { extract($args); $title = apply_filters('widget_title', $instance['title']); echo $before_widget; @@ -303,12 +335,12 @@ function widget($args, $instance) { echo "