✅ Новости WEB и WordPress, темы, плагины. Здесь мы делимся советами и лучшими решениями для веб-сайтов.

Виджеты WordPress: рефакторинг, часть 2

29

После всего предварительного контента мы, наконец, подошли к тому моменту, когда готовы приступить к решению стандартных проблем кодирования, возникающих в нашей среде IDE и наших инструментах качества кода.

При этом я собираюсь разбить содержание на два поста:

  • первый пост будет посвящен исключительно рефакторингу существующего кода,
  • в следующем посте мы рассмотрим рефакторинг структуры плагина для улучшения организации и архитектуры.

А пока давайте взглянем на ошибки, которые выдает анализатор кода, и посмотрим, не сможем ли мы привести его в соответствие с более современными стандартами.

Шаблон виджета WordPress: рефакторинг, часть 2

Обратите внимание, что я все еще нахожусь в ветке разработки, так как мы еще не готовы объединить ее с мастером. И если у вас правильно настроены анализаторы кода и другие инструменты, вы должны увидеть следующее:

Warning: Uncaught ErrorException: require_once(/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/vendor/autoload.php): failed to open stream: No such file or directory in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer:62
Stack trace:
#0 /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer(62): {closure}(2, 'require_once(/U...', '/Users/tommcfar...', 62, Array)
#1 /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer(62): require_once()
#2 {main}
  thrown in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer on line 62

Call Stack:
    0.0041     365448   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer:0

