From b25fc548339763776e86d7cf1aef5f7bb19c8e59 Mon Sep 17 00:00:00 2001 From: David Artiss Date: Mon, 3 Feb 2020 11:15:06 +0000 Subject: [PATCH] Version 2.4 --- includes/admin-config.php | 24 +++-- includes/generate-code.php | 165 ++++++++++++++++++---------------- includes/generate-widget.php | 53 +++++++---- includes/meta-box.php | 19 ++-- includes/shared-functions.php | 11 ++- readme.txt | 83 +++++++++-------- simple-draft-list.php | 81 ++++++++--------- 7 files changed, 238 insertions(+), 198 deletions(-) diff --git a/includes/admin-config.php b/includes/admin-config.php index 90a42fb..407ea5e 100755 --- a/includes/admin-config.php +++ b/includes/admin-config.php @@ -21,7 +21,16 @@ function adl_set_plugin_meta( $links, $file ) { - if ( strpos( $file, 'simple-draft-list.php' ) !== false ) { $links = array_merge( $links, array( '' . __( 'Support', 'simple-draft-list' ) . '' ) ); } + if ( strpos( $file, 'simple-draft-list.php' ) !== false ) { + + $links = array_merge( $links, array( '' . __( 'Github', 'simple-draft-list' ) . '' ) ); + + $links = array_merge( $links, array( '' . __( 'Support', 'simple-draft-list' ) . '' ) ); + + $links = array_merge( $links, array( '' . __( 'Donate', 'simple-draft-list' ) . '' ) ); + + $links = array_merge( $links, array( '' . __( 'Write a Review', 'simple-draft-list' ) . ' ⭐️⭐️⭐️⭐️⭐️' ) ); + } return $links; } @@ -46,13 +55,13 @@ function adl_menu_initialise() { $all_posts = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft'" ); if ( $all_posts > 0 ) { - add_submenu_page( 'edit.php', '', __( 'All Drafts ' . $all_posts . '', 'simple-draft-list' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post' ) ); + add_submenu_page( 'edit.php', '', __( 'All Drafts ' . $all_posts . '', 'simple-draft-list' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post' ) ); // Get total number of draft posts for current user. If more than zero add a sub-menu option $your_posts = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author=" . $author ); - if ( $your_posts > 0 && $your_posts != $all_posts ) { - add_submenu_page( 'edit.php', '', __( 'Your Drafts ' . $your_posts . '', 'simple-draft-list' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post&author=' . $author . '' ) ); + if ( $your_posts > 0 && $your_posts !== $all_posts ) { + add_submenu_page( 'edit.php', '', __( 'My Drafts ' . $your_posts . '', 'simple-draft-list' ), 'edit_posts', esc_url( 'edit.php?post_status=draft&post_type=post&author=' . $author . '' ) ); } } @@ -61,17 +70,16 @@ function adl_menu_initialise() { $all_pages = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'draft'" ); if ( $all_pages > 0 ) { - add_submenu_page( 'edit.php?post_type=page', '', __( 'All Drafts ' . $all_pages . '', 'simple-draft-list' ), 'edit_pages', esc_url( 'edit.php?post_status=draft&post_type=page' ) ); + add_submenu_page( 'edit.php?post_type=page', '', __( 'All Drafts ' . $all_pages . '', 'simple-draft-list' ), 'edit_pages', esc_url( 'edit.php?post_status=draft&post_type=page' ) ); // Get total number of draft pages for current user. If more than zero add a sub-menu option $your_pages = $wpdb -> get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'draft' AND post_author=" . $author ); - if ( $your_pages > 0 && $your_pages != $all_pages ) { - add_submenu_page( 'edit.php?post_type=page', '', __( 'Your Drafts ' . $your_pages . '', 'simple-draft-list' ), 'edit_pages', esc_url( 'edit.php?post_status=draft&post_type=page&author=' . $author . '' ) ); + if ( $your_pages > 0 && $your_pages !== $all_pages ) { + add_submenu_page( 'edit.php?post_type=page', '', __( 'My Drafts ' . $your_pages . '', 'simple-draft-list' ), 'edit_pages', esc_url( 'edit.php?post_status=draft&post_type=page&author=' . $author . '' ) ); } } } add_action( 'admin_menu', 'adl_menu_initialise' ); -?> \ No newline at end of file diff --git a/includes/generate-code.php b/includes/generate-code.php index 8100ab9..044d4f5 100755 --- a/includes/generate-code.php +++ b/includes/generate-code.php @@ -21,43 +21,15 @@ function adl_draft_list_shortcode( $paras = '', $content = '' ) { - extract( shortcode_atts( array( 'limit' => '', 'type' => '', 'order' => '', 'scheduled' => '', 'icon' => '', 'folder' => '', 'author' => '', 'template' => '', 'date' => '', 'created' => '', 'modified' => '', 'cache' => '' ), $paras ) ); + extract( shortcode_atts( array( 'limit' => '', 'type' => '', 'order' => '', 'scheduled' => '', 'icon' => '', 'folder' => '', 'author' => '', 'template' => '', 'date' => '', 'created' => '', 'modified' => '', 'cache' => '', 'words' => '', 'pending' => '' ), $paras ) ); - if ( $template == '' ) { $template = adl_convert_to_template( $icon, $author); } + if ( '' === $template ) { $template = adl_convert_to_template( $icon, $author); } - return adl_generate_code( $limit, $type, $order, $scheduled, $folder, $date, $created, $modified, $cache, $template ); + return adl_generate_code( $limit, $type, $order, $scheduled, $folder, $date, $created, $modified, $cache, $template, $words, $pending ); } add_shortcode( 'drafts', 'adl_draft_list_shortcode' ); -/** -* Draft List Function -* -* Function to output draft list -* -* @since 2.0 -* -* @param $template string Template -* @param $paras string Parameters -*/ - -function draft_list( $template, $parameters ) { - - $limit = adl_get_parameters( $parameters, 'limit' ); - $type = adl_get_parameters( $parameters, 'type' ); - $order = adl_get_parameters( $parameters, 'order' ); - $scheduled = adl_get_parameters( $parameters, 'scheduled' ); - $folder = adl_get_parameters( $parameters, 'folder' ); - $date = adl_get_parameters( $parameters, 'date' ); - $created = adl_get_parameters( $parameters, 'created' ); - $modified = adl_get_parameters( $parameters, 'modified' ); - $cache = adl_get_parameters( $parameters, 'cache' ); - - echo adl_generate_code( $limit, $type, $order, $scheduled, $folder, $date, $created, $modified, $cache, $template ); - - return; -} - /** * Generate draft list * @@ -77,41 +49,41 @@ function draft_list( $template, $parameters ) { * @return string Draft list output */ -function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', $scheduled = '', $icon_folder = '', $date_format = '', $created = '', $modified = '', $cache_time = '', $template = '' ) { - +function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', $scheduled = '', $icon_folder = '', $date_format = '', $created = '', $modified = '', $cache_time = '', $template = '', $words = '', $pending = '' ) { + // Check and valid cache details // If invalid, assign a value to it that will ensure no cache is fetched and it drops through to the error checking $cache_time = strtolower( $cache_time ); - if ( $cache_time == '' ) { $cache_time = 0.5; } - if ( ( $cache_time != 'no' ) && ( !is_numeric( $cache_time ) ) ) { $cache_time = 'error'; } + if ( $cache_time === '' ) { $cache_time = 0.5; } + if ( ( $cache_time !== 'no' ) && ( !is_numeric( $cache_time ) ) ) { $cache_time = 'error'; } // Validate the list type as this will determine the cache key - if ( ( $list_type != 'post' ) && ( $list_type != 'page' ) && ( $list_type != '' ) ) { $list_type = 'error'; } + if ( ( $list_type !== 'post' ) && ( $list_type !== 'page' ) && ( $list_type !== '' ) ) { $list_type = 'error'; } // Check for cache - if ( ( $cache_time == 'no' ) or ( $cache_time == 'error' ) or ( $list_type == 'error' ) ) { + if ( ( $cache_time === 'no' ) or ( $cache_time === 'error' ) or ( $list_type === 'error' ) ) { $code = false; } else { // Build an item of the cache key based on current privilages - this ensures that // editors have their own cache - if ( ( current_user_can( 'edit_posts' ) ) && ( ( $list_type == 'post' ) or ( $list_type == '' ) ) ) { $editor = "Y"; } else { $editor = "N"; } - if ( ( current_user_can( 'edit_pages' ) ) && ( ( $list_type == 'page' ) or ( $list_type == '' ) ) ) { $editor .= "Y"; } else { $editor .= "N"; } + if ( ( current_user_can( 'edit_posts' ) ) && ( ( $list_type === 'post' ) or ( $list_type === '' ) ) ) { $editor = "Y"; } else { $editor = "N"; } + if ( ( current_user_can( 'edit_pages' ) ) && ( ( $list_type === 'page' ) or ( $list_type === '' ) ) ) { $editor .= "Y"; } else { $editor .= "N"; } // Build cache key & attempt to get cache - $cache_key = 'adl_' . md5( $list_limit . $list_type . $list_order . $scheduled . $icon_folder . $date_format . $created . $modified . $cache_time . $editor . $template ); + $cache_key = 'adl_' . md5( $list_limit . $list_type . $list_order . $scheduled . $icon_folder . $date_format . $created . $modified . $cache_time . $editor . $template . $words . $pending ); $code = get_transient( $cache_key ); } if ( !$code ) { $plugin_name = 'Draft List'; - $code = "\n\n"; + $code = "\n\n"; // Convert appropriate parameters @@ -122,11 +94,12 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Set default values - if ( $list_limit == '' ) { $list_limit = 0; } - if ( $list_order == '' ) { $list_order = 'da'; } - if ( $scheduled == '' ) { $scheduled = 'yes'; } - if ( $date_format == '' ) { $date_format = 'F j, Y, g:i a'; } - if ( $template == '' ) { $template = '%ul%%draft%'; } + if ( $list_limit === '' ) { $list_limit = 0; } + if ( $words === '' ) { $words = 0; } + if ( $list_order === '' ) { $list_order = 'da'; } + if ( $scheduled === '' ) { $scheduled = 'yes'; } + if ( $date_format === '' ) { $date_format = 'F j, Y, g:i a'; } + if ( $template === '' ) { $template = '{{ul}}{{draft}}'; } // Define order of the results @@ -135,11 +108,11 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', $sort_field = ''; $sort_sequence = ''; - if ( ( $first_char == 'd' ) or ( $first_char == 'm' ) ) { $sort_field = 'post_modified'; } - if ( $first_char == 'c' ) { $sort_field = 'post_date'; } - if ( $first_char == 't' ) { $sort_field = 'post_title'; } - if ( $second_char == 'a' ) { $sort_sequence = ' ASC'; } - if ( $second_char == 'd' ) { $sort_sequence = ' DESC'; } + if ( ( $first_char === 'd' ) or ( $first_char === 'm' ) ) { $sort_field = 'post_modified'; } + if ( $first_char === 'c' ) { $sort_field = 'post_date'; } + if ( $first_char === 't' ) { $sort_field = 'post_title'; } + if ( $second_char === 'a' ) { $sort_sequence = ' ASC'; } + if ( $second_char === 'd' ) { $sort_sequence = ' DESC'; } $order = $sort_field . $sort_sequence; @@ -147,40 +120,45 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', $error = false; if ( !is_numeric( $list_limit ) ) { $code .= adl_report_error( __( 'The limit is invalid - it must be a number', 'simple-draft-list' ), $plugin_name, false ); $error = true; } - if ( ( $sort_field == '' ) or ( $sort_sequence == '' ) ) { $code .= adl_report_error( __( 'The order is invalid - please view the instructions for valid combinations', 'simple-draft-list' ), $plugin_name, false ); $error = true; } - if ( ( $list_type != 'post' ) && ( $list_type != 'page' ) && ( $list_type != '' ) ) { $code .= adl_report_error( __( "The list type is invalid - it must be blank, 'post' or 'page'.", 'simple-draft-list' ), $plugin_name, false ); $error = true; } - if ( ( $scheduled != 'no' ) && ( $scheduled != 'yes' ) ) { $code .= adl_report_error( __( "The scheduled parameter must be either 'Yes' or 'No'", 'simple-draft-list' ), $plugin_name, false ); $error = true; } - if ( ( $cache_time != 'no' ) && ( !is_numeric( $cache_time ) ) ) { $code .= adl_report_error( __( "The cache time is invalid - it should either be a number or 'No'", 'simple-draft-list' ), $plugin_name, false ); $error = true; } - if ( strpos( $template, '%draft%' ) === false ) { $code .= adl_report_error( __( 'The template must include the %draft% tag', 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( !is_numeric( $words ) ) { $code .= adl_report_error( __( 'The minimum number of words is invalid - it must be a number', 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $sort_field === '' ) or ( $sort_sequence === '' ) ) { $code .= adl_report_error( __( 'The order is invalid - please view the instructions for valid combinations', 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $list_type !== 'post' ) && ( $list_type !== 'page' ) && ( $list_type !== '' ) ) { $code .= adl_report_error( __( "The list type is invalid - it must be blank, 'post' or 'page'.", 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $scheduled !== 'no' ) && ( $scheduled !== 'yes' ) ) { $code .= adl_report_error( __( "The scheduled parameter must be either 'Yes' or 'No'", 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $cache_time !== 'no' ) && ( !is_numeric( $cache_time ) ) ) { $code .= adl_report_error( __( "The cache time is invalid - it should either be a number or 'No'", 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( strpos( $template, '%draft%' ) === false && strpos( $template, '{{draft}}' ) === false ) { $code .= adl_report_error( __( 'The template must include the {{draft}} tag', 'simple-draft-list' ), $plugin_name, false ); $error = true; } // Calculate created and modified dates to compare with $far_past = '2 January 1970'; - if ( $created != '' ) { $created = '-' . $created; } else { $created = $far_past; } + if ( $created !== '' ) { $created = '-' . $created; } else { $created = $far_past; } $created = strtotime( $created ); - if ( ( $created == -1 ) or ( !$created ) ) { $code .= adl_report_error( __( 'The created parameter is invalid', 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $created === -1 ) or ( !$created ) ) { $code .= adl_report_error( __( 'The created parameter is invalid', 'simple-draft-list' ), $plugin_name, false ); $error = true; } $created = date( 'Y-m-d H:i:s', $created ); - if ( $modified != '' ) { $modified = '-' . $modified; } else { $modified = $far_past; } + if ( $modified !== '' ) { $modified = '-' . $modified; } else { $modified = $far_past; } $modified = strtotime( $modified ); - if ( ( $modified == -1 ) or ( !$modified ) ) { $code .= adl_report_error( __( 'The modified parameter is invalid', 'simple-draft-list' ), $plugin_name, false ); $error = true; } + if ( ( $modified === -1 ) or ( !$modified ) ) { $code .= adl_report_error( __( 'The modified parameter is invalid', 'simple-draft-list' ), $plugin_name, false ); $error = true; } $modified = date( 'Y-m-d H:i:s', $modified ); if ( !$error ) { // Define the type of list required - if ( ( $list_type == 'post' ) or ( $list_type == 'page' ) ) { + if ( ( $list_type === 'post' ) or ( $list_type === 'page' ) ) { $type = " AND post_type = '" . $list_type . "'"; } else { $type = " AND (post_type = 'post' OR post_type = 'page')"; } - if ( $scheduled != 'no' ) { $status = " OR post_status = 'future'";} else { $status = ''; } + + $status = ''; + + if ( $scheduled !== 'no' && $scheduled !== false ) { $status .= " OR post_status = 'future'"; } + if ( $pending ) { $status .= " OR post_status = 'pending'"; } // Define icon folder - if ( $icon_folder == '' ) { + if ( $icon_folder === '' ) { $icon_folder = plugins_url( 'images/', dirname(__FILE__) ); } else { $icon_folder = get_bloginfo( 'template_url' ) . '/' . $icon_folder . '/'; @@ -188,7 +166,7 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Has a word or character count been requested? - if ( ( strpos( $template, '%words%' ) !== false ) or ( strpos( $template, '%chars%' ) !== false ) or ( strpos( $template, '%chars+space%' ) !== false ) ) { + if ( ( $words > 0 ) or ( strpos( $template, '%words%' ) !== false ) or ( strpos( $template, '{{words}}' ) !== false ) or ( strpos( $template, '%chars%' ) !== false ) or ( strpos( $template, '%chars+space%' ) !== false ) or ( strpos( $template, '{{chars+space}}' ) !== false ) ) { $sql_content = ', post_content'; $count = true; } else { @@ -199,6 +177,7 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Extract drafts from database based on parameters global $wpdb; + $drafts = $wpdb -> get_results( "SELECT A.id, post_type, post_title, post_status, display_name, user_url, post_date, post_modified" . $sql_content . " FROM $wpdb->posts A, $wpdb->users B WHERE B.ID = A.post_author AND (post_status = 'draft'" . $status . ") AND post_title NOT LIKE '!%'" . $type . " ORDER BY " . $order ); // Loop through and output results @@ -208,7 +187,7 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // If template contains list tags at beginning, wrap these around output $list = false; - if ( ( ( substr( $template, 0, 4 ) == '%ol%' ) or ( substr( $template, 0, 4 ) == '%ul%' ) ) && ( $list_limit != 1 ) ) { + if ( ( ( substr( $template, 0, 4 ) === '%ol%' ) or ( substr( $template, 0, 4 ) === '%ul%' ) ) && ( $list_limit !== 1 ) ) { $list_type = substr( $template, 1, 2 ); $code .= '<' . $list_type . ">\n"; $list = true; @@ -216,7 +195,17 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Remove any OL and UL tags $template = str_replace( '%' . $list_type . '%', '', $template ); - } + } + + if ( ( ( substr( $template, 0, 6 ) === '{{ol}}' ) or ( substr( $template, 0, 6 ) === '{{ul}}' ) ) && ( $list_limit !== 1 ) ) { + $list_type = substr( $template, 2, 2 ); + $code .= '<' . $list_type . ">\n"; + $list = true; + + // Remove any OL and UL tags + + $template = str_replace( '{{' . $list_type . '}}', '', $template ); + } $valid_draft = 1; foreach ( $drafts as $draft_data ) { @@ -228,23 +217,31 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', $draft_title = $draft_data -> post_title; $post_created = $draft_data -> post_date; $post_modified = $draft_data -> post_modified; + if ( $count ) { + $post_content = $draft_data -> post_content; + $post_length = str_word_count( $post_content ); + } + + // Check if the post has enough words in it + + if ( $count && $post_length <= $words ) { $enough_words = false; } else { $enough_words = true; } // Does the current user have editor privileges for the current post type - if ( ( ( current_user_can( 'edit_posts' ) ) && ( $post_type == 'post' ) ) or ( ( current_user_can( 'edit_pages' ) ) && ( $post_type == 'page' ) ) ) { $can_edit = true; } else { $can_edit = false; } + if ( ( ( current_user_can( 'edit_posts' ) ) && ( $post_type === 'post' ) ) or ( ( current_user_can( 'edit_pages' ) ) && ( $post_type === 'page' ) ) ) { $can_edit = true; } else { $can_edit = false; } // If the current user can edit then allow a blank title - if ( ( $draft_title == '' ) && ( $can_edit ) ) { $draft_title = __( '[No Title]', 'simple-draft-list' ); } + if ( ( $draft_title === '' ) && ( $can_edit ) ) { $draft_title = __( '[No Title]', 'simple-draft-list' ); } // Work out whether created and/or modified date is acceptable if ( ( $post_created > $created ) && ( $post_modified > $modified ) ) { $date_accept = true; } else { $date_accept = false; } // Only output if the meta isn't set to exclude it, the limit hasn't been reached, - // the dates are fine and the title isn't blank + // there are enough words in the post, the dates are fine and the title isn't blank - if ( ( $date_accept ) && ( $draft_title != '' ) && ( strtolower( get_post_meta( $post_id, 'draft_hide', true ) ) != 'yes' ) && ( ( $list_limit == 0 ) or ( $valid_draft <= $list_limit ) ) ) { + if ( ( $date_accept ) && ( $enough_words ) && ( $draft_title != '' ) && ( strtolower( get_post_meta( $post_id, 'draft_hide', true ) ) != 'yes' ) && ( ( $list_limit == 0 ) or ( $valid_draft <= $list_limit ) ) ) { $post_status = $draft_data -> post_status; $author = $draft_data -> display_name; @@ -261,55 +258,66 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Replace the icon tag $alt_title = __( 'Scheduled', 'simple-draft-list' ); - if ( $post_status == 'future' ) { $icon_url = '' . $alt_title . ''; } else { $icon_url = ''; } + if ( $post_status === 'future' ) { $icon_url = '' . $alt_title . ''; } else { $icon_url = ''; } $this_line = str_replace( '%icon%', $icon_url, $this_line ); + $this_line = str_replace( '{{icon}}', $icon_url, $this_line ); // Replace the author tag $this_line = str_replace( '%author%', $author, $this_line ); - if ( $author_url != '' ) { $author_link = '' . $author . ''; } else { $author_link = $author; } + $this_line = str_replace( '{{author}}', $author, $this_line ); + if ( $author_url !== '' ) { $author_link = '' . $author . ''; } else { $author_link = $author; } $this_line = str_replace( '%author+link%', $author_link, $this_line ); + $this_line = str_replace( '{{author+link}}', $author_link, $this_line ); // Replace the draft tag - if ( $draft_title != '' ) { $draft = $draft_title; } else { $draft = __( '(no title)', 'simple-draft-list' ); } + if ( $draft_title !== '' ) { $draft = $draft_title; } else { $draft = __( '(no title)', 'simple-draft-list' ); } if ( $can_edit ) { $draft = '' . $draft . ''; } $this_line = str_replace( '%draft%', $draft, $this_line ); + $this_line = str_replace( '{{draft}}', $draft, $this_line ); // Replace the created date $created_date = date( $date_format, strtotime( $post_created ) ); $this_line = str_replace( '%created%', $created_date, $this_line ); + $this_line = str_replace( '{{created}}', $created_date, $this_line ); // Replace the modified date $modified_date = date( $date_format, strtotime( $post_modified ) ); $this_line = str_replace( '%modified%', $modified_date, $this_line ); + $this_line = str_replace( '{{modified}}', $modified_date, $this_line ); // Replace the word and character counts if ( $count ) { - if ( strpos( $this_line, '%words%' ) !== false ) { $this_line = str_replace( '%words%', number_format( str_word_count( $post_content, 0 ) ), $this_line ); } + if ( strpos( $this_line, '%words%' ) !== false ) { $this_line = str_replace( '%words%', number_format( $word_count ), $this_line ); } + if ( strpos( $this_line, '{{words}}' ) !== false ) { $this_line = str_replace( '{{words}}', number_format( $word_count ), $this_line ); } if ( strpos( $this_line, '%chars%' ) !== false ) { $this_line = str_replace( '%chars%', number_format( strlen( $post_content ) - substr_count( $post_content, ' ' ) ), $this_line ); } + if ( strpos( $this_line, '{{chars}}' ) !== false ) { $this_line = str_replace( '{{chars}}', number_format( strlen( $post_content ) - substr_count( $post_content, ' ' ) ), $this_line ); } if ( strpos( $this_line, '%chars+space%' ) !== false ) { $this_line = str_replace( '%chars+space%', number_format( strlen( $post_content ) ), $this_line ); } + if ( strpos( $this_line, '{{chars+space}}' ) !== false ) { $this_line = str_replace( '{{chars+space}}', number_format( strlen( $post_content ) ), $this_line ); } } // Replace the category $category = get_the_category( $post_id ); $category = $category[ 0 ] -> cat_name; - if ( $category == 'Uncategorized' ) { $category = ''; } + if ( $category === 'Uncategorized' ) { $category = ''; } $this_line = str_replace( '%category%', $category, $this_line ); + $this_line = str_replace( '{{category}}', $category, $this_line ); // Replace the categories $category_list = ''; foreach( ( get_the_category( $post_id ) ) as $category ) { - if ( $category -> cat_name != 'Uncategorized' ) { $category_list .= ', ' . $category -> cat_name; } + if ( $category -> cat_name !== 'Uncategorized' ) { $category_list .= ', ' . $category -> cat_name; } } - if ( $category_list != '' ) { $category_list = substr( $category_list, 2 ); } + if ( $category_list !== '' ) { $category_list = substr( $category_list, 2 ); } $this_line = str_replace( '%categories%', $category_list, $this_line ); + $this_line = str_replace( '{{categories}}', $category_list, $this_line ); // Now add the current line to the overall code output @@ -327,9 +335,8 @@ function adl_generate_code( $list_limit = '', $list_type = '', $list_order = '', // Saving resulting output to cache - if ( ( $cache_key != 'no' ) && ( !$error ) ) { set_transient( $cache_key, $code, 3600 * $cache_time ); } + if ( ( $cache_key !== 'no' ) && ( !$error ) ) { set_transient( $cache_key, $code, HOUR_IN_SECONDS * $cache_time ); } } return $code; } -?> \ No newline at end of file diff --git a/includes/generate-widget.php b/includes/generate-widget.php index afdb58d..e514798 100755 --- a/includes/generate-widget.php +++ b/includes/generate-widget.php @@ -24,7 +24,11 @@ function __construct() { parent::__construct( 'draft_list_widget', __( 'Draft List', 'simple-draft-list' ), - array( 'description' => __( 'Display a list of draft posts and/or pages.', 'simple-draft-list' ), 'class' => 'my-widget-class' ) + array( + 'description' => __( 'Display a list of draft posts and/or pages.', 'simple-draft-list' ), + 'class' => 'dl-widget', + 'customize_selective_refresh' => true, + ) ); } @@ -42,6 +46,7 @@ function __construct() { */ function widget( $args, $instance ) { + extract( $args, EXTR_SKIP ); // Output the header @@ -63,7 +68,10 @@ function widget( $args, $instance ) { $instance[ 'created' ], $instance[ 'modified' ], $instance[ 'cache' ], - $instance[ 'template' ] ); + $instance[ 'template' ], + $instance[ 'words' ], + $instance[ 'pending' ] + ); // Output the trailer echo $after_widget; @@ -84,7 +92,7 @@ function widget( $args, $instance ) { function update( $new_instance, $old_instance ) { $instance = $old_instance; - $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); + $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] ); $instance[ 'limit' ] = $new_instance[ 'limit' ]; $instance[ 'type' ] = $new_instance[ 'type' ]; $instance[ 'order' ] = $new_instance[ 'order' ]; @@ -95,6 +103,8 @@ function update( $new_instance, $old_instance ) { $instance[ 'created' ] = $new_instance[ 'created' ]; $instance[ 'modified' ] = $new_instance[ 'modified' ]; $instance[ 'template' ] = $new_instance[ 'template' ]; + $instance[ 'words' ] = $new_instance[ 'words' ]; + $instance[ 'pending' ] = $new_instance[ 'pending' ]; return $instance; } @@ -113,7 +123,7 @@ function form( $instance ) { // Set default options - $default = array( 'title' => __( 'Coming Soon', 'simple-draft-list' ), 'limit' => '0', 'type' => '', 'order' => '', 'scheduled' => '', 'folder' => '', 'cache' => '0.5', 'date' => 'F j, Y, g:i a', 'created' => '', 'modified' => '', 'template' => '%ol%%draft% %icon%' ); + $default = array( 'title' => __( 'Coming Soon', 'simple-draft-list' ), 'limit' => '0', 'type' => '', 'order' => '', 'scheduled' => '', 'folder' => '', 'cache' => '0.5', 'date' => 'F j, Y, g:i a', 'created' => '', 'modified' => '', 'template' => '{{ol}}{{draft}} {{icon}}' ); $instance = wp_parse_args( ( array ) $instance, $default ); // Title field @@ -134,16 +144,22 @@ function form( $instance ) { $field_name = $this -> get_field_name( 'limit' ); echo "\r\n" . '

'; + // Minimum number of words + + $field_id = $this -> get_field_id( 'words' ); + $field_name = $this -> get_field_name( 'words' ); + echo "\r\n" . '

'; + // Draft types field $field_id = $this -> get_field_id( 'type' ); $field_name = $this -> get_field_name( 'type' ); echo "\r\n" . '

'; // Order field @@ -151,17 +167,17 @@ function form( $instance ) { $field_id = $this -> get_field_id( 'order' ); $field_name = $this -> get_field_name( 'order' ); echo "\r\n" . '

'; // Scheduled field @@ -169,9 +185,17 @@ function form( $instance ) { $field_id = $this -> get_field_id( 'scheduled' ); $field_name = $this -> get_field_name( 'scheduled' ); echo "\r\n" . '

'; + // Show pending posts + + $field_id = $this -> get_field_id( 'pending' ); + $field_name = $this -> get_field_name( 'pending' ); + echo "\r\n" . '

'; + // Folder field $field_id = $this -> get_field_id( 'folder' ); @@ -219,4 +243,3 @@ function adl_register_widgets() { } add_action( 'widgets_init', 'adl_register_widgets' ); -?> \ No newline at end of file diff --git a/includes/meta-box.php b/includes/meta-box.php index ffe57f0..a3590bd 100755 --- a/includes/meta-box.php +++ b/includes/meta-box.php @@ -19,9 +19,9 @@ function adl_add_custom_box() { $box_title = __( 'Draft List', 'simple-draft-list' ); - add_meta_box( 'adl_metaid', __( $box_title ), 'adl_custom_box', 'post' ); + add_meta_box( 'adl_metaid', __( $box_title ), 'adl_custom_box', 'post', 'side' ); - add_meta_box( 'adl_metaid', __( $box_title ), 'adl_custom_box', 'page' ); + add_meta_box( 'adl_metaid', __( $box_title ), 'adl_custom_box', 'page', 'side' ); } @@ -47,7 +47,7 @@ function adl_custom_box( $post ) { echo ' '; echo 'ID, 'draft_hide', true ) ) == 'yes' ) { echo ' checked="checked"'; } + if ( strtolower( get_post_meta( $post->ID, 'draft_hide', true ) ) === 'yes' ) { echo ' checked="checked"'; } echo ' />'; } @@ -71,14 +71,14 @@ function adl_save_postdata( $post_id ) { // Verify this came from the correct meta box and with proper authorization - if ( isset( $_POST[ 'artiss_draft_list_noncename' ] ) ) { - if ( !wp_verify_nonce( $_POST[ 'artiss_draft_list_noncename' ], plugin_basename( __FILE__ ) ) ) { return; } + if ( isset( $_POST[ 'artiss_draft_list_noncename' ] ) ) { // Input var okay. + if ( !wp_verify_nonce( sanitize_text_field( $_POST[ 'artiss_draft_list_noncename' ] ), plugin_basename( __FILE__ ) ) ) { return; } // Input var okay. } // Check permissions - if ( isset( $_POST[ 'post_hide' ] ) ) { - if ( $_POST[ 'post_type' ] == 'page' ) { + if ( isset( $_POST[ 'post_hide' ] ) ) { // Input var okay. + if ( sanitize_text_field( $_POST[ 'post_type' ] ) === 'page' ) { // Input var okay. if ( !current_user_can( 'edit_page', $post_id ) ) { return; } } else { if ( !current_user_can( 'edit_post', $post_id ) ) { return; } @@ -87,8 +87,8 @@ function adl_save_postdata( $post_id ) { // Save the data - if ( isset( $_POST[ 'adl_hide' ] ) ) { - $data = sanitize_text_field( $_POST[ 'adl_hide' ] ); + if ( isset( $_POST[ 'adl_hide' ] ) ) { // Input var okay. + $data = sanitize_text_field( $_POST[ 'adl_hide' ] ); // Input var okay. } else { $data = ''; } @@ -102,4 +102,3 @@ function adl_save_postdata( $post_id ) { } add_action( 'save_post', 'adl_save_postdata' ); -?> \ No newline at end of file diff --git a/includes/shared-functions.php b/includes/shared-functions.php index 0489d83..59ce403 100755 --- a/includes/shared-functions.php +++ b/includes/shared-functions.php @@ -21,11 +21,11 @@ function adl_convert_to_template( $icon = '', $author = '' ) { - $template = '%ul%'; - if ( strtolower( $icon ) == 'left' ) { $template .= '%icon% '; } - $template .= '%draft%'; - if ( strtolower( $author ) == 'yes' ) { $template .= ' (%author%)'; } - if ( strtolower( $icon ) == 'right' ) { $template .= ' %icon%'; } + $template = '{{ul}}'; + if ( strtolower( $icon ) === 'left' ) { $template .= '{{icon}} '; } + $template .= '{{draft}}'; + if ( strtolower( $author ) === 'yes' ) { $template .= ' ({{author}})'; } + if ( strtolower( $icon ) === 'right' ) { $template .= ' {{icon}}'; } return $template; } @@ -77,4 +77,3 @@ function adl_report_error( $error, $plugin_name, $echo = true ) { return $output; } } -?> \ No newline at end of file diff --git a/readme.txt b/readme.txt index a367044..b44d53a 100755 --- a/readme.txt +++ b/readme.txt @@ -1,10 +1,11 @@ === Draft List === -Contributors: dartiss +Contributors: dartiss +Donate link: https://artiss.blog/donate Tags: draft, list, SEO, sidebar, widget, coming soon Requires at least: 4.6 Tested up to: 5.3 Requires PHP: 5.3 -Stable tag: 2.3.3 +Stable tag: 2.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -16,7 +17,7 @@ Draft List allows you to both manage your draft and scheduled posts more easily How easy is it display a list of draft posts? Here's an example of how you could use it in a post or page... -`[drafts limit=5 type=post order=ma scheduled=no template='%ul%%draft% %icon%']` +`[drafts limit=5 type=post order=ma scheduled=no template='{{ul}}{{draft}} {{icon}}']` This would display a list of up to 5 draft posts in ascending modified date sequence, with an icon displayed to the right of each if the draft is scheduled. @@ -35,54 +36,49 @@ Options coming soon… * Quick links to be added to the admin menu for scheduled posts and pages * Shortcuts to be added to the Admin Bar -Technical specification... - -* Licensed under [GPLv2 (or later)](http://wordpress.org/about/gpl/ "GNU General Public License") -* Designed for both single and multi-site installations -* PHP7 compatible -* Fully internationalized, ready for translations. **If you would like to add a translation to this plugin then please head to our [Translating WordPress](https://translate.wordpress.org/projects/wp-plugins/simple-draft-list "Translating WordPress") page** - Please visit the [Github page](https://github.com/dartiss/draft-list "Github") for the latest code development, planned enhancements and known issues. == Shortcode Parameters == The following shortcode parameters are valid... - -* **limit=** : The maximum number of draft items to display. The default is 0, which is unlimited. -* **type=** : This allows you to limit the results to either `post` or `page`. The default is both. -* **order=** : This is the sequence that you'd like to order the results in. It consists of 2 codes - the first is either `t`, `m` or `c` to represent the title, modified date or created date and the second is `a` or `d` for ascending or descending. Therefore `order=td` will display the results in descending title sequence. The default is descending modified date. -* **scheduled=** : If specified as `No` then scheduled posts will not display in the list, only drafts. + +* **cache=** : How long to cache the output for, in hours. Defaults to half an hour. Set to `No` to not cache at all. Whenever you save a post any cache will be cleared to ensure that any lists are updated. +* **date=** : The format of any dates output. This uses the PHP date formatting system - [read here](http://uk3.php.net/manual/en/function.date.php "date") for the formatting codes. Defaults to `F j, Y, g:i a`. * **folder=** : The scheduled icon will be, by default, the one in the plugin folder named `scheduled.png`. However, use this parameter to specify a folder within your theme that you'd prefer the icon to be fetched from. -* **cache=** : How long to cache the output for, in hours. Defaults to half an hour. Set to `No` to not cache at all. Whenever you save a post any cache will be cleared to ensure that any lists are updated. +* **limit=** : The maximum number of draft items to display. The default is 0, which is unlimited. +* **order=** : This is the sequence that you'd like to order the results in. It consists of 2 codes - the first is either `t`, `m` or `c` to represent the title, modified date or created date and the second is `a` or `d` for ascending or descending. Therefore `order=td` will display the results in descending title sequence. The default is descending modified date. +* **pending=** : Whether to include pending posts as well. Should be set to `true` or `false`. The default is `false`. +* **scheduled=** : If specified as false then scheduled posts will not display in the list, only drafts. * **template=** : This is the template which formats the output. See the section below on * *Templates** for further information. -* **date=** : The format of any dates output. This uses the PHP date formatting system - [read here](http://uk3.php.net/manual/en/function.date.php "date") for the formatting codes. Defaults to `F j, Y, g:i a`. +* **type=** : This allows you to limit the results to either `post` or `page`. The default is both. +* **words=** : The minimum number of words that must be present in the draft for it to be included. Defaults to 0. To restrict the posts/pages to a particular timeframe you can use the following 2 parameters. You simply state, in words, how long ago the posts must be dated for e.g. "2 days", "3 months", etc. - -* **modified=** : This reflects how long ago the post/page must have been modified last for it to be listed. For example `6 months` would only list drafts that have been modified in the last 6 months. + * **created=** : his reflects how long ago the post/page must have been created for it to be listed. For example `6 months` would only list drafts that were created in the last 6 months. +* **modified=** : This reflects how long ago the post/page must have been modified last for it to be listed. For example `6 months` would only list drafts that have been modified in the last 6 months. -== Templates == +== 🧩 Templates == The template parameter allows you to format the output by allowing you to specify how each line of output will display. A number of tags can be added, and you can mix these with HTML. The available tags are as follows... -* **%ul%** - Specifies this is an un-ordered list (i.e. bullet point output). This MUST be specified at the beginning of the template if it is to be used. -* **%ol%** - Specifies this is an ordered list (i.e. number output). This MUST be specified at the beginning of the template if it is to be used. -* **%icon%** - This is the icon that indicates a scheduled post. -* **%draft%** - This is the draft post details. This is the only **REQUIRED** tag. -* **%author%** - This is the name of the post author. -* **%author+link%** - This is the name of the post author with, where available, a link to their URL. -* **%words%** - The number of words in the draft post. -* **%chars%** - The number of characters (exc. spaces) in the draft post. -* **%chars+space%** - The number of characters (inc. spaces) in the draft post. -* **%created%** - The date/time the post was created. -* **%modified%** - The date/time the post was last modified. -* **%category%** - Shows the first category assigned to the post. -* **%categories%** - Shows all categories assigned to the post, comma separated. +* **{{ul}}** - Specifies this is an un-ordered list (i.e. bullet point output). This MUST be specified at the beginning of the template if it is to be used. +* **{{ol}}** - Specifies this is an ordered list (i.e. number output). This MUST be specified at the beginning of the template if it is to be used. +* **{{icon}}** - This is the icon that indicates a scheduled post. +* **{{draft}}** - This is the draft post details. This is the only **REQUIRED** tag. +* **{{author}}** - This is the name of the post author. +* **{{author+link}}** - This is the name of the post author with, where available, a link to their URL. +* **{{words}}** - The number of words in the draft post. +* **{{chars}}** - The number of characters (exc. spaces) in the draft post. +* **{{chars+space}}** - The number of characters (inc. spaces) in the draft post. +* **{{created}}** - The date/time the post was created. +* **{{modified}}** - The date/time the post was last modified. +* **{{category}}** - Shows the first category assigned to the post. +* **{{categories}}** - Shows all categories assigned to the post, comma separated. -If %ul% or %ol% are specified then all the appropriate list tags will be added to the output. If neither are used then it's assumed that line output will be controlled by yourself. +If {{ul}} or {{ol}} are specified then all the appropriate list tags will be added to the output. If neither are used then it's assumed that line output will be controlled by yourself. -== Omitting Posts/Pages from Results == +== ⛔️ Omitting Posts/Pages from Results == If you wish to omit a page or post from the list then you can do this in 3 ways... @@ -90,7 +86,7 @@ If you wish to omit a page or post from the list then you can do this in 3 ways. 2. The post and page editor has a meta box, where you can select to hide the page/post. 3. You can add a custom field to a page/post with a name of 'draft_hide' and a value of 'Yes' -== Edit Link == +== ✏️ Edit Link == If the current user can edit the draft item being listed then it will be linked to the appropriate edit page. The user then simply needs to click on the draft item to edit it. @@ -122,6 +118,17 @@ Voila! It's ready to go. [Learn more about my version numbering methodology](https://artiss.blog/2016/09/wordpress-plugin-versioning/ "WordPress Plugin Versioning") += 2.4 = +* Enhancement: New option to include pending posts in lists +* Enhancement: New option to limit the posts listed to those with a minimum number of words +* Enhancement: Now using a time constant for the caching +* Enhancement: Added Github links to the plugin meta +* Enhancement: Double braces are the new template standard! +* Enhancement: Renamed 'Your Drafts' to the more appropriate 'My Drafts' +* Maintenance: Added selective refresh support to the widget +* Bug: Fixed an `undefined constant` warning +* Bug: Improved the padding around the draft count in the admin menu + = 2.3.3 = * Maintenance: The README has had a re-write and the image assets have been replaced. Shiny! @@ -226,5 +233,5 @@ Voila! It's ready to go. == Upgrade Notice == -= 2.3.3 = -* A number of maintenance updates \ No newline at end of file += 2.4 = +* A few assorted improvements, alongside some bug fixes \ No newline at end of file diff --git a/simple-draft-list.php b/simple-draft-list.php index d93ffdd..1850c42 100755 --- a/simple-draft-list.php +++ b/simple-draft-list.php @@ -1,42 +1,39 @@ - \ No newline at end of file +