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 108I 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 132A 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.
- The topic ‘Some coding improvements’ is closed to new replies.