Fatal error: main(): Failed opening required '/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/vendor/autoload.php' (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer on line 62

Call Stack:
    0.0041     365448   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer:0

You can fix all errors by running following commands:
'/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer' '--allow-risky=yes' '--config=.php_cs.dist' '--using-cache=yes' '--verbose' 'fix'
ERROR: Ruleset /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/phpcs.xml is not valid

Run "phpcs --help" for usage information

Warning: require(/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint.php): failed to open stream: No such file or directory in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint on line 4

Call Stack:
    0.0035     352864   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint:0

Fatal error: require(): Failed opening required '/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint.php' (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint on line 4

Call Stack:
    0.0035     352864   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint:0

PHP Warning:  require(/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint.php): failed to open stream: No such file or directory in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint on line 4
PHP Stack trace:
PHP   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint:0
PHP Fatal error:  require(): Failed opening required '/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint.php' (include_path='.:/usr/local/Cellar/php@7.1/7.1.19/share/php@7.1/pear') in /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint on line 4
PHP Stack trace:
PHP   1. {main}() /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/parallel-lint:0
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:33     The class Widget_Name has 12 public methods. Consider refactoring Widget_Name to keep number of public methods under 10.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:33     The class Widget_Name is not named in CamelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:33     The property$widget_slug is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:97     The method get_widget_slug is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$widget_string is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$before_widget is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$widget_string is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$widget_string is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$after_widget is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$widget_string is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    The variable$widget_string is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:111    Avoid unusedparameters such as '$instance'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:131    Avoid unusedlocal variables such as '$before_widget'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:137    Avoid unusedlocal variables such as '$after_widget'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:149    The method flush_widget_cache is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:159    The parameter $new_instance is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:159    The parameter $old_instance is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:159    The variable$old_instance is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:159    Avoid unusedparameters such as '$new_instance'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:195    The method widget_textdomain is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:207    The parameter $network_wide is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:207    Avoid unusedparameters such as '$network_wide'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:216    The parameter $network_wide is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:216    Avoid unusedparameters such as '$network_wide'.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:223    The method register_admin_styles is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:232    The method register_admin_scripts is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:241    The method register_widget_styles is not named in camelCase.
/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/src/Plugin.php:250    The method register_widget_scripts is not named in camelCase.
[FATAL] /Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/phpcs.xml: Premature end of data in tag ruleset line 2
 (77) on line 28,12

И вы должны увидеть по крайней мере все это после запуска:

$ vendor/bin/grumphp run

Прежде чем двигаться дальше, нам нужно внести небольшие изменения в организацию файлов. То есть нам нужно разделить основной файл плагина (который у нас есть как Plugin.php) на два файла.

В частности, нам нужен загрузочный файл плагина, а затем нам нужно поддерживать основной файл плагина. Итак, во-первых, давайте создадим загрузчик плагина.

Создание начальной загрузки

В корне плагина создайте файл с именем wordpress-widget-boilerplate.php. Затем добавьте к нему следующее :

<?php
/**
 * WordPress Widget Boilerplate
 *
 * The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building widgets using WordPress best practices.
 *
 * @package   Widget_Name
 * @author    Your Name <email@example.com>
 * @license   GPL-2.0+
 * @link      http://example.com
 * @copyright 2018 Your Name or Company Name
 *
 * @wordpress-plugin
 * Plugin Name:       @TODO
 * Plugin URI:        @TODO
 * Description:       @TODO
 * Version:           1.0.0
 * Author:            @TODO
 * Author URI:        @TODO
 * Text Domain:       widget-name
 * License:           GPL-3.0+
 * License URI:       http://www.gnu.org/licenses/gpl-3.0.txt
 * Domain Path:       /lang
 * GitHub Plugin URI: https://github.com/<owner>/<repo>
 */

// Prevent this file from being called directly.
defined('WPINC') || die;

// Include the autoloader.
require_once __DIR__. '/inc/autoload.php';

Примечание в файле выше; у нас есть заголовок плагина, условие для определения возможности доступа к файлу, а затем загрузка автозагрузчика Composer.

Отредактируйте основной плагин

Как только это будет сделано, основной файл Plugin.php будет выглядеть следующим образом:

<?php

// TODO: change 'Widget_Name' to the name of your plugin
class Widget_Name extends WP_Widget {

    /**
     * @TODO - Rename "widget-name" to the name your your widget
     *
     * Unique identifier for your widget.
     *
     *
     * The variable name is used as the text domain when internationalizing strings
     * of text. Its value should match the Text Domain file header in the main
     * widget file.
     *
     * @since    1.0.0
     *
     * @var      string
     */
    protected $widget_slug = 'widget-name';

    /*--------------------------------------------------*/
    /* Constructor
    /*--------------------------------------------------*/

    /**
     * Specifies the classname and description, instantiates the widget,
     * loads localization files, and includes necessary stylesheets and JavaScript.
     */
    public function __construct() {

        // load plugin text domain
        add_action( 'init', array( $this, 'widget_textdomain') );

        // TODO: update description
        parent::__construct(
            $this->get_widget_slug(),
            __( 'Widget Name', $this->get_widget_slug() ),
            array(
                'classname'  => $this->get_widget_slug().'-class',
                'description' => __( 'Short description of the widget goes here.', $this->get_widget_slug())) );

        // Register admin styles and scripts
        add_action( 'admin_print_styles', array( $this, 'register_admin_styles') );
        add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts') );

        // Register site styles and scripts
        add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles') );
        add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts') );

        // Refreshing the widget's cached output with each new post
        add_action( 'save_post',    array( $this, 'flush_widget_cache') );
        add_action( 'deleted_post', array( $this, 'flush_widget_cache') );
        add_action( 'switch_theme', array( $this, 'flush_widget_cache') );

    } // end constructor

    /**
     * Return the widget slug.
     *
     * @since    1.0.0
     *
     * @return    Plugin slug variable.
     */
    public function get_widget_slug() {
        return $this->widget_slug;
    }

    /*--------------------------------------------------*/
    /* Widget API Functions
    /*--------------------------------------------------*/

    /**
     * Outputs the content of the widget.
     *
     * @param array args  The array of form elements
     * @param array instance The current instance of the widget
     */
    public function widget( $args, $instance) {

        // Check if there is a cached output
        $cache = wp_cache_get( $this->get_widget_slug(), 'widget' );

        if (!is_array( $cache)) $cache = array();

        if (! isset ($args['widget_id'])) $args['widget_id'] = $this->id;

        if (isset ($cache[ $args['widget_id'] ])) return print $cache[ $args['widget_id'] ];

        // go on with your widget logic, put everything into a string and …

        extract( $args, EXTR_SKIP );

        $widget_string = $before_widget;

        // TODO: Here is where you manipulate your widget's values based on their input fields
        ob_start();
        include( plugin_dir_path( __FILE__ ). 'views/widget.php' );
        $widget_string .= ob_get_clean();
        $widget_string .= $after_widget;

        $cache[ $args['widget_id'] ] = $widget_string;

        wp_cache_set( $this->get_widget_slug(), $cache, 'widget' );

        print $widget_string;

    } // end widget

    public function flush_widget_cache()
    {
        wp_cache_delete( $this->get_widget_slug(), 'widget' );
    }
    /**
     * Processes the widget's options to be saved.
     *
     * @param array new_instance The new instance of values to be generated via the update.
     * @param array old_instance The previous instance of values before the update.
     */
    public function update( $new_instance, $old_instance) {

        $instance = $old_instance;

        // TODO: Here is where you update your widget's old values with the new, incoming values

        return $instance;

    } // end update

    /**
     * Generates the administration form for the widget.
     *
     * @param array instance The array of keys and values for the widget.
     */
    public function form( $instance) {

        // TODO: Define default values for your variables
        $instance = wp_parse_args(
            (array) $instance
        );

        // TODO: Store the values of the widget in their own variable

        // Display the admin form
        include( plugin_dir_path(__FILE__). 'views/admin.php' );

    } // end form

    /*--------------------------------------------------*/
    /* Public Functions
    /*--------------------------------------------------*/

    /**
     * Loads the Widget's text domain for localization and translation.
     */
    public function widget_textdomain() {

        // TODO be sure to change 'widget-name' to the name of *your* plugin
        load_plugin_textdomain( $this->get_widget_slug(), false, dirname( plugin_basename( __FILE__) ). 'lang/' );

    } // end widget_textdomain

    /**
     * Fired when the plugin is activated.
     *
     * @param  boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
     */
    public static function activate( $network_wide) {
        // TODO define activation functionality here
    } // end activate

    /**
     * Fired when the plugin is deactivated.
     *
     * @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog
     */
    public static function deactivate( $network_wide) {
        // TODO define deactivation functionality here
    } // end deactivate

    /**
     * Registers and enqueues admin-specific styles.
     */
    public function register_admin_styles() {

        wp_enqueue_style( $this->get_widget_slug().'-admin-styles', plugins_url( 'css/admin.css', __FILE__) );

    } // end register_admin_styles

    /**
     * Registers and enqueues admin-specific JavaScript.
     */
    public function register_admin_scripts() {

        wp_enqueue_script( $this->get_widget_slug().'-admin-script', plugins_url( 'js/admin.js', __FILE__ ), array('jquery') );

    } // end register_admin_scripts

    /**
     * Registers and enqueues widget-specific styles.
     */
    public function register_widget_styles() {

        wp_enqueue_style( $this->get_widget_slug().'-widget-styles', plugins_url( 'css/widget.css', __FILE__) );

    } // end register_widget_styles

    /**
     * Registers and enqueues widget-specific scripts.
     */
    public function register_widget_scripts() {

        wp_enqueue_script( $this->get_widget_slug().'-script', plugins_url( 'js/widget.js', __FILE__ ), array('jquery') );

    } // end register_widget_scripts

} // end class

// TODO: Remember to change 'Widget_Name' to match the class name definition
add_action( 'widgets_init', create_function( '', 'register_widget("Widget_Name");') );

// Hooks fired when the Widget is activated and deactivated
// TODO: Remember to change 'Widget_Name' to match the class name definition
register_activation_hook( __FILE__, array( 'Widget_Name', 'activate') );
register_deactivation_hook( __FILE__, array( 'Widget_Name', 'deactivate') );

Помните, нам предстоит еще много работы, но первое, что нам нужно сделать, это начать очистку кодовой базы от ее текущего состояния. В конце концов, мы будем рефакторить его в гораздо более объектно-ориентированном стиле, но нам нужно получить текущую версию плагина в работоспособном состоянии.

Однако теперь мы можем вернуться к исходному выводу GrumPHP. Если вы просмотрите, на выходе вы увидите строку, подобную этой:

You can fix all errors by running following commands:
'/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer' '--allow-risky=yes' '--config=.php_cs.dist' '--using-cache=yes' '--verbose' 'fix'

Итак, на этом этапе давайте рассмотрим выполнение рекомендуемой команды и посмотрим, что произойдет. В частности, введите это в свой терминал:

'/Users/tommcfarlin/Dropbox/Projects/trunk/wp-content/plugins/WordPress-Widget-Boilerplate/vendor/bin/php-cs-fixer' '--allow-risky=yes' '--config=.php_cs.dist' '--using-cache=yes' '--verbose' 'fix'

Предполагая, что все работает правильно, вы должны увидеть что-то вроде этого:

Примечание об ошибках вывода

Если вы получаете какую-либо ошибку всякий раз, когда делаете это, вам может потребоваться обновить каталог поставщика или даже обновить каталог. Если это так, то сначала попробуйте это:

$ composer update

И если это не решит вашу проблему, попробуйте:

$ rm -rf vendor
$ composer update

После этого повторно запустите команду.

Назад к выводу

Затем вы должны увидеть примерно такой вывод:

Виджеты WordPress: рефакторинг, часть 2

Затем выполните следующую команду на своем терминале:

$ vendor/grumphp/run

Здесь вы должны увидеть проблемы со стандартами кодирования. Список слишком длинный, чтобы перечислять его здесь, но вы должны увидеть возможность сделать что-то вроде этого:

----------------------------------------------------------------------
FOUND 9 ERRORS AFFECTING 8 LINES
----------------------------------------------------------------------

И на данный момент нам нужно внести множество изменений, если мы хотим привести его в соответствие со стандартами PSR2. Итак, давайте сделаем это сейчас.

Рефакторинг шаблона виджета

Предполагая, что у вас установлены все необходимые расширения и подключаемые модули в Visual Studio Code, вы, вероятно, увидите в своей среде IDE изрядное количество красного цвета.

Виджеты WordPress: рефакторинг, часть 2

Это все проблемы, которые необходимо исправить. Итак, я собираюсь пройти и обновить код. Тогда я поделюсь кодом здесь (без комментариев ради экономии места).

Предполагая, что вы внесли все необходимые изменения, у вас должно получиться что-то вроде этого:

<?php

namespace WordPressWidgetBoilerplate;

class Plugin extends WP_Widget
{
    protected $widgetSlug = 'widget-name';

    public function __construct()
    {
        add_action('init', [$this, 'widgetTextdomain']);

        parent::__construct(
            $this->getWidgetSlug(),
            __('Widget Name', $this->getWidgetSlug()),
            [
                'classname' => $this->getWidgetSlug().'-class',
                'description' => __('Short description of the widget goes here.', $this->getWidgetSlug()),
            ]
        );

        add_action('admin_print_styles', [$this, 'registerAdminStyles']);
        add_action('admin_enqueue_scripts', [$this, 'registerAdminScripts']);

        add_action('wp_enqueue_scripts', [$this, 'registerWidgetStyles']);
        add_action('wp_enqueue_scripts', [$this, 'registerWidgetScripts']);

        add_action('save_post', [$this, 'flushWidgetCache']);
        add_action('deleted_post', [$this, 'flushWidgetCache']);
        add_action('switch_theme', [$this, 'flushWidgetCache']);
    }

    public function getWidgetSlug()
    {
        return $this->widgetSlug;
    }

    public function widget($args, $instance)
    {
        // Check if there is a cached output
        $cache = wp_cache_get($this->getWidgetSlug(), 'widget');

        if (!is_array($cache)) {
            $cache = [];
        }

        if (!isset($args['widget_id'])) {
            $args['widget_id'] = $this->id;
        }

        if (isset($cache[$args['widget_id']])) {
            return print $cache[$args['widget_id']];
        }

        extract($args, EXTR_SKIP);

        $widgetString = $beforeWidget;

        ob_start();
        include plugin_dir_path(__FILE__).'views/widget.php';
        $widgetString .= ob_get_clean();
        $widgetString .= $afterWidget;

        $cache[$args['widget_id']] = $widgetString;

        wp_cache_set($this->getWidgetSlug(), $cache, 'widget');

        echo $widgetString;
    }

    public function flushWidgetCache()
    {
        wp_cache_delete($this->getWidgetSlug(), 'widget');
    }

    public function update($newInstance, $oldInstance)
    {
        $instance = $oldInstance;

        // TODO: Here is where you update your widget's old values with the new, incoming values

        return $instance;
    }

    public function form($instance)
    {
        // TODO: Define default values for your variables
        $instance = wp_parse_args(
            (array) $instance
        );

        // TODO: Store the values of the widget in their own variable

        // Display the admin form
        include plugin_dir_path(__FILE__).'views/admin.php';
    }

    public function widgetTextdomain()
    {
        // TODO: be sure to change 'widget-name' to the name of *your* plugin
        load_plugin_textdomain($this->getWidgetSlug(), false, dirname(plugin_basename(__FILE__)).'lang/');
    }

    public static function activate($networkWide)
    {
        // TODO: define activation functionality here
    }

    public static function deactivate($networkWide)
    {
        // TODO:define deactivation functionality here
    }

    public function registerAdminStyles()
    {
        wp_enqueue_style($this->getWidgetSlug().'-admin-styles', plugins_url('css/admin.css', __FILE__));
    }

    public function registerAdminScripts()
    {
        wp_enqueue_script($this->getWidgetSlug().'-admin-script', plugins_url('js/admin.js', __FILE__), ['jquery']);
    }

    public function registerWidgetStyles()
    {
        wp_enqueue_style($this->getWidgetSlug().'-widget-styles', plugins_url('css/widget.css', __FILE__));
    }

    public function registerWidgetScripts()
    {
        wp_enqueue_script($this->getWidgetSlug().'-script', plugins_url('js/widget.js', __FILE__), ['jquery']);
    }
}

Еще раз запустите:

$ vendor/bin/grumphp run

И посмотрите, какой результат у вас получится. Вы по-прежнему будете получать множество ошибок, а именно то, что говорит о тональности:

Избегайте неиспользуемых локальных переменных, таких как …

Это связано с тем, что правила анализа кода не хотят, чтобы мы использовали переменные, которые не используются; однако в шаблоне мы, вероятно, будем использовать эти переменные до конца проекта.

Так что, с этой целью, на данный момент нет необходимости беспокоиться об этом. Мы займемся этим, когда придет время.

До ПСР

На данный момент большая часть плагина была куплена в соответствии со стандартами PSR и имеет солидный набор инструментов, которые мы можем использовать для последовательного тестирования нашего кода во время его написания.

Если вы следите за репозиторием, ветка разработки была обновлена, чтобы отразить изменения в этом посте, поэтому не стесняйтесь извлекать ее и просматривать код.

В следующем посте мы начнем рефакторинг кода в более объектно-ориентированной манере.

Источник записи: tommcfarlin.com

Этот веб-сайт использует файлы cookie для улучшения вашего опыта. Мы предполагаем, что вы согласны с этим, но вы можете отказаться, если хотите. Принимаю Подробнее