{"id":230625,"date":"2022-12-16T14:33:00","date_gmt":"2022-12-16T11:33:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=230625"},"modified":"2022-12-16T14:34:21","modified_gmt":"2022-12-16T11:34:21","slug":"widzety-wordpress-refaktoryzacja-czesc-2","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/pl\/widzety-wordpress-refaktoryzacja-czesc-2\/","title":{"rendered":"Wid\u017cety WordPress: Refaktoryzacja, cz\u0119\u015b\u0107 2"},"content":{"rendered":"\n<p><strong><a href=\"https:\/\/wordpress.mediadoma.com\/pl\/widzety-wordpress-refaktoryzacja-czesc-1\/\" title=\"Po przygotowaniu wszystkich wst\u0119pnych tre\u015bci\">Po przygotowaniu wszystkich wst\u0119pnych tre\u015bci<\/a><\/strong>, jeste\u015bmy w ko\u0144cu w miejscu, w kt\u00f3rym jeste\u015bmy gotowi do rozpocz\u0119cia rozwi\u0105zywania standardowych problem\u00f3w zwi\u0105zanych z kodowaniem rzuconych przez nasze IDE i nasze narz\u0119dzia jako\u015bci kodu.<\/p>\n<p>Robi\u0105c to, podziel\u0119 tre\u015b\u0107 na dwa posty:<\/p>\n<ul>\n<li>pierwszy post skupi si\u0119 wy\u0142\u0105cznie na refaktoryzacji istniej\u0105cego kodu,<\/li>\n<li>w nast\u0119pnym po\u015bcie przyjrzymy si\u0119 refaktoryzacji struktury wtyczki w celu usprawnienia organizacji i architektury.<\/li>\n<\/ul>\n<p>Na razie jednak przyjrzyjmy si\u0119 b\u0142\u0119dom zg\u0142aszanym przez sniffer kodu i zobaczmy, czy nie mo\u017cemy go dostosowa\u0107 do bardziej nowoczesnych standard\u00f3w.<\/p>\n<h2>WordPress Widget Boilerplate: Refaktoryzacja, cz\u0119\u015b\u0107 2<\/h2>\n<p>Zwr\u00f3\u0107 uwag\u0119, \u017ce wci\u0105\u017c jestem w ga\u0142\u0119zi <strong>deweloperskiej<\/strong>, poniewa\u017c nie jeste\u015bmy jeszcze gotowi, aby po\u0142\u0105czy\u0107 j\u0105 z masterem. A je\u015bli masz poprawnie skonfigurowane sniffery kodu i inne narz\u0119dzia, powiniene\u015b zobaczy\u0107 <strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/dce6bda0d104192a21cb06dc7d3ca23c#file-00-regex-results-txt\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">nast\u0119puj\u0105ce informacje:<\/a><\/strong><\/p>\n<pre><code>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\nStack trace:\n#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)\n#1 \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/php-cs-fixer(62): require_once()\n#2 {main}\n  thrown in \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/php-cs-fixer on line 62\n\nCall Stack:\n    0.0041     365448   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/php-cs-fixer:0\n\nFatal 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\n\nCall Stack:\n    0.0041     365448   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/php-cs-fixer:0\n\nYou can fix all errors by running following commands:\n'\/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'\nERROR: Ruleset \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/phpcs.xml is not valid\n\nRun \"phpcs --help\" for usage information\n\nWarning: 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\n\nCall Stack:\n    0.0035     352864   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/parallel-lint:0\n\nFatal 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\n\nCall Stack:\n    0.0035     352864   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/parallel-lint:0\n\nPHP 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\nPHP Stack trace:\nPHP   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/parallel-lint:0\nPHP 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\nPHP Stack trace:\nPHP   1. {main}() \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/vendor\/bin\/parallel-lint:0\n\/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.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:33     The class Widget_Name is not named in CamelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:33     The property$widget_slug is not named in camelCase.\n\/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.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$widget_string is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$before_widget is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$widget_string is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$widget_string is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$after_widget is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$widget_string is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    The variable$widget_string is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:111    Avoid unusedparameters such as '$instance'.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:131    Avoid unusedlocal variables such as '$before_widget'.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:137    Avoid unusedlocal variables such as '$after_widget'.\n\/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.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:159    The parameter $new_instance is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:159    The parameter $old_instance is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:159    The variable$old_instance is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:159    Avoid unusedparameters such as '$new_instance'.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:195    The method widget_textdomain is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:207    The parameter $network_wide is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:207    Avoid unusedparameters such as '$network_wide'.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:216    The parameter $network_wide is not named in camelCase.\n\/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/src\/Plugin.php:216    Avoid unusedparameters such as '$network_wide'.\n\/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.\n\/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.\n\/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.\n\/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.\n[FATAL] \/Users\/tommcfarlin\/Dropbox\/Projects\/trunk\/wp-content\/plugins\/WordPress-Widget-Boilerplate\/phpcs.xml: Premature end of data in tag ruleset line 2\n (77) on line 28,12<\/code><\/pre>\n<p>I powiniene\u015b zobaczy\u0107 przynajmniej to wszystko po uruchomieniu:<\/p>\n<pre><code>$ vendor\/bin\/grumphp run<\/code><\/pre>\n<p>Zanim przejdziemy dalej, musimy wprowadzi\u0107 kilka drobnych zmian w organizacji plik\u00f3w. Oznacza to, \u017ce musimy podzieli\u0107 g\u0142\u00f3wny plik wtyczki (kt\u00f3ry mamy jako <strong>Plugin.php<\/strong>) na dwa pliki.<\/p>\n<p>W szczeg\u00f3lno\u015bci potrzebujemy mie\u0107 plik startowy wtyczki, a nast\u0119pnie musimy zachowa\u0107 g\u0142\u00f3wny plik wtyczki. Wi\u0119c najpierw stw\u00f3rzmy bootstrap wtyczek.<\/p>\n<h3>Stw\u00f3rz Bootstrap<\/h3>\n<p>W katalogu g\u0142\u00f3wnym wtyczki utw\u00f3rz plik o nazwie <strong>wordpress-widget-boilerplate.php.<\/strong> Nast\u0119pnie <strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/dce6bda0d104192a21cb06dc7d3ca23c#file-01-wordpress-widget-boilerplate-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">dodaj do niego<\/a><\/strong> :<\/p>\n<pre><code>&lt;?php\n\/**\n * WordPress Widget Boilerplate\n *\n * The WordPress Widget Boilerplate is an organized, maintainable boilerplate for building widgets using WordPress best practices.\n *\n * @package   Widget_Name\n * @author    Your Name &lt;email@example.com&gt;\n * @license   GPL-2.0+\n * @link      http:\/\/example.com\n * @copyright 2018 Your Name or Company Name\n *\n * @wordpress-plugin\n * Plugin Name:       @TODO\n * Plugin URI:        @TODO\n * Description:       @TODO\n * Version:           1.0.0\n * Author:            @TODO\n * Author URI:        @TODO\n * Text Domain:       widget-name\n * License:           GPL-3.0+\n * License URI:       http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\n * Domain Path:       \/lang\n * GitHub Plugin URI: https:\/\/github.com\/&lt;owner&gt;\/&lt;repo&gt;\n *\/\n\n\/\/ Prevent this file from being called directly.\ndefined('WPINC') || die;\n\n\/\/ Include the autoloader.\nrequire_once __DIR__. '\/inc\/autoload.php';\n<\/code><\/pre>\n<p>Uwaga w powy\u017cszym pliku; mamy nag\u0142\u00f3wek wtyczki, warunek okre\u015blaj\u0105cy, czy mo\u017cna uzyska\u0107 dost\u0119p do pliku, a nast\u0119pnie \u0142adowanie autoloadera Composera.<\/p>\n<h3>Edytuj podstawow\u0105 wtyczk\u0119<\/h3>\n<p>Gdy to zrobisz, zmieni to podstawowy plik <strong>Plugin.php na<\/strong> <strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/dce6bda0d104192a21cb06dc7d3ca23c#file-02-plugin-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">nast\u0119puj\u0105cy:<\/a><\/strong><\/p>\n<pre><code>&lt;?php\n\n\/\/ TODO: change 'Widget_Name' to the name of your plugin\nclass Widget_Name extends WP_Widget {\n\n    \/**\n     * @TODO - Rename \"widget-name\" to the name your your widget\n     *\n     * Unique identifier for your widget.\n     *\n     *\n     * The variable name is used as the text domain when internationalizing strings\n     * of text. Its value should match the Text Domain file header in the main\n     * widget file.\n     *\n     * @since    1.0.0\n     *\n     * @var      string\n     *\/\n    protected $widget_slug = 'widget-name';\n\n    \/*--------------------------------------------------*\/\n    \/* Constructor\n    \/*--------------------------------------------------*\/\n\n    \/**\n     * Specifies the classname and description, instantiates the widget,\n     * loads localization files, and includes necessary stylesheets and JavaScript.\n     *\/\n    public function __construct() {\n\n        \/\/ load plugin text domain\n        add_action( 'init', array( $this, 'widget_textdomain') );\n\n        \/\/ TODO: update description\n        parent::__construct(\n            $this-&gt;get_widget_slug(),\n            __( 'Widget Name', $this-&gt;get_widget_slug() ),\n            array(\n                'classname'  =&gt; $this-&gt;get_widget_slug().'-class',\n                'description' =&gt; __( 'Short description of the widget goes here.', $this-&gt;get_widget_slug())) );\n\n        \/\/ Register admin styles and scripts\n        add_action( 'admin_print_styles', array( $this, 'register_admin_styles') );\n        add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts') );\n\n        \/\/ Register site styles and scripts\n        add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_styles') );\n        add_action( 'wp_enqueue_scripts', array( $this, 'register_widget_scripts') );\n\n        \/\/ Refreshing the widget's cached output with each new post\n        add_action( 'save_post',    array( $this, 'flush_widget_cache') );\n        add_action( 'deleted_post', array( $this, 'flush_widget_cache') );\n        add_action( 'switch_theme', array( $this, 'flush_widget_cache') );\n\n    } \/\/ end constructor\n\n    \/**\n     * Return the widget slug.\n     *\n     * @since    1.0.0\n     *\n     * @return    Plugin slug variable.\n     *\/\n    public function get_widget_slug() {\n        return $this-&gt;widget_slug;\n    }\n\n    \/*--------------------------------------------------*\/\n    \/* Widget API Functions\n    \/*--------------------------------------------------*\/\n\n    \/**\n     * Outputs the content of the widget.\n     *\n     * @param array args  The array of form elements\n     * @param array instance The current instance of the widget\n     *\/\n    public function widget( $args, $instance) {\n\n        \/\/ Check if there is a cached output\n        $cache = wp_cache_get( $this-&gt;get_widget_slug(), 'widget' );\n\n        if (!is_array( $cache)) $cache = array();\n\n        if (! isset ($args['widget_id'])) $args['widget_id'] = $this-&gt;id;\n\n        if (isset ($cache[ $args['widget_id'] ])) return print $cache[ $args['widget_id'] ];\n\n        \/\/ go on with your widget logic, put everything into a string and \u2026\n\n        extract( $args, EXTR_SKIP );\n\n        $widget_string = $before_widget;\n\n        \/\/ TODO: Here is where you manipulate your widget's values based on their input fields\n        ob_start();\n        include( plugin_dir_path( __FILE__ ). 'views\/widget.php' );\n        $widget_string .= ob_get_clean();\n        $widget_string .= $after_widget;\n\n        $cache[ $args['widget_id'] ] = $widget_string;\n\n        wp_cache_set( $this-&gt;get_widget_slug(), $cache, 'widget' );\n\n        print $widget_string;\n\n    } \/\/ end widget\n\n    public function flush_widget_cache()\n    {\n        wp_cache_delete( $this-&gt;get_widget_slug(), 'widget' );\n    }\n    \/**\n     * Processes the widget's options to be saved.\n     *\n     * @param array new_instance The new instance of values to be generated via the update.\n     * @param array old_instance The previous instance of values before the update.\n     *\/\n    public function update( $new_instance, $old_instance) {\n\n        $instance = $old_instance;\n\n        \/\/ TODO: Here is where you update your widget's old values with the new, incoming values\n\n        return $instance;\n\n    } \/\/ end update\n\n    \/**\n     * Generates the administration form for the widget.\n     *\n     * @param array instance The array of keys and values for the widget.\n     *\/\n    public function form( $instance) {\n\n        \/\/ TODO: Define default values for your variables\n        $instance = wp_parse_args(\n            (array) $instance\n        );\n\n        \/\/ TODO: Store the values of the widget in their own variable\n\n        \/\/ Display the admin form\n        include( plugin_dir_path(__FILE__). 'views\/admin.php' );\n\n    } \/\/ end form\n\n    \/*--------------------------------------------------*\/\n    \/* Public Functions\n    \/*--------------------------------------------------*\/\n\n    \/**\n     * Loads the Widget's text domain for localization and translation.\n     *\/\n    public function widget_textdomain() {\n\n        \/\/ TODO be sure to change 'widget-name' to the name of *your* plugin\n        load_plugin_textdomain( $this-&gt;get_widget_slug(), false, dirname( plugin_basename( __FILE__) ). 'lang\/' );\n\n    } \/\/ end widget_textdomain\n\n    \/**\n     * Fired when the plugin is activated.\n     *\n     * @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.\n     *\/\n    public static function activate( $network_wide) {\n        \/\/ TODO define activation functionality here\n    } \/\/ end activate\n\n    \/**\n     * Fired when the plugin is deactivated.\n     *\n     * @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\n     *\/\n    public static function deactivate( $network_wide) {\n        \/\/ TODO define deactivation functionality here\n    } \/\/ end deactivate\n\n    \/**\n     * Registers and enqueues admin-specific styles.\n     *\/\n    public function register_admin_styles() {\n\n        wp_enqueue_style( $this-&gt;get_widget_slug().'-admin-styles', plugins_url( 'css\/admin.css', __FILE__) );\n\n    } \/\/ end register_admin_styles\n\n    \/**\n     * Registers and enqueues admin-specific JavaScript.\n     *\/\n    public function register_admin_scripts() {\n\n        wp_enqueue_script( $this-&gt;get_widget_slug().'-admin-script', plugins_url( 'js\/admin.js', __FILE__ ), array('jquery') );\n\n    } \/\/ end register_admin_scripts\n\n    \/**\n     * Registers and enqueues widget-specific styles.\n     *\/\n    public function register_widget_styles() {\n\n        wp_enqueue_style( $this-&gt;get_widget_slug().'-widget-styles', plugins_url( 'css\/widget.css', __FILE__) );\n\n    } \/\/ end register_widget_styles\n\n    \/**\n     * Registers and enqueues widget-specific scripts.\n     *\/\n    public function register_widget_scripts() {\n\n        wp_enqueue_script( $this-&gt;get_widget_slug().'-script', plugins_url( 'js\/widget.js', __FILE__ ), array('jquery') );\n\n    } \/\/ end register_widget_scripts\n\n} \/\/ end class\n\n\/\/ TODO: Remember to change 'Widget_Name' to match the class name definition\nadd_action( 'widgets_init', create_function( '', 'register_widget(\"Widget_Name\");') );\n\n\/\/ Hooks fired when the Widget is activated and deactivated\n\/\/ TODO: Remember to change 'Widget_Name' to match the class name definition\nregister_activation_hook( __FILE__, array( 'Widget_Name', 'activate') );\nregister_deactivation_hook( __FILE__, array( 'Widget_Name', 'deactivate') );\n<\/code><\/pre>\n<p>Pami\u0119taj, \u017ce wci\u0105\u017c jest du\u017co pracy, kt\u00f3r\u0105 musimy wykona\u0107, ale pierwsz\u0105 rzecz\u0105, kt\u00f3r\u0105 musimy zrobi\u0107, jest rozpocz\u0119cie czyszczenia bazy kodu z jej obecnego stanu. Ostatecznie zmienimy to w spos\u00f3b znacznie bardziej zorientowany obiektowo, ale musimy uzyska\u0107 obecn\u0105 wersj\u0119 wtyczki w dobrym stanie.<\/p>\n<p>Teraz jednak mo\u017cemy zwr\u00f3ci\u0107 nasz\u0105 uwag\u0119 z powrotem na oryginalny wynik GrumPHP. Je\u015bli przejrzysz, na wyj\u015bciu zobaczysz lini\u0119 tak\u0105 jak ta:<\/p>\n<pre><code>You can fix all errors by running following commands:\n'\/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'<\/code><\/pre>\n<p>W tym momencie sp\u00f3jrzmy wi\u0119c na uruchomienie zalecanego polecenia i zobaczmy, co si\u0119 stanie. W szczeg\u00f3lno\u015bci wprowad\u017a to do swojego terminala:<\/p>\n<pre><code>'\/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'<\/code><\/pre>\n<p>Zak\u0142adaj\u0105c, \u017ce wszystko dzia\u0142a poprawnie, powiniene\u015b zobaczy\u0107 co\u015b takiego:<\/p>\n<h4>Uwaga na temat b\u0142\u0119d\u00f3w wyj\u015bciowych<\/h4>\n<p>Je\u015bli za ka\u017cdym razem, gdy to robisz, pojawi si\u0119 b\u0142\u0105d, mo\u017ce by\u0107 konieczne zaktualizowanie katalogu <strong>dostawcy<\/strong> lub nawet zaktualizowanie katalogu. Je\u015bli tak jest, najpierw spr\u00f3buj tego:<\/p>\n<pre><code>$ composer update<\/code><\/pre>\n<p>A je\u015bli to nie rozwi\u0105\u017ce problemu, spr\u00f3buj:<\/p>\n<pre><code>$ rm -rf vendor\n$ composer update<\/code><\/pre>\n<p>Nast\u0119pnie ponownie uruchom polecenie.<\/p>\n<h3>Powr\u00f3t do wyj\u015bcia<\/h3>\n<p>Powiniene\u015b wtedy zobaczy\u0107 takie wyj\u015bcie:<\/p>\n<p><a href=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-161324-61e71fbbbfefa.png\" data-rel=\"lightbox\"><img decoding=\"async\" class=\"SDStudio-light-box-enable SDStudio-editor-tools-md-imp\" src=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-161324-61e71fbbbfefa.png\" alt=\"Wid\u017cety WordPress: Refaktoryzacja, cz\u0119\u015b\u0107 2\"><\/a><\/p>\n<p>Nast\u0119pnie uruchom nast\u0119puj\u0105ce polecenie na swoim terminalu:<\/p>\n<pre><code>$ vendor\/grumphp\/run<\/code><\/pre>\n<p>Tutaj powiniene\u015b zobaczy\u0107 problemy ze standardami kodowania. Lista jest zbyt d\u0142uga, aby j\u0105 tu wymieni\u0107, ale powiniene\u015b zobaczy\u0107 opcj\u0119 zrobienia czego\u015b <strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/dce6bda0d104192a21cb06dc7d3ca23c#file-03-coding-standards-txt\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">takiego:<\/a><\/strong><\/p>\n<pre><code>----------------------------------------------------------------------\nFOUND 9 ERRORS AFFECTING 8 LINES\n----------------------------------------------------------------------<\/code><\/pre>\n<p>W tym momencie musimy wprowadzi\u0107 wiele zmian, je\u015bli chcemy dostosowa\u0107 go do standard\u00f3w PSR2. Wi\u0119c zr\u00f3bmy to teraz.<\/p>\n<h3>Refaktoryzacja Widget Boilerplate<\/h3>\n<p>Zak\u0142adaj\u0105c, \u017ce masz wszystkie niezb\u0119dne rozszerzenia i wtyczki zainstalowane w Visual Studio Code, prawdopodobnie zobaczysz sporo czerwonego w swoim IDE.<\/p>\n<p><a href=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-161324-61e71fbfb62de.png\" data-rel=\"lightbox\"><img decoding=\"async\" class=\"SDStudio-light-box-enable SDStudio-editor-tools-md-imp\" src=\"https:\/\/wordpress.mediadoma.com\/wp-content\/uploads\/2022\/01\/post-161324-61e71fbfb62de.png\" alt=\"Wid\u017cety WordPress: Refaktoryzacja, cz\u0119\u015b\u0107 2\"><\/a><\/p>\n<p>To s\u0105 wszystkie problemy, kt\u00f3re nale\u017cy naprawi\u0107. Przejd\u0119 wi\u0119c i zaktualizuj\u0119 kod. Nast\u0119pnie udost\u0119pni\u0119 kod tutaj (bez komentarzy ze wzgl\u0119du na oszcz\u0119dno\u015b\u0107 miejsca).<\/p>\n<p>Zak\u0142adaj\u0105c, \u017ce dokona\u0142e\u015b wszystkich poprawnych zmian, powiniene\u015b mie\u0107 co\u015b <strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/dce6bda0d104192a21cb06dc7d3ca23c#file-04-wordpress-widget-boilerplate-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">takiego:<\/a><\/strong><\/p>\n<pre><code>&lt;?php\n\nnamespace WordPressWidgetBoilerplate;\n\nclass Plugin extends WP_Widget\n{\n    protected $widgetSlug = 'widget-name';\n\n    public function __construct()\n    {\n        add_action('init', [$this, 'widgetTextdomain']);\n\n        parent::__construct(\n            $this-&gt;getWidgetSlug(),\n            __('Widget Name', $this-&gt;getWidgetSlug()),\n            [\n                'classname' =&gt; $this-&gt;getWidgetSlug().'-class',\n                'description' =&gt; __('Short description of the widget goes here.', $this-&gt;getWidgetSlug()),\n            ]\n        );\n\n        add_action('admin_print_styles', [$this, 'registerAdminStyles']);\n        add_action('admin_enqueue_scripts', [$this, 'registerAdminScripts']);\n\n        add_action('wp_enqueue_scripts', [$this, 'registerWidgetStyles']);\n        add_action('wp_enqueue_scripts', [$this, 'registerWidgetScripts']);\n\n        add_action('save_post', [$this, 'flushWidgetCache']);\n        add_action('deleted_post', [$this, 'flushWidgetCache']);\n        add_action('switch_theme', [$this, 'flushWidgetCache']);\n    }\n\n    public function getWidgetSlug()\n    {\n        return $this-&gt;widgetSlug;\n    }\n\n    public function widget($args, $instance)\n    {\n        \/\/ Check if there is a cached output\n        $cache = wp_cache_get($this-&gt;getWidgetSlug(), 'widget');\n\n        if (!is_array($cache)) {\n            $cache = [];\n        }\n\n        if (!isset($args['widget_id'])) {\n            $args['widget_id'] = $this-&gt;id;\n        }\n\n        if (isset($cache[$args['widget_id']])) {\n            return print $cache[$args['widget_id']];\n        }\n\n        extract($args, EXTR_SKIP);\n\n        $widgetString = $beforeWidget;\n\n        ob_start();\n        include plugin_dir_path(__FILE__).'views\/widget.php';\n        $widgetString .= ob_get_clean();\n        $widgetString .= $afterWidget;\n\n        $cache[$args['widget_id']] = $widgetString;\n\n        wp_cache_set($this-&gt;getWidgetSlug(), $cache, 'widget');\n\n        echo $widgetString;\n    }\n\n    public function flushWidgetCache()\n    {\n        wp_cache_delete($this-&gt;getWidgetSlug(), 'widget');\n    }\n\n    public function update($newInstance, $oldInstance)\n    {\n        $instance = $oldInstance;\n\n        \/\/ TODO: Here is where you update your widget's old values with the new, incoming values\n\n        return $instance;\n    }\n\n    public function form($instance)\n    {\n        \/\/ TODO: Define default values for your variables\n        $instance = wp_parse_args(\n            (array) $instance\n        );\n\n        \/\/ TODO: Store the values of the widget in their own variable\n\n        \/\/ Display the admin form\n        include plugin_dir_path(__FILE__).'views\/admin.php';\n    }\n\n    public function widgetTextdomain()\n    {\n        \/\/ TODO: be sure to change 'widget-name' to the name of *your* plugin\n        load_plugin_textdomain($this-&gt;getWidgetSlug(), false, dirname(plugin_basename(__FILE__)).'lang\/');\n    }\n\n    public static function activate($networkWide)\n    {\n        \/\/ TODO: define activation functionality here\n    }\n\n    public static function deactivate($networkWide)\n    {\n        \/\/ TODO:define deactivation functionality here\n    }\n\n    public function registerAdminStyles()\n    {\n        wp_enqueue_style($this-&gt;getWidgetSlug().'-admin-styles', plugins_url('css\/admin.css', __FILE__));\n    }\n\n    public function registerAdminScripts()\n    {\n        wp_enqueue_script($this-&gt;getWidgetSlug().'-admin-script', plugins_url('js\/admin.js', __FILE__), ['jquery']);\n    }\n\n    public function registerWidgetStyles()\n    {\n        wp_enqueue_style($this-&gt;getWidgetSlug().'-widget-styles', plugins_url('css\/widget.css', __FILE__));\n    }\n\n    public function registerWidgetScripts()\n    {\n        wp_enqueue_script($this-&gt;getWidgetSlug().'-script', plugins_url('js\/widget.js', __FILE__), ['jquery']);\n    }\n}\n<\/code><\/pre>\n<p>Jeszcze raz uruchom:<\/p>\n<pre><code>$ vendor\/bin\/grumphp run<\/code><\/pre>\n<p>I zobacz, jakie wyniki uzyskasz. Nadal b\u0119dziesz otrzymywa\u0107 r\u00f3\u017cne b\u0142\u0119dy, kt\u00f3re m\u00f3wi\u0105 co\u015b w tonie:<\/p>\n<blockquote>\n<p>Unikaj nieu\u017cywanych zmiennych lokalnych, takich jak\u2026<\/p>\n<\/blockquote>\n<p>Dzieje si\u0119 tak dlatego, \u017ce regu\u0142y pods\u0142uchiwania kodu nie chc\u0105, aby\u015bmy u\u017cywali zmiennych, kt\u00f3re nie s\u0105 u\u017cywane; jednak w schemacie prawdopodobnie b\u0119dziemy u\u017cywa\u0107 tych zmiennych przed zako\u0144czeniem projektu.<\/p>\n<p>Tak wi\u0119c w tym momencie nie musisz si\u0119 tym martwi\u0107. Zajmiemy si\u0119 tym, kiedy nadejdzie czas.<\/p>\n<h2>Do PSR<\/h2>\n<p>W tym momencie wi\u0119kszo\u015b\u0107 wtyczki zosta\u0142a zakupiona zgodnie ze standardami PSR i ma solidny zestaw narz\u0119dzi, kt\u00f3rych mo\u017cemy u\u017cywa\u0107 do konsekwentnego testowania naszego kodu podczas jego pisania.<\/p>\n<p>Je\u015bli \u015bledzisz wraz z repozytorium, <strong><a href=\"https:\/\/github.com\/tommcfarlin\/WordPress-Widget-Boilerplate\/tree\/develop\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">ga\u0142\u0105\u017a<\/a><\/strong> deweloperska zosta\u0142a zaktualizowana, aby odzwierciedli\u0107 zmiany w tym po\u015bcie, wi\u0119c mo\u017cesz j\u0105 pobra\u0107 i przejrze\u0107 kod.<\/p>\n<p>W nast\u0119pnym po\u015bcie zaczniemy refaktoryzacj\u0119 kodu w znacznie bardziej zorientowany obiektowo spos\u00f3b.<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">\u0179r\u00f3d\u0142o nagrywania:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Przyjrzyjmy si\u0119 b\u0142\u0119dom, kt\u00f3re zg\u0142asza sniffer kodu dla WordPress Widget Boilerplate i zobaczmy, czy nie mo\u017cemy go dostosowa\u0107 do bardziej nowoczesnych standard\u00f3w.<\/p>\n","protected":false},"author":1,"featured_media":236238,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[721,897,805,845,866],"tags":[1169],"class_list":["post-230625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deweloper","category-kod","category-php-7","category-samouczki","category-wordpress-7","tag-affiai-pl"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts\/230625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/comments?post=230625"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/posts\/230625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/media\/236238"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/media?parent=230625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/categories?post=230625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/pl\/wp-json\/wp\/v2\/tags?post=230625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}