Title: Some coding improvements
Last modified: July 2, 2020

---

# Some coding improvements

 *  [drtonyb](https://wordpress.org/support/users/drtonyb/)
 * (@drtonyb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/some-coding-improvements/)
 * This is a very useful plugin, so firstly, thanks to the authors.
 * I have DEBUG enabled on my development system so that I know when there are problems.
   There are a couple of issues I have noticed:
 * 1. In lcp-templater.php line 108, scandir($path) fails when there is no “list-
   category-posts” folder in the child or parent theme. As an example of no “list-
   category-posts” folder in “parent-theme”, this results in:
 * PHP Warning: scandir(C:\Server\public_html\wordpress/wp-content/themes/parent-
   theme/list-category-posts/,C:\Server\public_html\wordpress/wp-content/themes/
   parent-theme/list-category-posts/): The system cannot find the file specified.(
   code: 2) in C:\Server\public_html\wordpress\wp-content\plugins\list-category-
   posts\include\lcp-templater.php on line 108
    PHP Warning: scandir(C:\Server\public_html\
   wordpress/wp-content/themes/parent-theme/list-category-posts/): failed to open
   dir: No such file or directory in C:\Server\public_html\wordpress\wp-content\
   plugins\list-category-posts\include\lcp-templater.php on line 108 PHP Warning:
   scandir(): (errno 2): No such file or directory in C:\Server\public_html\wordpress\
   wp-content\plugins\list-category-posts\include\lcp-templater.php on line 108 
   PHP Warning: Invalid argument supplied for foreach() in C:\Server\public_html\
   wordpress\wp-content\plugins\list-category-posts\include\lcp-templater.php on
   line 108
 * I suggest adding a check for a folder’s existence using is_dir(). Also, in the
   inner foreach loop, the negative validate_template test, which if true, executes
   continue. I would avoid this type of construct and use if validate_template is
   true then execute the necessary code. So the function could be written as:
 *     ```
         public static function get_templates() {
           $templates = [];
           $paths = self::get_template_paths();
   
           foreach ($paths as $path) {
             if (is_dir($path)) {
               foreach (scandir($path) as $file) {
                 if (self::validate_template($path, $file)) {
                   $template_name = substr($file, 0, strlen($file) - 4);
   
                   // Add the template only if necessary
                   if (! in_array( $template_name, $templates)) {
                     $templates[] = $template_name;
                   }
                 }
               }
             }
           }
           return $templates;
         }
       ```
   
 * 2. In lcp-widget there are no tests for the existence of checkbox inputs that
   are unchecked when the widget is saved. If all checkboxes are unchecked, this
   results in:
 * PHP Notice: Undefined index: dateformat in C:\Server\public_html\wordpress\wp-
   content\plugins\list-category-posts\include\lcp-widget.php on line 119
    PHP Notice:
   Undefined index: show_date in C:\Server\public_html\wordpress\wp-content\plugins\
   list-category-posts\include\lcp-widget.php on line 120 PHP Notice: Undefined 
   index: show_modified_date in C:\Server\public_html\wordpress\wp-content\plugins\
   list-category-posts\include\lcp-widget.php on line 121 PHP Notice: Undefined 
   index: show_excerpt in C:\Server\public_html\wordpress\wp-content\plugins\list-
   category-posts\include\lcp-widget.php on line 122 PHP Notice: Undefined index:
   show_author in C:\Server\public_html\wordpress\wp-content\plugins\list-category-
   posts\include\lcp-widget.php on line 124 PHP Notice: Undefined index: show_catlink
   in C:\Server\public_html\wordpress\wp-content\plugins\list-category-posts\include\
   lcp-widget.php on line 125 PHP Notice: Undefined index: show_catlink in C:\Server\
   public_html\wordpress\wp-content\plugins\list-category-posts\include\lcp-widget.
   php on line 126 PHP Notice: Undefined index: thumbnail in C:\Server\public_html\
   wordpress\wp-content\plugins\list-category-posts\include\lcp-widget.php on line
   127 PHP Notice: Undefined index: pagination in C:\Server\public_html\wordpress\
   wp-content\plugins\list-category-posts\include\lcp-widget.php on line 132
 * A test such as isset() is needed before trying to get the checkbox state. For
   example, line 120 for ‘show_date’ is better written as:
 * `$instance['show_date'] = (isset($new_instance['show_date']) ? strip_tags($new_instance['
   show_date']) : 'no');`
 * Also, the widget has no ‘dateformat’, so line 119 will always fail.
 * Line 125 is redundant, as it is a duplicate of line 124 for saving ‘show_catlink’.
 * I hope that you find this helpful feedback.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [drtonyb](https://wordpress.org/support/users/drtonyb/)
 * (@drtonyb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/some-coding-improvements/#post-13061831)
 * A small correction. The unchecked state for checkboxes should be set to an empty
   string ”, not ‘no’:
 * `$instance['show_date'] = (isset($new_instance['show_date']) ? strip_tags($new_instance['
   show_date']) : '');`
 *  Plugin Contributor [zymeth25](https://wordpress.org/support/users/zymeth25/)
 * (@zymeth25)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/some-coding-improvements/#post-13061876)
 * Hi, thanks for getting in touch.
 * As for the widget, we are no longer developing it and we encourage users to use
   a shortode in the text widet as described [in the docs](https://github.com/picandocodigo/List-Category-Posts/wiki/Widget).
   But we will try to fix this small issue.
 *  Plugin Contributor [zymeth25](https://wordpress.org/support/users/zymeth25/)
 * (@zymeth25)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/some-coding-improvements/#post-13061896)
 * The `get_templates` method is **only** used by the widget, so this is also why
   we never encoutered this issue. Most probably we are going to refactor the widget
   anyway so it’s more maintainable so all this code will change.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Some coding improvements’ is closed to new replies.

 * ![](https://ps.w.org/list-category-posts/assets/icon-256x256.png?rev=2517221)
 * [List category posts](https://wordpress.org/plugins/list-category-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/list-category-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/list-category-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/list-category-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/list-category-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/list-category-posts/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [zymeth25](https://wordpress.org/support/users/zymeth25/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/some-coding-improvements/#post-13061896)
 * Status: not a support question