Plugin Directory

Changeset 2241250


Ignore:
Timestamp:
02/09/2020 11:26:30 AM (6 years ago)
Author:
trasweb
Message:

Version 0.3:

  • Feat: Add bytecheck site
  • Feat: Add description to sites
  • Feat: Improve appearance of site selection page
Location:
wpo-checker/trunk
Files:
20 edited
1 moved

Legend:

Unmodified
Added
Removed
  • wpo-checker/trunk/config/hooks.php

    r2217761 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    34namespace Trasweb\Plugins\WpoChecker;
    45
    56use Trasweb\Plugins\WpoChecker\Framework\Hook;
    6 use Trasweb\Plugins\WpoChecker\Pages\Plugins_List;
    7 use Trasweb\Plugins\WpoChecker\Pages\Posts_List;
    8 use Trasweb\Plugins\WpoChecker\Pages\Sites_Selection;
    9 use Trasweb\Plugins\WpoChecker\Pages\Terms_List;
     7use Trasweb\Plugins\WpoChecker\Places\Plugins_List;
     8use Trasweb\Plugins\WpoChecker\Places\Posts_List;
     9use Trasweb\Plugins\WpoChecker\Places\Sites_Selection;
     10use Trasweb\Plugins\WpoChecker\Places\Terms_List;
    1011use Trasweb\Plugins\WpoChecker\Repositories\Settings;
    1112use Trasweb\Plugins\WpoChecker\Repositories\Sites;
    1213
    1314return [
    14     Hook::new('post_row_actions', [
    15         'listener' => Posts_List::class,
    16         'filter'   => 'show_wpo_links_in_cpt_row_actions',
    17     ]),
    18     Hook::new('page_row_actions', [
    19         'listener' => Posts_List::class,
    20         'filter'   => 'show_wpo_links_in_cpt_row_actions',
    21     ]),
    22     Hook::new('tag_row_actions', [
    23         'listener' => Terms_List::class,
    24         'filter'   => 'show_wpo_links_in_tags_row_actions',
    25     ]),
    26     Hook::new('admin_menu', [
    27         'listener' => Sites_Selection::class,
    28         'action'   => 'show_menu',
    29         'options'  => [
    30             'page_title' => __('Sites selection', PLUGIN_NAME),
    31             'menu_title' => __('WPO Checker', PLUGIN_NAME),
    32             'capability' => 'manage_options',
    33             'menu_slug'  => 'sites_selection_page',
    34         ],
    35     ]),
    36     Hook::new('plugin_action_links', [
    37         'listener' => Plugins_List::class,
    38         'filter'   => 'show_settings_in_plugin_links',
    39         'options'  => [
    40             'link_to_settings' => [
    41                 'capability' => 'manage_options',
    42                 'slug'       => 'sites_selection_page',
    43                 'label'      => __('Settings', PLUGIN_NAME),
    44                 'format'     => '<a href="%s">%s</a>',
    45             ],
    46         ],
    47     ]),
    48     Hook::new(PLUGIN_NAME . '-loaded', [
    49         'listener'  => Settings::class,
    50         'action'    => 'register',
    51         'with_args' => false,
    52         'options'   => [
    53             'sites' => [
    54                 'sanitize_callback' => Hook::callback(Sites::class, 'sanitize'),
    55                 'show_in_rest'      => false,
    56                 'default'           => [],
    57             ],
    58         ],
    59     ]),
     15    Hook::new( 'post_row_actions', [
     16        'listener' => Posts_List::class,
     17        'filter'   => 'show_wpo_links_in_cpt_row_actions',
     18    ] ),
     19    Hook::new( 'page_row_actions', [
     20        'listener' => Posts_List::class,
     21        'filter'   => 'show_wpo_links_in_cpt_row_actions',
     22    ] ),
     23    Hook::new( 'tag_row_actions', [
     24        'listener' => Terms_List::class,
     25        'filter'   => 'show_wpo_links_in_tags_row_actions',
     26    ] ),
     27    Hook::new( 'admin_menu', [
     28        'listener' => Sites_Selection::class,
     29        'action'   => 'show_menu',
     30        'options'  => [
     31            'page_title' => __( 'Sites selection', PLUGIN_NAME ),
     32            'menu_title' => PLUGIN_TITLE,
     33            'capability' => 'manage_options',
     34            'menu_slug'  => Plugin::CONFIG_PAGE,
     35        ],
     36    ] ),
     37    Hook::new( 'plugin_action_links', [
     38        'listener' => Plugins_List::class,
     39        'filter'   => 'show_settings_in_plugin_links',
     40        'options'  => [
     41            'link_to_settings' => [
     42                'capability' => 'manage_options',
     43                'slug'       => Plugin::CONFIG_PAGE,
     44                'label'      => __( 'Settings', PLUGIN_NAME ),
     45                'format'     => '<a href="%s">%s</a>',
     46            ],
     47        ],
     48    ] ),
     49    Hook::new( 'wp_redirect', [
     50        'listener' => Plugins_List::class,
     51        'filter'   => 'redirect_to_sites_selection',
     52    ] ),
     53    Hook::new( PLUGIN_NAME . '-loaded', [
     54        'listener'  => Settings::class,
     55        'action'    => 'register',
     56        'with_args' => false,
     57        'options'   => [
     58            'sites' => [
     59                'sanitize_callback' => Hook::callback( Sites::class, 'sanitize' ),
     60                'show_in_rest'      => false,
     61                'default'           => [],
     62            ],
     63        ],
     64    ] ),
    6065];
  • wpo-checker/trunk/config/sites.ini

    r2217761 r2241250  
    33name = Gtmetrix
    44link = https://gtmetrix.com/analyze.html
     5description = "GTmetrix is a free tool that analyzes your page&#039;s speed performance.  Using PageSpeed and YSlow, GTmetrix generates scores for your pages and offers actionable recommendations on how to fix them."
    56
    67[pagespeed]
     
    1011link = https://developers.google.com/speed/pagespeed/insights/
    1112method = get
     13description = "Make your web pages fast on all devices"
    1214
    1315[webpagetest]
     
    1618short_name = WebPageTest
    1719link = https://www.webpagetest.org/
     20description = "Run a free website speed test from around the globe using real browsers at consumer connection speeds with detailed optimization recommendations."
    1821
    1922[pagespeedo]
     
    2225short_name = Speed Otimization
    2326link = https://varvy.com/pagespeed/speedcombined.php
     27description = "Test your site to see if it follows the Google guidelines."
    2428
    2529[yellowlab]
     
    2933link = https://yellowlab.tools/
    3034method = get
     35description = "Free online web performance tool. Audit your webpage for performance and front-end quality issues. And it&apos;s open-source!"
    3136
    3237[batchspeed]
     
    3540link = https://batchspeed.com/test/
    3641var = parent-url
     42description = "Batch Speed Test"
    3743
    3844[keycdn]
     
    4248link = https://tools.keycdn.com/performance
    4349method = get
     50description = "Single asset performance test from 14 locations. Analyze the TTFB, DNS, TLS and connection times for any URL."
     51
     52[bytecheck]
     53id = bytecheck
     54name = Bytecheck
     55link = https://www.bytecheck.com/results
     56method = get
     57var = resource
     58description = "Check the load speeds of and information of a given website"
  • wpo-checker/trunk/readme.txt

    r2218106 r2241250  
    1212
    1313== Description ==
    14 WPO Checker allows to you to check performance of  your posts and terms. You'll test your website contents  with Gtmetrix, Google PageSpeed Insights, Web Page Test, Page Speed Optimization, Yellow Lab Tools and Batch Speed.
     14WPO Checker allows to you to check performance of  your posts and terms. You'll test your website contents  with Gtmetrix, Google PageSpeed Insights, Web Page Test, Page Speed Optimization, Yellow Lab Tools,  Batch Speed, Keycdn Performance Test and Bytecheck.
     15
     16== More information about WPO( in Spanish ) ==
     17
     18You can see [my WPO posts](https://trasweb.net/webperf) in order to learn about Web Performance( or WebPerf ).
    1519
    1620== Send me bugs or improvements ==
     
    3337
    3438== Changelog ==
    35 = 0.1 / 2019-12-24 =
    36 * Initial release. Support to test using Gtmetrix, Google PageSpeed Insights, Web Page Test, Page Speed Optimization, Yellow Lab Tools and Batch Speed
     39More detailed changes in [WPO Checker GitHub Repository](https://github.com/trasweb/wpo-checker/).
     40
     41= 0.3 / 2019-02-09 =
     42* Feat: Add bytecheck site
     43* Feat: Add description to sites
     44* Feat: Improve appearance of site selection page
    3745
    3846= 0.2 / 2019-12-25 =
    3947* Feat: Add Keycdn Performance Test
    4048* Fix: remove unnecessary code which breaks uninstall.
    41 * Style: pass phpcbf to files.
    42 * Fix: readme.
     49
     50= 0.1 / 2019-12-24 =
     51* Initial release. Support to test using Gtmetrix, Google PageSpeed Insights, Web Page Test, Page Speed Optimization, Yellow Lab Tools and Batch Speed
     52
    4353
    4454== Upgrade Notice ==
    4555= 0.1 =
    4656First version. Install it
    47 
  • wpo-checker/trunk/src/Collections/class-sites.php

    r2217624 r2241250  
    1212 * @package Collections
    1313 */
    14 final class Sites implements Iterator, Countable {
    15     /**
    16      * @var array
    17      */
    18     private $wpo_sites;
    19     /**
    20      * @var int
    21      */
    22     private $current_site;
     14final class Sites implements Iterator, Countable
     15{
     16    /**
     17     * @var array
     18     */
     19    private $wpo_sites;
     20    /**
     21     * @var int
     22     */
     23    private $current_site;
    2324
    24     /**
    25      * Sites constructor.
    26      *
    27      * @param array $sites
    28      */
    29     final public function __construct( array $sites ) {
    30         $this->rewind();
    31         $this->wpo_sites = array_values( $sites );
    32     }
     25    /**
     26     * Sites constructor.
     27     *
     28     * @param array $sites
     29     */
     30    final public function __construct(array $sites)
     31    {
     32        $this->rewind();
     33        $this->wpo_sites = array_values($sites);
     34    }
    3335
    34     /**
    35      * Rewind the Iterator to the first element.
    36      *
    37      * @link  https://php.net/manual/en/iterator.rewind.php
    38      * @return void Any returned value is ignored.
    39      * @since 5.0.0
    40      */
    41     final public function rewind() {
    42         $this->current_site = 0;
    43     }
     36    /**
     37     * Rewind the Iterator to the first element.
     38     *
     39     * @link  https://php.net/manual/en/iterator.rewind.php
     40     * @return void Any returned value is ignored.
     41     * @since 5.0.0
     42     */
     43    final public function rewind()
     44    {
     45        $this->current_site = 0;
     46    }
    4447
    45     /**
    46      * Return the current element.
    47      *
    48      * @link  https://php.net/manual/en/iterator.current.php
    49      * @return mixed Can return any type.
    50      * @since 5.0.0
    51      */
    52     final public function current() {
    53         return new Site( $this->key(), $this->get_current_site() );
    54     }
     48    /**
     49     * Return the current element.
     50     *
     51     * @link  https://php.net/manual/en/iterator.current.php
     52     * @return mixed Can return any type.
     53     * @since 5.0.0
     54     */
     55    final public function current()
     56    {
     57        return new Site($this->key(), $this->get_current_site());
     58    }
    5559
    56     /**
    57      * Return the key of the current element.
    58      *
    59      * @link  https://php.net/manual/en/iterator.key.php
    60      * @return mixed scalar on success, or null on failure.
    61      * @since 5.0.0
    62      */
    63     final public function key() {
    64         return $this->get_current_site()['id'];
    65     }
     60    /**
     61     * Return the key of the current element.
     62     *
     63     * @link  https://php.net/manual/en/iterator.key.php
     64     * @return mixed scalar on success, or null on failure.
     65     * @since 5.0.0
     66     */
     67    final public function key()
     68    {
     69        return $this->get_current_site()['id'];
     70    }
    6671
    67     /**
    68      * Retrieve current site in loop.
    69      *
    70      * @return array
    71      */
    72     final private function get_current_site(): array {
    73         return $this->wpo_sites[ $this->current_site ] ?? [];
    74     }
     72    /**
     73     * Retrieve current site in loop.
     74     *
     75     * @return array
     76     */
     77    final private function get_current_site(): array
     78    {
     79        return $this->wpo_sites[ $this->current_site ] ?? [];
     80    }
    7581
    76     /**
    77      * Move forward to next element.
    78      *
    79      * @link  https://php.net/manual/en/iterator.next.php
    80      * @return void
    81      * @since 5.0.0
    82      */
    83     final public function next(): void {
    84         $this->current_site ++;
    85     }
     82    /**
     83     * Move forward to next element.
     84     *
     85     * @link  https://php.net/manual/en/iterator.next.php
     86     * @return void
     87     * @since 5.0.0
     88     */
     89    final public function next(): void
     90    {
     91        $this->current_site ++;
     92    }
    8693
    87     /**
    88      * Checks if current position is valid.
    89      *
    90      * @link  https://php.net/manual/en/iterator.valid.php
    91      * @return boolean The return value will be casted to boolean and then evaluated.
    92      * Returns true on success or false on failure.
    93      * @since 5.0.0
    94      */
    95     final public function valid(): bool {
    96         return ! empty( $this->get_current_site() );
    97     }
     94    /**
     95     * Checks if current position is valid.
     96     *
     97     * @link  https://php.net/manual/en/iterator.valid.php
     98     * @return boolean The return value will be casted to boolean and then evaluated.
     99     * Returns true on success or false on failure.
     100     * @since 5.0.0
     101     */
     102    final public function valid(): bool
     103    {
     104        return ! empty($this->get_current_site());
     105    }
    98106
    99     /**
    100      * Retrieve count of sites.
    101      *
    102      * @return int
    103      */
    104     public function count(): int {
    105         return count( $this->wpo_sites );
    106     }
     107    /**
     108     * Retrieve count of sites.
     109     *
     110     * @return int
     111     */
     112    public function count(): int
     113    {
     114        return count($this->wpo_sites);
     115    }
    107116}
  • wpo-checker/trunk/src/Entities/class-site.php

    r2217624 r2241250  
    1616    public $input_name;
    1717    public $active;
     18    public $description;
    1819
    1920    /**
     
    2425     */
    2526    final public function __construct( string $current_site_id, array $current_site_options ) {
    26         $this->id         = $current_site_id;
    27         $this->name       = $current_site_options['name'];
    28         $this->short_name = $current_site_options['short_name'] ?? $this->name;
    29         $this->link       = $current_site_options['link'];
    30         $this->method     = $current_site_options['method'] ?? 'post';
    31         $this->input_name = $current_site_options['var'] ?? 'url';
    32         $this->active     = $current_site_options['active'] ?? true;
     27        $this->id          = $current_site_id;
     28        $this->name        = $current_site_options['name'];
     29        $this->short_name  = $current_site_options['short_name'] ?? $this->name;
     30        $this->link        = $current_site_options['link'];
     31        $this->method      = $current_site_options['method'] ?? 'post';
     32        $this->input_name  = $current_site_options['var'] ?? 'url';
     33        $this->active      = $current_site_options['active'] ?? true;
     34        $this->description = $current_site_options['description'] ?? '';
    3335    }
    3436}
  • wpo-checker/trunk/src/Framework/class-autoload.php

    r2217624 r2241250  
    88 * @package Framework
    99 */
    10 final class Autoload {
    11     /**
    12      * @var string
    13      */
    14     private $base_namespace;
    15     /**
    16      * @var string
    17      */
    18     private $base_dir;
     10final class Autoload
     11{
     12    /**
     13     * @var string
     14     */
     15    private $base_namespace;
     16    /**
     17     * @var string
     18     */
     19    private $base_dir;
    1920
    20     /**
    21      * Autoload constructor.
    22      *
    23      * @param $base_namespace
    24      * @param $base_dir
    25      */
    26     final private function __construct( string $base_namespace, string $base_dir ) {
    27         $this->base_namespace = $base_namespace;
    28         $this->base_dir       = $base_dir;
    29     }
     21    /**
     22     * Autoload constructor.
     23     *
     24     * @param $base_namespace
     25     * @param $base_dir
     26     */
     27    final private function __construct(string $base_namespace, string $base_dir)
     28    {
     29        $this->base_namespace = $base_namespace;
     30        $this->base_dir       = $base_dir;
     31    }
    3032
    31     /**
    32      * Named constructor.
    33      *
    34      * @param array $options
    35      *
    36      * @return static
    37      */
    38     final public static function get_instance( array $options ): self {
    39         return new self( $options['base_namespace'], $options['base_dir'] );
    40     }
     33    /**
     34     * Named constructor.
     35     *
     36     * @param array $options
     37     *
     38     * @return static
     39     */
     40    final public static function get_instance(array $options): self
     41    {
     42        return new self($options['base_namespace'], $options['base_dir']);
     43    }
    4144
    42     /**
    43      * Find a class according to its name.
    44      *
    45      * @param string $class_name
    46      *
    47      * @return void
    48      */
    49     final public function find_class( string $class_name ): void {
    50         $class_relative_namespace = explode( $this->base_namespace, $class_name )[1] ?? '';
     45    /**
     46     * Find a class according to its name.
     47     *
     48     * @param string $class_name
     49     *
     50     * @return void
     51     */
     52    final public function find_class(string $class_name): void
     53    {
     54        $class_relative_namespace = explode($this->base_namespace, $class_name)[1] ?? '';
    5155
    52         if ( ! $class_relative_namespace ) {
    53             return;
    54         }
     56        if (! $class_relative_namespace) {
     57            return;
     58        }
    5559
    56         $class_name = $this->class_namespace_to_class_name( $class_relative_namespace );
    57         $file_name  = $this->class_name_to_file_name( $class_name );
    58         $class_path = $this->class_path_from_file( $class_relative_namespace, $class_name );
     60        $class_name = $this->class_namespace_to_class_name($class_relative_namespace);
     61        $file_name  = $this->class_name_to_file_name($class_name);
     62        $class_path = $this->class_path_from_file($class_relative_namespace, $class_name);
    5963
    60         require( $this->base_dir . "{$class_path}{$file_name}" );
    61     }
     64        require($this->base_dir . "{$class_path}{$file_name}");
     65    }
    6266
    63     /**
    64      * Retrieve class name from class namespace.
    65      *
    66      * @example With `\Trasweb\Plugins\Wpo_Checker` class namespace ===> We'll get `Wpo_Checker` as class name.
    67      *
    68      * @param string $class_file
    69      *
    70      * @return string
    71      */
    72     final private function class_namespace_to_class_name( string $class_file ): string {
    73         //cut for the last '\'
    74         return  trim( strrchr( $class_file, '\\' ), '\\' );
    75     }
     67    /**
     68     * Retrieve class name from class namespace.
     69     *
     70     * @example With `\Trasweb\Plugins\Wpo_Checker` class namespace ===> We'll get `Wpo_Checker` as class name.
     71     *
     72     * @param string $class_file
     73     *
     74     * @return string
     75     */
     76    final private function class_namespace_to_class_name(string $class_file): string
     77    {
     78        //cut for the last '\'
     79        return  trim(strrchr($class_file, '\\'), '\\');
     80    }
    7681
    77     /**
    78      * Retrieve name of file from clas name.
    79      *
    80      * @example With `Wpo_Checker` class name ===> We'll get `class-wpo-checker.php`
    81      *
    82      * @param string $class_name
    83      *
    84      * @return string
    85      */
    86     final private function class_name_to_file_name( string $class_name ): string {
    87         //Wpo_Checker => wpo-checker
    88         $snake_case_name = strtolower( str_replace( '_', '-', $class_name) );
     82    /**
     83     * Retrieve name of file from clas name.
     84     *
     85     * @example With `Wpo_Checker` class name ===> We'll get `class-wpo-checker.php`
     86     *
     87     * @param string $class_name
     88     *
     89     * @return string
     90     */
     91    final private function class_name_to_file_name(string $class_name): string
     92    {
     93        //Wpo_Checker => wpo-checker
     94        $snake_case_name = strtolower(str_replace('_', '-', $class_name));
    8995
    90         //wpo_checker => class-wpo-checker.php
    91         $file_name = "class-{$snake_case_name}.php";
     96        //wpo_checker => class-wpo-checker.php
     97        $file_name = "class-{$snake_case_name}.php";
    9298
    93         return $file_name;
    94     }
     99        return $file_name;
     100    }
    95101
    96     /**
    97      * Extract path part from namespace and convert this a path.
    98      *
    99      * @param $class_file
    100      * @param $file_name
    101      *
    102      * @return string
    103      */
    104     final private function class_path_from_file( $class_relative_namespace, $file_name ): string {
    105         //\Trasweb\Plugins\Entitites/Site  ==>  \Trasweb\Plugins\Entities
    106         $namespace_path = substr( $class_relative_namespace, 0, - strlen( $file_name ) );
     102    /**
     103     * Extract path part from namespace and convert this a path.
     104     *
     105     * @param $class_file
     106     * @param $file_name
     107     *
     108     * @return string
     109     */
     110    final private function class_path_from_file($class_relative_namespace, $file_name): string
     111    {
     112        //\Trasweb\Plugins\Entitites/Site  ==>  \Trasweb\Plugins\Entities
     113        $namespace_path = substr($class_relative_namespace, 0, - strlen($file_name));
    107114
    108         //\Trasweb\Plugins\\Entities => /Trasweb/Plugins/Entities
    109         return str_replace( '\\', '/', $namespace_path );
    110     }
     115        //\Trasweb\Plugins\\Entities => /Trasweb/Plugins/Entities
     116        return str_replace('\\', '/', $namespace_path);
     117    }
    111118}
  • wpo-checker/trunk/src/Framework/class-hook.php

    r2217624 r2241250  
    88 * @package Framework
    99 */
    10 class Hook {
    11     private $id;
    12     private $listener;
    13     private $method;
    14     private $type;
    15     private $priority;
    16     private $options;
    17     private $with_args;
    18     private $expand_args;
    19     const COMMON_UP_METHOD = 'commons';
    20     const COMMON_DOWN_METHOD = 'down';
     10class Hook
     11{
     12    private $id;
     13    private $listener;
     14    private $method;
     15    private $type;
     16    private $priority;
     17    private $options;
     18    private $with_args;
     19    private $expand_args;
     20    const COMMON_UP_METHOD = 'commons';
     21    const COMMON_DOWN_METHOD = 'down';
    2122
    22     /**
    23      * Create a new hook.
    24      *
    25      * @param string $hook_id WP tag.
    26      * @param array  $hook_options
    27      *
    28      * @return Hook
    29      */
    30     final public static function new( string $hook_id, array $hook_options ): self {
    31         $hook = new Hook();
     23    /**
     24     * Create a new hook.
     25     *
     26     * @param string $hook_id WP tag.
     27     * @param array  $hook_options
     28     *
     29     * @return Hook
     30     */
     31    final public static function new(string $hook_id, array $hook_options): self
     32    {
     33        $hook = new Hook();
    3234
    33         $hook->id          = $hook_id;
    34         $hook->listener    = $hook_options['listener'];
    35         $hook->method      = $hook_options['filter'] ?? $hook_options['action'];
    36         $hook->type        = isset( $hook_options['filter'] ) ? 'filter' : 'action';
    37         $hook->priority    = $hook_options['priority'] ?? 10;
    38         $hook->options     = $hook_options['options'] ?? [];
    39         $hook->with_args   = $hook_options['with_args'] ?? true;
    40         $hook->expand_args = $hook_options['expand_args'] ?? $hook->with_args;
     35        $hook->id          = $hook_id;
     36        $hook->listener    = $hook_options['listener'];
     37        $hook->method      = $hook_options['filter'] ?? $hook_options['action'];
     38        $hook->type        = isset($hook_options['filter']) ? 'filter' : 'action';
     39        $hook->priority    = $hook_options['priority'] ?? 10;
     40        $hook->options     = $hook_options['options'] ?? [];
     41        $hook->with_args   = $hook_options['with_args'] ?? true;
     42        $hook->expand_args = $hook_options['expand_args'] ?? $hook->with_args;
    4143
    42         return $hook;
    43     }
     44        return $hook;
     45    }
    4446
    45     /**
    46      * Generate a new Hook callback on the fly.
    47      *
    48      * @param mixed  $listener Object or name of class.
    49      * @param string $method
    50      * @param array  $options
    51      * @param bool   $with_args
    52      *
    53      * @return callable
    54      */
    55     final public static function callback( $listener, $method, $options = [], $with_args = true ): callable {
    56         $hook = new Hook();
     47    /**
     48     * Generate a new Hook callback on the fly.
     49     *
     50     * @param mixed  $listener Object or name of class.
     51     * @param string $method
     52     * @param array  $options
     53     * @param bool   $with_args
     54     *
     55     * @return callable
     56     */
     57    final public static function callback($listener, $method, $options = [], $with_args = true): callable
     58    {
     59        $hook = new Hook();
    5760
    58         $hook->listener    = $listener;
    59         $hook->method      = $method;
    60         $hook->options     = $options;
    61         $hook->with_args   = $with_args;
    62         $hook->expand_args = true;
     61        $hook->listener    = $listener;
     62        $hook->method      = $method;
     63        $hook->options     = $options;
     64        $hook->with_args   = $with_args;
     65        $hook->expand_args = true;
    6366
    64         return [ $hook, 'invoke' ];
    65     }
     67        return [ $hook, 'invoke' ];
     68    }
    6669
    67     /**
    68      * Register hook in WP.
    69      *
    70      * @return null
    71      */
    72     final public function enqueue(): void {
    73         $to_register = 'add_' . $this->type;
     70    /**
     71     * Register hook in WP.
     72     *
     73     * @return null
     74     */
     75    final public function enqueue(): void
     76    {
     77        $to_register = 'add_' . $this->type;
    7478
    75         $to_register( $this->id, [ $this, 'invoke' ], $this->priority, 99 );
    76     }
     79        $to_register($this->id, [ $this, 'invoke' ], $this->priority, 99);
     80    }
    7781
    78     /**
    79      * Invoke hook.
    80      *
    81      * @param mixed ...$args
    82      *
    83      * @return mixed
    84      */
    85     final public function invoke( ...$args ) {
    86         $class  = $this->listener;
    87         $method = $this->method;
     82    /**
     83     * Invoke hook.
     84     *
     85     * @param mixed ...$args
     86     *
     87     * @return mixed
     88     */
     89    final public function invoke(...$args)
     90    {
     91        $class  = $this->listener;
     92        $method = $this->method;
    8893
    89         if ( $this->with_args ) {
    90             return $this->invoke_hook( $class, $method, $args, $this->options );
    91         }
     94        if ($this->with_args) {
     95            return $this->invoke_hook($class, $method, $args, $this->options);
     96        }
    9297
    93         return $this->invoke_hook( $class, $method, $this->options );
    94     }
     98        return $this->invoke_hook($class, $method, $this->options);
     99    }
    95100
    96     /**
    97      * Helper: Invoke hook in low level.
    98      *
    99      * @param mixed  $class Object or name of class.
    100      * @param string $method
    101      * @param array  $method_args
    102      * @param ?array   $class_args Arguments for class.
    103      *
    104      * @return mixed
    105      */
    106     final private function invoke_hook( $class, string $method, array $method_args = [], ?array $class_args = null ) {
    107         $object = \is_object( $class ) ? $class : new $class( $class_args );
     101    /**
     102     * Helper: Invoke hook in low level.
     103     *
     104     * @param mixed  $class Object or name of class.
     105     * @param string $method
     106     * @param array  $method_args
     107     * @param ?array   $class_args Arguments for class.
     108     *
     109     * @return mixed
     110     */
     111    final private function invoke_hook($class, string $method, array $method_args = [], ?array $class_args = null)
     112    {
     113        $object = \is_object($class) ? $class : new $class($class_args);
    108114
    109         $common_task_method = self::COMMON_UP_METHOD;
    110         if ( \method_exists( $object, $common_task_method ) ) {
    111             $object->$common_task_method( $method, $method_args );
    112         }
     115        $common_task_method = self::COMMON_UP_METHOD;
     116        if (\method_exists($object, $common_task_method)) {
     117            $object->$common_task_method($method, $method_args);
     118        }
    113119
    114         if ( $this->expand_args ) {
    115             $result = $object->$method( ...$method_args );
    116         } else {
    117             $result = $object->$method( $method_args );
    118         }
     120        if ($this->expand_args) {
     121            $result = $object->$method(...$method_args);
     122        } else {
     123            $result = $object->$method($method_args);
     124        }
    119125
    120         return $result;
    121     }
     126        return $result;
     127    }
    122128}
  • wpo-checker/trunk/src/Framework/class-parser.php

    r2217624 r2241250  
    1010 * @package Framework
    1111 */
    12 final class Parser {
    13     private const FIND_VAR = '/{{(?P<var>.*?)}}/';
    14     private const FIND_BLOCKS = '/' .                                   //start regex
    15                                 '\{\%\s?(?<block_name>[\w\!\_\.]+)' .   //start block:  {% block_name
    16                                 '(\s(?<var>[^{}]*))?' .                 // var ?
    17                                 '\ \%\}' .                              // %}
    18                                 '(?<content>(([^%]+)|(?R))*)' .         //content with not %
    19                                 '\{\%\s?end\1\s?\%\}' .                 //end macro:  {% end{macro_name} %}
    20                                 '/mU';                                  //end regex
    21     private const FIND_GETTEXT_STRINGS = '/\{\'(?P<string>.*?)\'\}/';
    22     private const DEFAULT_VALUE = '';
    23 
    24     private $vars;
    25 
    26     /**
    27      * Parse a text using as vars $vars
    28      *
    29      * @param string $content
    30      * @param array $vars
    31      *
    32      * @return string
    33      */
    34     final public function parse( string $content = '', array $vars = [] ): string {
    35         $this->vars = $vars;
    36 
    37         $patterns_and_callbacks = [
    38             self::FIND_BLOCKS          => [ $this, 'do_block' ],
    39             self::FIND_VAR             => [ $this, 'return_var_value_from_tokens' ],
    40             self::FIND_GETTEXT_STRINGS => [ $this, 'replace_gettext_strings' ],
    41         ];
    42 
    43         return \preg_replace_callback_array( $patterns_and_callbacks, $content );
    44     }
    45 
    46 
    47     /**
    48      * Check if a content has a var inside or not.
    49      *
    50      * @param string $maybe_inside_var
    51      *
    52      * @return bool
    53      */
    54     final public function has_var( string $maybe_inside_var ): bool {
    55         $matches = [];
    56 
    57         \preg_match( '/' . static::FIND_VAR . '/us', $maybe_inside_var, $matches );
    58 
    59         return ! empty( $matches['var'] );
    60     }
    61 
    62 
    63     /**
    64      * Retrieve value of a var
    65      *
    66      * @param string $var_name
    67      *
    68      * @return mixed|string
    69      */
    70     final private function get_value_of_var_name( string $var_name ) {
    71         $var_name = \trim( $var_name );
    72 
    73         $vars_name = \explode( '.', $var_name );
    74         $value     = static::DEFAULT_VALUE;
    75 
    76         $vars =& $this->vars;
    77         foreach ( $vars_name as $var_name ) {
    78             if ( is_array( $vars ) ) {
    79                 $value = $vars[ $var_name ] ?? static::DEFAULT_VALUE;
    80             } elseif ( \is_object( $vars ) ) {
    81                 $value = $vars->$var_name ?? static::DEFAULT_VALUE;
    82             } else {
    83                 return static::DEFAULT_VALUE;
    84             }
    85 
    86             $vars =& $value;
    87         }
    88 
    89         return $value;
    90     }
    91 
    92 
    93     /**
    94      * Parse a block statement
    95      *
    96      * @param array{block_name: string, var: string, content: string}  $tokens
    97      *
    98      * @return string
    99      */
    100     final private function do_block( $tokens ): string {
    101         $block_name = $tokens['block_name'];
    102         $var        = $tokens['var'] ?? '';
    103         $content    = $tokens['content'] ?? '';
    104 
    105         if ( 'foreach' === $block_name ) {
    106             return $this->do_foreach( $var, $content );
    107         }
    108 
    109         if ( 'if' === $block_name ) {
    110             return $this->do_if( $var, $content );
    111         }
    112 
    113         return (string) \apply_filters( '\trasweb\blocks\\' . $block_name, $content, $var, $this );
    114     }
    115 
    116     /**
    117      * Parse a foreach statement
    118      *
    119      * @param string $var
    120      * @param string $content_of_foreach
    121      *
    122      * @return string
    123      */
    124     final private function do_foreach( string $var, string $content_of_foreach ): string {
    125         $vars      = explode( ' as ', $var ?? '' );
    126         $var_name  = trim( $vars[0] );
    127         $var_alias = trim( $vars[1] ?? 'item' );
    128 
    129         $items_to_iterate = $this->get_value_of_var_name( $var_name );
    130 
    131         if ( empty( $items_to_iterate ) || ! \is_iterable( $items_to_iterate ) || '' === $content_of_foreach ) {
    132             return $this::DEFAULT_VALUE;
    133         }
    134 
    135         return $this->parse_content_for_all_items( $items_to_iterate, $content_of_foreach, $var_alias );
    136     }
    137 
    138     /**
    139      * Parse foreach content iteratively.
    140      *
    141      * @param iterable $items_to_iterate Items to iterate.
    142      * @param string   $content_of_foreach
    143      * @param string   $var_alias        Alias of each item
    144      *
    145      * @return string
    146      */
    147     final private function parse_content_for_all_items( iterable $items_to_iterate, string $content_of_foreach, string $var_alias ) {
    148         $index = 1;
    149         $max   = count( $items_to_iterate );
    150 
    151         $foreach_result = '';
    152         foreach ( $items_to_iterate as $item ) {
    153             $vars = $this->vars;
    154 
    155             $vars[ $var_alias ] = $item;
    156             $vars['index']      = $index;
    157             $vars['count']      = $max;
    158             $vars['is_first']   = 1 === $index;
    159             $vars['is_last']    = $max === $index;
    160 
    161             $foreach_result .= (new $this)->parse( $content_of_foreach, $vars );
    162             $index ++;
    163         }
    164 
    165         return $foreach_result;
    166     }
    167 
    168     /**
    169      * Parse an if statement
    170      *
    171      * @param string $var_of_conditional
    172      * @param string $content_of_conditional
    173      *
    174      * @return string
    175      */
    176     final private function do_if( string $var_of_conditional, string $content_of_conditional ): string {
    177         $true_with_empty    = '!' === $var_of_conditional[0];
    178         $var_of_conditional = ltrim( $var_of_conditional, '! ' );
    179 
    180         if ( '' === $var_of_conditional || '' === $content_of_conditional ) {
    181             return $this::DEFAULT_VALUE;
    182         }
    183 
    184         $condictional_is_false = empty( $this->get_value_of_var_name( $var_of_conditional ) );
    185 
    186         if ( $condictional_is_false !== $true_with_empty ) {
    187             return $this::DEFAULT_VALUE;
    188         }
    189 
    190         return (new $this)->parse( $content_of_conditional, $this->vars );
    191     }
    192 
    193     /**
    194      * Replace a {{var}} for its value
    195      *
    196      * @param array $tokens Tokens from parsing.
    197      *
    198      * @return string
    199      */
    200     final protected function return_var_value_from_tokens( array $tokens ): string {
    201         return $this->get_value_of_var_name( $tokens['var'] );
    202     }
    203 
    204     /**
    205      * Replace a {'string'] for its gettext version.
    206      *
    207      * @param array{string: string} $tokens Tokens from parsing.
    208      *
    209      * @return string
    210      */
    211     final private function replace_gettext_strings( array $tokens ): string {
    212         $string = $tokens['string'] ?? '';
    213 
    214         return __( $string, PLUGIN_NAME );
    215     }
     12final class Parser
     13{
     14    private const FIND_VAR = '/{{(?P<var>.*?)}}/';
     15    private const FIND_BLOCKS = '/' .                                   //start regex
     16                                '\{\%\s?(?<block_name>[\w\!\_\.]+)' .   //start block:  {% block_name
     17                                '(\s(?<var>[^{}]*))?' .                 // var ?
     18                                '\ \%\}' .                              // %}
     19                                '(?<content>(([^%]+)|(?R))*)' .         //content with not %
     20                                '\{\%\s?end\1\s?\%\}' .                 //end macro:  {% end{macro_name} %}
     21                                '/mU';                                  //end regex
     22    private const FIND_GETTEXT_STRINGS = '/\{\'(?P<string>.*?)\'\}/';
     23    private const DEFAULT_VALUE = '';
     24
     25    private $vars;
     26
     27    /**
     28     * Parse a text using as vars $vars
     29     *
     30     * @param string $content
     31     * @param array $vars
     32     *
     33     * @return string
     34     */
     35    final public function parse(string $content = '', array $vars = []): string
     36    {
     37        $this->vars = $vars;
     38
     39        $patterns_and_callbacks = [
     40            self::FIND_BLOCKS          => [ $this, 'do_block' ],
     41            self::FIND_VAR             => [ $this, 'return_var_value_from_tokens' ],
     42            self::FIND_GETTEXT_STRINGS => [ $this, 'replace_gettext_strings' ],
     43        ];
     44
     45        return \preg_replace_callback_array($patterns_and_callbacks, $content);
     46    }
     47
     48
     49    /**
     50     * Check if a content has a var inside or not.
     51     *
     52     * @param string $maybe_inside_var
     53     *
     54     * @return bool
     55     */
     56    final public function has_var(string $maybe_inside_var): bool
     57    {
     58        $matches = [];
     59
     60        \preg_match('/' . static::FIND_VAR . '/us', $maybe_inside_var, $matches);
     61
     62        return ! empty($matches['var']);
     63    }
     64
     65
     66    /**
     67     * Retrieve value of a var
     68     *
     69     * @param string $var_name
     70     *
     71     * @return mixed|string
     72     */
     73    final private function get_value_of_var_name(string $var_name)
     74    {
     75        $var_name = \trim($var_name);
     76
     77        $vars_name = \explode('.', $var_name);
     78        $value     = static::DEFAULT_VALUE;
     79
     80        $vars =& $this->vars;
     81        foreach ($vars_name as $var_name) {
     82            if (is_array($vars)) {
     83                $value = $vars[ $var_name ] ?? static::DEFAULT_VALUE;
     84            } elseif (\is_object($vars)) {
     85                $value = $vars->$var_name ?? static::DEFAULT_VALUE;
     86            } else {
     87                return static::DEFAULT_VALUE;
     88            }
     89
     90            $vars =& $value;
     91        }
     92
     93        return $value;
     94    }
     95
     96
     97    /**
     98     * Parse a block statement
     99     *
     100     * @param array{block_name: string, var: string, content: string}  $tokens
     101     *
     102     * @return string
     103     */
     104    final private function do_block($tokens): string
     105    {
     106        $block_name = $tokens['block_name'];
     107        $var        = $tokens['var'] ?? '';
     108        $content    = $tokens['content'] ?? '';
     109
     110        if ('foreach' === $block_name) {
     111            return $this->do_foreach($var, $content);
     112        }
     113
     114        if ('if' === $block_name) {
     115            return $this->do_if($var, $content);
     116        }
     117
     118        return (string) \apply_filters('\trasweb\blocks\\' . $block_name, $content, $var, $this);
     119    }
     120
     121    /**
     122     * Parse a foreach statement
     123     *
     124     * @param string $var
     125     * @param string $content_of_foreach
     126     *
     127     * @return string
     128     */
     129    final private function do_foreach(string $var, string $content_of_foreach): string
     130    {
     131        $vars      = explode(' as ', $var ?? '');
     132        $var_name  = trim($vars[0]);
     133        $var_alias = trim($vars[1] ?? 'item');
     134
     135        $items_to_iterate = $this->get_value_of_var_name($var_name);
     136
     137        if (empty($items_to_iterate) || ! \is_iterable($items_to_iterate) || '' === $content_of_foreach) {
     138            return $this::DEFAULT_VALUE;
     139        }
     140
     141        return $this->parse_content_for_all_items($items_to_iterate, $content_of_foreach, $var_alias);
     142    }
     143
     144    /**
     145     * Parse foreach content iteratively.
     146     *
     147     * @param iterable $items_to_iterate Items to iterate.
     148     * @param string   $content_of_foreach
     149     * @param string   $var_alias        Alias of each item
     150     *
     151     * @return string
     152     */
     153    final private function parse_content_for_all_items(iterable $items_to_iterate, string $content_of_foreach, string $var_alias): string
     154    {
     155        $index = 1;
     156        $max   = count($items_to_iterate);
     157
     158        $foreach_result = '';
     159        foreach ($items_to_iterate as $item) {
     160            $vars = $this->vars;
     161
     162            $vars[ $var_alias ] = $item;
     163            $vars['index']      = $index;
     164            $vars['count']      = $max;
     165            $vars['is_first']   = 1 === $index;
     166            $vars['is_last']    = $max === $index;
     167
     168            $foreach_result .= (new $this)->parse($content_of_foreach, $vars);
     169            $index ++;
     170        }
     171
     172        return $foreach_result;
     173    }
     174
     175    /**
     176     * Parse an if statement
     177     *
     178     * @param string $var_of_conditional
     179     * @param string $content_of_conditional
     180     *
     181     * @return string
     182     */
     183    final private function do_if(string $var_of_conditional, string $content_of_conditional): string
     184    {
     185        $true_with_empty    = '!' === $var_of_conditional[0];
     186        $var_of_conditional = ltrim($var_of_conditional, '! ');
     187
     188        if ('' === $var_of_conditional || '' === $content_of_conditional) {
     189            return $this::DEFAULT_VALUE;
     190        }
     191
     192        $condictional_is_false = empty($this->get_value_of_var_name($var_of_conditional));
     193
     194        if ($condictional_is_false !== $true_with_empty) {
     195            return $this::DEFAULT_VALUE;
     196        }
     197
     198        return (new $this)->parse($content_of_conditional, $this->vars);
     199    }
     200
     201    /**
     202     * Replace a {{var}} for its value
     203     *
     204     * @param array $tokens Tokens from parsing.
     205     *
     206     * @return string
     207     */
     208    final protected function return_var_value_from_tokens(array $tokens): string
     209    {
     210        return $this->get_value_of_var_name($tokens['var']);
     211    }
     212
     213    /**
     214     * Replace a {'string'] for its gettext version.
     215     *
     216     * @param array{string: string} $tokens Tokens from parsing.
     217     *
     218     * @return string
     219     */
     220    final private function replace_gettext_strings(array $tokens): string
     221    {
     222        $string = $tokens['string'] ?? '';
     223
     224        return __($string, PLUGIN_NAME);
     225    }
    216226}
  • wpo-checker/trunk/src/Framework/class-service.php

    r2217761 r2241250  
    1515class Service
    1616{
    17     private const options_METHOD = 'get_instance';
     17    private const OPTIONS_METHOD = 'get_instance';
    1818
    1919    private static $services = [];
     
    116116            $service = new $class_name(...$args);
    117117        } else {
    118             $options_method = self::options_METHOD;
     118            $options_method = self::OPTIONS_METHOD;
    119119            if (method_exists($class_name, $options_method)) {
    120120                $service = $class_name::$options_method($this->options, $args);
  • wpo-checker/trunk/src/Framework/class-view.php

    r2217624 r2241250  
    1111 * @package Framework
    1212 */
    13 class View {
    14     private const _VIEWS_ = _PLUGIN_ . '/views';
     13class View
     14{
     15    private const _VIEWS_ = _PLUGIN_ . '/views';
    1516
    16     private $view_content;
     17    private $view_content;
    1718
    18     /**
    19      * Parse a view using some variables.
    20      *
    21      * @param string $view_name View to parse.
    22      * @param array  $vars      Variables to use in parsing.
    23      *
    24      * @return string
    25      */
    26     final public static function get( string $view_name, array $vars = [] ): string {
    27         $view_engine_class = Service::get( 'View', __NAMESPACE__, $view_name, $vars  );
     19    /**
     20     * Parse a view using some variables.
     21     *
     22     * @param string $view_name View to parse.
     23     * @param array  $vars      Variables to use in parsing.
     24     *
     25     * @return string
     26     */
     27    final public static function get(string $view_name, array $vars = []): string
     28    {
     29        $view_engine_class = Service::get('View', __NAMESPACE__, $view_name, $vars);
    2830
    29         return $view_engine_class->parse();
    30     }
     31        return $view_engine_class->parse();
     32    }
    3133
    32     /**
    33      * View constructor.
    34      *
    35      * @param string $view_name View to parse.
    36      * @param array  $vars      Variables to use in parsing.
    37      */
    38     final public function __construct( string $view_name, array $vars = [] ) {
    39         $this->view_content = $this->get_view_content( $view_name );
    40         $this->vars         = $vars;
    41     }
     34    /**
     35     * View constructor.
     36     *
     37     * @param string $view_name View to parse.
     38     * @param array  $vars      Variables to use in parsing.
     39     */
     40    final public function __construct(string $view_name, array $vars = [])
     41    {
     42        $this->view_content = $this->get_view_content($view_name);
     43        $this->vars         = $vars;
     44    }
    4245
    43     /**
    44      * Retrieve content of a view.
    45      *
    46      * @param string $view_name View where content will be extract.
    47      *
    48      * @return string
    49      */
    50     final private function get_view_content( string $view_name ): string {
    51         static $views_content_cache = [];
     46    /**
     47     * Retrieve content of a view.
     48     *
     49     * @param string $view_name View where content will be extract.
     50     *
     51     * @return string
     52     */
     53    final private function get_view_content(string $view_name): string
     54    {
     55        static $views_content_cache = [];
    5256
    53         if ( ! empty( $views_content_cache[ $view_name ] ) ) {
    54             return $views_content_cache[ $view_name ];
    55         }
     57        if (! empty($views_content_cache[ $view_name ])) {
     58            return $views_content_cache[ $view_name ];
     59        }
    5660
    57         $view_content = \file_get_contents( self::_VIEWS_ . '/' . $view_name . '.tpl' );
     61        $view_content = \file_get_contents(self::_VIEWS_ . '/' . $view_name . '.tpl');
    5862
    59         $views_content_cache[ $view_name ] = \apply_filters( PLUGIN_NAME . '-view-' . $view_name, $view_content );
     63        $views_content_cache[ $view_name ] = \apply_filters(PLUGIN_NAME . '-view-' . $view_name, $view_content);
    6064
    61         return $views_content_cache[ $view_name ];
    62     }
     65        return $views_content_cache[ $view_name ];
     66    }
    6367
    64     /**
    65      * Parse current view using a Parser service.
    66      *
    67      * @return mixed
    68      */
    69     final public function parse() {
    70         $parser_engine_class = Service::get( 'Parser', __NAMESPACE__ );
     68    /**
     69     * Parse current view using a Parser service.
     70     *
     71     * @return mixed
     72     */
     73    final public function parse()
     74    {
     75        $parser_engine_class = Service::get('Parser', __NAMESPACE__);
    7176
    72         return $parser_engine_class->parse($this->view_content, $this->vars);
    73     }
     77        return $parser_engine_class->parse($this->view_content, $this->vars);
     78    }
    7479}
  • wpo-checker/trunk/src/Places/class-plugins-list.php

    r2217761 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    3 namespace Trasweb\Plugins\WpoChecker\Pages;
     4namespace Trasweb\Plugins\WpoChecker\Places;
    45
     6use Trasweb\Plugins\WpoChecker\Plugin;
     7use Trasweb\Plugins\WpoChecker\Repositories\Sites;
     8use function admin_url;
    59use const Trasweb\Plugins\WpoChecker\PLUGIN_NAME;
    610
     
    1115 */
    1216class Plugins_List {
     17    private const PLUGIN_INIT_FILE = PLUGIN_NAME . '/' . PLUGIN_NAME . '.php';
    1318    /**
    1419     * Links to settings data
     
    2328     * @param array<string, array> $config
    2429     */
    25     public function __construct( array $config ) {
    26         $this->link_to_settings = $config['link_to_settings'];
     30    public function __construct( array $config = [] )
     31    {
     32        $this->link_to_settings = $config[ 'link_to_settings' ] ?? [];
     33    }
     34
     35    /***
     36     * Redirect to config page when plugin is activated.
     37     *
     38     * @param string $url Default url of filter.
     39     *
     40     * @filter wp_redirect
     41     *
     42     * @return string Default url or url to plugin settings page
     43     */
     44    public function redirect_to_sites_selection( string $url ): string
     45    {
     46        $redirect_normally = $url;
     47
     48        if ( ! defined( Plugin::NAMESPACE . '\PLUGIN_ACTIVATION' ) ) {
     49            return $redirect_normally;
     50        }
     51
     52        $is_final_redirection = strpos( $url, admin_url( 'plugins.php?activate=true' ) ) !== false;
     53        if ( ! $is_final_redirection ) {
     54            return $redirect_normally;
     55        }
     56
     57        $is_activation_bulk = $_REQUEST[ 'activate-multi' ] ?? false;
     58        if ( $is_activation_bulk ) {
     59            return $redirect_normally;
     60        }
     61
     62        $already_plugin_configured = count( Sites::get_sites()->get_saved_site_collection() );
     63        if($already_plugin_configured) {
     64            return $redirect_normally;
     65        }
     66
     67        $sites_selection_url = add_query_arg( [ 'page' => Plugin::CONFIG_PAGE ], admin_url( 'admin.php' ) );
     68
     69        return $redirect_to = $sites_selection_url;
    2770    }
    2871
     
    3578     * @return array
    3679     */
    37     public function show_settings_in_plugin_links( array $links, string $file ): array {
    38         $init_file = PLUGIN_NAME.'/'.PLUGIN_NAME.".php";
    39 
    40         if ( $file === $init_file && current_user_can( $this->link_to_settings['capability'] ) ) {
     80    public function show_settings_in_plugin_links( array $links, string $file ): array
     81    {
     82        if ( $file === self::PLUGIN_INIT_FILE && current_user_can( $this->link_to_settings[ 'capability' ] ) ) {
    4183            array_unshift( $links, $this->get_settings_link() );
    4284        }
     
    5092     * @return string
    5193     */
    52     private function get_settings_link(): string {
    53         $settings_url = admin_url( 'options-general.php?page=' . $this->link_to_settings['slug'] );
     94    private function get_settings_link(): string
     95    {
     96        $settings_url = admin_url( 'options-general.php?page=' . $this->link_to_settings[ 'slug' ] );
    5497
    55         return sprintf( $this->link_to_settings['format'], $settings_url, $this->link_to_settings['label'] );
     98        return sprintf( $this->link_to_settings[ 'format' ], $settings_url, $this->link_to_settings[ 'label' ] );
    5699    }
    57100}
  • wpo-checker/trunk/src/Places/class-posts-list.php

    r2217624 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    3 namespace Trasweb\Plugins\WpoChecker\Pages;
     4namespace Trasweb\Plugins\WpoChecker\Places;
    45
    56use Trasweb\Plugins\WpoChecker\Entities\Site;
     
    78use Trasweb\Plugins\WpoChecker\Repositories\Sites;
    89use WP_Post;
     10use function get_object_vars;
     11use function get_permalink;
    912
    1013/**
     
    1417 */
    1518class Posts_List {
     19    private const STATUS_WITH_WPO_LINKS = 'publish';
     20
    1621    /**
     22     * Add wpo quick links to action list of post.
     23     *
    1724     * @param array   $actions
    1825     * @param WP_Post $post
    1926     *
     27     * @filter post_row_actions
     28     * @filter page_row_actions
     29     *
    2030     * @return array
    2131     */
    22     public function show_wpo_links_in_cpt_row_actions( array $actions, WP_Post $post ) {
     32    public function show_wpo_links_in_cpt_row_actions( array $actions, WP_Post $post ): array
     33    {
    2334        $wpo_actions = Sites::get_sites()->get_saved_site_collection();
    2435
    25         $url =  \get_permalink( $post->ID );
    26         if ( ! $url || "publish" !== $post->post_status ) {
     36        $url = get_permalink( $post->ID );
     37        if ( ! $url || self::STATUS_WITH_WPO_LINKS !== $post->post_status ) {
    2738            return $actions;
    2839        }
     
    3647
    3748    /**
     49     * Helper: Generate site quick links for post.
     50     *
    3851     * @param WP_Post $post
    3952     * @param Site    $site
     
    4255     * @return string
    4356     */
    44     private function generate_action_for_post( WP_Post $post, Site $site, string $permalink ): string {
    45         $vars = \get_object_vars( $site );
     57    private function generate_action_for_post( WP_Post $post, Site $site, string $permalink ): string
     58    {
     59        $vars = get_object_vars( $site );
    4660
    47         $vars['item_url'] = $permalink;
    48         $vars['form_id']  = $site->id . '_' . $post->ID;
     61        $vars[ 'item_url' ] = $permalink;
     62        $vars[ 'form_id' ]  = $site->id . '_' . $post->ID;
    4963
    5064        return View::get( 'action_template', $vars );
  • wpo-checker/trunk/src/Places/class-sites-selection.php

    r2217624 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    3 namespace Trasweb\Plugins\WpoChecker\Pages;
     4namespace Trasweb\Plugins\WpoChecker\Places;
    45
    56use Trasweb\Plugins\WpoChecker\Framework\Hook;
    67use Trasweb\Plugins\WpoChecker\Framework\View;
    78use Trasweb\Plugins\WpoChecker\Repositories\Sites;
     9
     10use function ob_get_contents;
     11
    812use const Trasweb\Plugins\WpoChecker\PLUGIN_NAME;
    913
    1014/**
    11  * Class Sites_Selection
     15 * Class Sites_Selection. It is used in order to manage page selection.
    1216 *
    1317 * @package Pages
     
    1923    private $menu_slug;
    2024
     25    private const VIEW_NAME = 'sites_selection';
     26
    2127    /**
    2228     * Sites_Selection constructor.
     
    2430     * @param array $config
    2531     */
    26     public function __construct( array $config ) {
    27         $this->page_title = $config['page_title'];
    28         $this->menu_title = $config['menu_title'];
    29         $this->capability = $config['capability'];
    30         $this->menu_slug  = $config['menu_slug'];
     32    public function __construct( array $config )
     33    {
     34        $this->page_title = $config[ 'page_title' ];
     35        $this->menu_title = $config[ 'menu_title' ];
     36        $this->capability = $config[ 'capability' ];
     37        $this->menu_slug  = $config[ 'menu_slug' ];
    3138    }
    3239
    3340    /**
     41     * Show link to page in menu.
    3442     *
     43     * @action admin_menu
     44     *
     45     * @return void
    3546     */
    36     public function show_menu(): void {
     47    public function show_menu(): void
     48    {
    3749        $options_page = [
    3850            'page_title' => $this->page_title,
     
    4759
    4860    /**
     61     * Show page of sites selection.
    4962     *
     63     * @return void
    5064     */
    51     public function show_page(): void {
     65    public function show_page(): void
     66    {
    5267        $vars = [
    53             'title'           => $this->page_title,
    54             'PLUGIN'          => PLUGIN_NAME,
    55             'sites'           => Sites::get_sites()->get_available_site_collection(),
    56             'wordpress_tokens' => $this->get_wordpress_tokens(),
     68            'title'             => $this->page_title,
     69            'PLUGIN'            => PLUGIN_NAME,
     70            'sites'             => Sites::get_sites()->get_available_site_collection(),
     71            'all_sites_checked' => Sites::get_sites()->are_all_sites_selected(),
     72            'wordpress_tokens'  => $this->get_wordpress_tokens(),
    5773        ];
    5874
    59         echo View::get( 'sites_selection', $vars );
     75        echo View::get( self::VIEW_NAME, $vars );
    6076    }
    6177
    6278    /**
     79     * Retrieve nonces( and metadada ) in order to use in sites selection page.
     80     *
    6381     * @return string
    6482     */
    65     private function get_wordpress_tokens(): string {
     83    private function get_wordpress_tokens(): string
     84    {
    6685        ob_start();
    6786        settings_fields( PLUGIN_NAME );
  • wpo-checker/trunk/src/Places/class-terms-list.php

    r2217624 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    3 namespace Trasweb\Plugins\WpoChecker\Pages;
     4namespace Trasweb\Plugins\WpoChecker\Places;
    45
    56use Trasweb\Plugins\WpoChecker\Entities\Site;
     
    78use Trasweb\Plugins\WpoChecker\Repositories\Sites;
    89use WP_Term;
     10use function get_object_vars;
     11use function get_term_link;
    912
    1013/**
     
    1518class Terms_List {
    1619    /**
     20     * Add wpo quick links to action list of term.
     21     *
    1722     * @param array   $actions
    1823     * @param WP_Term $term
    1924     *
     25     * @filter tag_row_actions
     26     *
    2027     * @return array
    2128     */
    22     public function show_wpo_links_in_tags_row_actions( array $actions, WP_Term $term ) {
     29    public function show_wpo_links_in_tags_row_actions( array $actions, WP_Term $term ): array
     30    {
    2331        $wpo_actions = Sites::get_sites()->get_saved_site_collection();
    2432
    25         $url =  \get_term_link( $term->term_id );
     33        $url = get_term_link( $term->term_id );
    2634        if ( ! $url ) {
    2735            return $actions;
     
    3644
    3745    /**
     46     * Helper: Generate site quick links for term.
     47     *
    3848     * @param WP_Term $term
    3949     * @param Site    $site
     
    4252     * @return string
    4353     */
    44     private function generate_action_for_term( WP_Term $term, Site $site, string $term_link ): string {
    45         $vars = \get_object_vars( $site );
     54    private function generate_action_for_term( WP_Term $term, Site $site, string $term_link ): string
     55    {
     56        $vars = get_object_vars( $site );
    4657
    47         $vars['item_url'] = $term_link;
    48         $vars['form_id']  = $site->id . '_' . $term->term_id;
     58        $vars[ 'item_url' ] = $term_link;
     59        $vars[ 'form_id' ]  = $site->id . '_' . $term->term_id;
    4960
    5061        return View::get( 'action_template', $vars );
  • wpo-checker/trunk/src/Repositories/class-settings.php

    r2217624 r2241250  
    1010 * @package Repositories
    1111 */
    12 class Settings {
    13     private const OPTION = 'sites';
     12class Settings
     13{
     14    private const OPTION = 'sites';
    1415
    15     /**
    16      * Register plugin settings.
    17      *
    18      * @param $settings
    19      *
    20      * @return void
    21      */
    22     public function register( array $settings ): void {
    23         foreach ( $settings as $name => $config ) {
    24             register_setting( PLUGIN_NAME, $this->get_option_name( $name ), $config );
    25         }
    26     }
     16    /**
     17     * Register plugin settings.
     18     *
     19     * @param $settings
     20     *
     21     * @return void
     22     */
     23    public function register(array $settings): void
     24    {
     25        foreach ($settings as $name => $config) {
     26            register_setting(PLUGIN_NAME, $this->get_option_name($name), $config);
     27        }
     28    }
    2729
    28     /**
    29      * Remove plugin settings from WP.
    30      */
    31     public function unregister(): void {
    32         delete_option( $this->get_option_name( self::OPTION ) );
    33     }
     30    /**
     31     * Remove plugin settings from WP.
     32     */
     33    public function unregister(): void
     34    {
     35        delete_option($this->get_option_name(self::OPTION));
     36    }
    3437
    35     /**
    36      * Retrieve sites from settings.
    37      *
    38      * @return array<string, array>
    39      */
    40     public function get_sites(): array {
    41         return $this->get( self::OPTION, [] ) ?: [];
    42     }
     38    /**
     39     * Retrieve sites from settings.
     40     *
     41     * @return array<string, array>
     42     */
     43    public function get_sites(): array
     44    {
     45        return $this->get(self::OPTION, []) ?: [];
     46    }
    4347
    44     /**
    45      * Helper: Retrieve value of a setting by its name.
    46      *
    47      * @param string $setting_name Setting name whose value is returned.
    48      * @param string $default      Default value if setting is not found.
    49      *
    50      * @return mixed|void
    51      */
    52     private function get( string $setting_name, $default = '' ) {
    53         return get_option( $this->get_option_name( $setting_name ), $default );
    54     }
     48    /**
     49     * Helper: Retrieve value of a setting by its name.
     50     *
     51     * @param string $setting_name Setting name whose value is returned.
     52     * @param string $default      Default value if setting is not found.
     53     *
     54     * @return mixed|void
     55     */
     56    private function get(string $setting_name, $default = '')
     57    {
     58        return get_option($this->get_option_name($setting_name), $default);
     59    }
    5560
    56     /**
    57      * Helper: Make up option name from a prefix( plugin name ) and setting name.
    58      *
    59      * @param string $setting_name
    60      *
    61      * @return string
    62      */
    63     private function get_option_name( string $setting_name ): string {
    64         return PLUGIN_NAME . '-' . $setting_name;
    65     }
     61    /**
     62     * Helper: Make up option name from a prefix( plugin name ) and setting name.
     63     *
     64     * @param string $setting_name
     65     *
     66     * @return string
     67     */
     68    private function get_option_name(string $setting_name): string
     69    {
     70        return PLUGIN_NAME . '-' . $setting_name;
     71    }
    6672}
  • wpo-checker/trunk/src/Repositories/class-sites.php

    r2217624 r2241250  
    1717     * @return Sites
    1818     */
    19     public static function get_sites(): self {
    20         return Service::get('Sites', __NAMESPACE__);
     19    public static function get_sites(): self
     20    {
     21        return Service::get( 'Sites', __NAMESPACE__ );
    2122    }
    2223
     
    2829     * @return array
    2930     */
    30     public function sanitize( ?array $saved_sites = [] ): array {
     31    public function sanitize( ?array $saved_sites = [] ): array
     32    {
    3133        $saved_sites     = array_filter( (array) $saved_sites, 'is_string' );
    3234        $available_sites = array_keys( $this->get_availables() );
     
    4244     * @return array<string, array>
    4345     */
    44     private function get_availables(): array {
     46    private function get_availables(): array
     47    {
    4548        static $site_list;
    4649
     
    6164
    6265    /**
     66     * Retrieve if all sites are selected.
     67     *
     68     * @return bool
     69     */
     70    public function are_all_sites_selected(): bool
     71    {
     72        return count( $this->get_saved() ) === count( $this->get_availables() );
     73    }
     74
     75    /**
    6376     * Retrieve a site collection with available sites.
    6477     *
    6578     * @return SiteCollection
    6679     */
    67     public function get_available_site_collection(): SiteCollection {
     80    public function get_available_site_collection(): SiteCollection
     81    {
    6882        return new SiteCollection( $this->get_availables() );
    6983    }
     
    7488     * @return SiteCollection
    7589     */
    76     public function get_saved_site_collection(): SiteCollection {
     90    public function get_saved_site_collection(): SiteCollection
     91    {
    7792        return new SiteCollection( $this->get_saved() );
    78 
    7993    }
    8094
     
    8498     * @return array<string, array>
    8599     */
    86     private function get_saved(): array {
     100    private function get_saved(): array
     101    {
     102        static $saved_list;
     103
     104        if ( isset( $saved_list ) ) {
     105            return $saved_list;
     106        }
     107
    87108        $saved_list = array_flip( ( new Settings() )->get_sites() );
    88109
    89110        $available_sites = $this->get_availables();
    90111
    91         return \array_intersect_key( $available_sites, $saved_list );
     112        $saved_list = \array_intersect_key( $available_sites, $saved_list );
     113
     114        return $saved_list;
    92115    }
    93116}
  • wpo-checker/trunk/src/class-plugin.php

    r2217761 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    34namespace Trasweb\Plugins\WpoChecker;
    45
     6use Trasweb\Plugins\WpoChecker\Framework\Hook;
    57use Trasweb\Plugins\WpoChecker\Framework\Service;
    68use Trasweb\Plugins\WpoChecker\Repositories\Config;
    79use Trasweb\Plugins\WpoChecker\Repositories\Settings;
    810
     11use function admin_url;
     12use function apply_filters;
     13use function define;
     14use function defined;
     15use function file_get_contents;
     16use function get_self_link;
     17
    918use const PHP_VERSION;
    1019
     
    1221 * Class Plugin. Initialize and configure plugin
    1322 */
    14 class Plugin
    15 {
    16     public const _CLASSES_ = __DIR__;
    17     public const CURRENT_VERSION = '0.2';
    18 
    19     private const SUPPORTED_PHP_VERSION = '7.2.0';
    20     private const LANG_DIR = '/languages';
    21 
    22 
    23     /**
    24      * Initialization file of plugin. Maybe 'wpo-checker.php'.
    25      *
    26      * @var string
    27      */
    28     private $init_file;
    29 
    30     /**
    31      * Plugin Bootstrap
    32      *
    33      * @return void
    34      */
    35     public function __invoke()
    36     {
    37         if (! \is_admin()) {
    38             return;
    39         }
    40 
    41         \define(__NAMESPACE__ . '\_PLUGIN_', dirname(__DIR__));
    42         \define(__NAMESPACE__ . '\PLUGIN_NAME', basename(_PLUGIN_));
    43 
    44         if (version_compare(PHP_VERSION, self::SUPPORTED_PHP_VERSION) < 0) {
    45             return add_action('admin_notices', [ $this, 'you_need_recent_version_of_PHP' ]);
    46         }
    47 
    48         require_once(self::_CLASSES_ . '/Framework/class-service.php');
    49         require_once(self::_CLASSES_ . '/Repositories/class-config.php');
    50 
    51         $this->register_services();
    52         $this->register_autoload();
    53 
    54         if (\defined('WP_UNINSTALL_PLUGIN')) {
    55             $this->uninstall();
    56         } else {
    57             $this->initialize();
    58         }
    59     }
    60 
    61     /**
    62      * Config action
    63      *
    64      * @return void
    65      */
    66     final private function initialize(): void
    67     {
    68         $this->add_i18n();
    69         $this->enqueue_hooks();
    70 
    71         do_action(PLUGIN_NAME . '-loaded');
    72     }
    73 
    74     /**
    75      * Tasks of uninstallation  :(
    76      *
    77      * @return void
    78      */
    79     final private function uninstall(): void
    80     {
    81         ( new Settings() )->unregister();
    82     }
    83 
    84     /**
    85      * Show a warning when PHP version is not recent.
    86      *
    87      * @return void
    88      */
    89     final public function you_need_recent_version_of_PHP(): void
    90     {
    91         $msg   = sprintf('You need %s version of PHP for <strong>WPO Checker</strong> plugin', self::SUPPORTED_PHP_VERSION);
    92         $alert = __($msg, PLUGIN_NAME);
    93 
    94         echo str_replace('{{ alert }}', $alert, \file_get_contents(_PLUGIN_ . '/views/need_php_version.tpl'));
    95     }
    96 
    97     /**
    98      * Register plugin services.
    99      *
    100      * @return void
    101      */
    102     final private function register_services(): void
    103     {
    104         $service_list = Config::services();
    105 
    106         $service_list = \apply_filters(PLUGIN_NAME . '-services', $service_list);
    107 
    108         foreach ($service_list as $service) {
    109             $service->register();
    110         }
    111     }
    112 
    113     /**
    114      * Register plugin autoload.
    115      *
    116      * @return void
    117      */
    118     final private function register_autoload(): void
    119     {
    120         $autoload = Service::get('Autoload', __NAMESPACE__);
    121 
    122         spl_autoload_register([ $autoload, 'find_class' ], $throw_exception = false);
    123     }
    124 
    125     /**
    126      * Add support to i18n for plugin.
    127      *
    128      * @return void
    129      */
    130     final private function add_i18n(): void
    131     {
    132         load_plugin_textdomain(PLUGIN_NAME, false, PLUGIN_NAME . self::LANG_DIR);
    133     }
    134 
    135     /**
    136      * Enqueue hooks used in plugin.
    137      *
    138      * @return void
    139      */
    140     final private function enqueue_hooks(): void
    141     {
    142         $hooks_list = Config::hooks();
    143 
    144         foreach ($hooks_list as $hook) {
    145             $hook->enqueue();
    146         }
    147     }
     23final class Plugin {
     24    public const _CLASSES_ = __DIR__;
     25    public const NAMESPACE = __NAMESPACE__;
     26    public const CURRENT_VERSION = '0.3';
     27    public const CONFIG_PAGE = 'sites_selection_page';
     28
     29    private const SUPPORTED_PHP_VERSION = '7.2.0';
     30    private const UNSUPPORTED_PHP_VERSION_MSG = 'You need %s version of PHP for <strong>%s</strong> plugin';
     31    private const UNSUPPORTED_PHP_VERSION_VIEW = '/views/need_php_version.tpl';
     32    private const LANG_DIR = '/languages';
     33
     34    /**
     35     * Plugin Starts
     36     *
     37     * @return void
     38     */
     39    public function __invoke(): void
     40    {
     41        $this->bootstrap();
     42        $this->register_services();
     43        $this->register_autoload();
     44
     45        if ( defined( 'WP_UNINSTALL_PLUGIN' ) ) {
     46            $this->uninstall();
     47
     48            return;
     49        }
     50
     51        $this->initialize();
     52
     53        if ( $this->is_activation() ) {
     54            $this->activation();
     55        }
     56    }
     57
     58    /**
     59     * Define basic of plugin in order to can be loaded.
     60     *
     61     * @return void
     62     */
     63    final public function bootstrap(): void
     64    {
     65        define( __NAMESPACE__ . '\_PLUGIN_', dirname( __DIR__ ) );
     66        define( __NAMESPACE__ . '\PLUGIN_NAME', basename( _PLUGIN_ ) );
     67        define( __NAMESPACE__ . '\PLUGIN_TITLE', __( 'WPO Checker', PLUGIN_NAME ) );
     68
     69        if ( version_compare( PHP_VERSION, self::SUPPORTED_PHP_VERSION ) < 0 ) {
     70            add_action( 'admin_notices', [ $this, 'you_need_recent_version_of_PHP' ] );
     71
     72            return;
     73        }
     74
     75        require_once( self::_CLASSES_ . '/Framework/class-service.php' );
     76        require_once( self::_CLASSES_ . '/Repositories/class-config.php' );
     77    }
     78
     79    /**
     80     * Config action
     81     *
     82     * @return void
     83     */
     84    final private function initialize(): void
     85    {
     86        $this->add_i18n();
     87        $this->enqueue_hooks();
     88
     89        do_action( PLUGIN_NAME . '-loaded' );
     90    }
     91
     92    /**
     93     * Tasks of uninstallation  :(
     94     *
     95     * @return void
     96     */
     97    final private function uninstall(): void
     98    {
     99        ( new Settings() )->unregister();
     100    }
     101
     102    /**
     103     * Tasks in plugin activation  :)
     104     *
     105     * @return void
     106     */
     107    final private function activation(): void
     108    {
     109        define( __NAMESPACE__ . '\PLUGIN_ACTIVATION', true );
     110    }
     111
     112    /**
     113     * Helper: Retrieve if current request is a request of activation of this plugin.
     114     *
     115     * @return bool
     116     */
     117    final private function is_activation(): bool
     118    {
     119        $not_plugin_activation = false;
     120
     121        if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
     122            return $not_plugin_activation;
     123        }
     124
     125        $plugins_page    = admin_url( 'plugins.php' );
     126        $is_plugins_page = strpos( get_self_link(), $plugins_page ) !== false;
     127
     128        if ( ! $is_plugins_page ) {
     129            return $not_plugin_activation;
     130        }
     131
     132        $plugin_init_file = PLUGIN_NAME . '/' . PLUGIN_NAME . '.php';
     133        $plugin_name      = $_GET[ 'plugin' ] ?? '';
     134        $action           = $_GET[ 'action' ] ?? '';
     135
     136        $is_plugin_activation = $plugin_init_file === $plugin_name && 'activate' === $action;
     137
     138        return $is_plugin_activation;
     139    }
     140
     141    /**
     142     * Show a warning when PHP version is not recent.
     143     *
     144     * @return void
     145     */
     146    final public function you_need_recent_version_of_PHP(): void
     147    {
     148        $msg = sprintf( self::UNSUPPORTED_PHP_VERSION_MSG, self::SUPPORTED_PHP_VERSION, PLUGIN_TITLE );
     149
     150        $alert = __( $msg, PLUGIN_NAME );
     151
     152        $alert_content = file_get_contents( _PLUGIN_ . self::UNSUPPORTED_PHP_VERSION_VIEW );
     153
     154        echo str_replace( '{{ alert }}', $alert, $alert_content );
     155    }
     156
     157    /**
     158     * Register plugin services. Services are loaded in this way in order to other developers can customize them.
     159     *
     160     * @return void
     161     */
     162    final private function register_services(): void
     163    {
     164        $service_list = Config::services();
     165
     166        /**
     167         * @var Service[] $service_list
     168         */
     169        $service_list = apply_filters( PLUGIN_NAME . '-services', $service_list ) ?: [];
     170
     171        foreach ( $service_list as $service ) {
     172            $service->register();
     173        }
     174    }
     175
     176    /**
     177     * Register plugin autoload.
     178     *
     179     * @return void
     180     */
     181    final private function register_autoload(): void
     182    {
     183        $autoload = Service::get( 'Autoload', __NAMESPACE__ );
     184
     185        spl_autoload_register( [ $autoload, 'find_class' ], $throw_exception = false );
     186    }
     187
     188    /**
     189     * Add support to i18n for plugin.
     190     *
     191     * @return void
     192     */
     193    final private function add_i18n(): void
     194    {
     195        load_plugin_textdomain( PLUGIN_NAME, false, PLUGIN_NAME . self::LANG_DIR );
     196    }
     197
     198    /**
     199     * Enqueue hooks used in plugin.
     200     *
     201     * @return void
     202     */
     203    final private function enqueue_hooks(): void
     204    {
     205        /**
     206         * @var Hook[] $hooks_list
     207         */
     208        $hooks_list = Config::hooks();
     209
     210        foreach ( $hooks_list as $hook ) {
     211            $hook->enqueue();
     212        }
     213    }
    148214}
  • wpo-checker/trunk/uninstall.php

    r2217624 r2241250  
    11<?php
     2declare( strict_types = 1 );
    23
    3 use Trasweb\Plugins\WpoChecker\Plugin;
    4 
    5 if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    6     die( "Hello, World!" );
    7 }
    8 
    9 require( __DIR__ . '/src/class-plugin.php' );
    10 
    11 $plugin = new Plugin();
    12 $plugin();
     4include( __DIR__ . '/wpo-checker.php' );
  • wpo-checker/trunk/views/sites_selection.tpl

    r2217624 r2241250  
    11<div class="wrap">
    22    <h1>{{title}}</h1>
    3     <p>{'Please, select sites to use in testing.'}</p>
     3    <p>{'Please, select sites to use in your testing.'}</p>
    44    <form method="post" action="options.php" novalidate="novalidate">
    55        {{wordpress_tokens}}
    66
    7         <table class="form-table">
    8             <tbody>
     7        <table class="wp-list-table widefat  striped plugins">
     8            <thead>
     9            <tr>
     10                <td id="cb" class="manage-column column-cb check-column">
     11                    <label class="screen-reader-text" for="cb-select-all-1">{'Check all'}</label>
     12                    <input id="cb-select-all-1" type="checkbox" title="{'Check all'}" {% if all_sites_checked %}checked="checked"{% endif %} />
     13                </td>
     14                <th scope="col" id="name" class="manage-column column-name column-primary">{'Site'}</th>
     15                <th scope="col" id="description" class="manage-column column-description">{'Description'}</th>
     16            </tr>
     17            </thead>
     18            <tbody id="the-list">
     19
    920            {% foreach sites as site %}
    10             <tr>
    11                 <th scope="row"><label for="{{site.id}}">{{site.name}}</label></th>
    12                 <td><input name="{{PLUGIN}}-sites[]" id="{{site.id}}" value="{{site.id}}"
    13                            {% if site.active %}checked="checked"{% endif %} type="checkbox"></td>
     21            <tr {% if site.active %}class="active"{% endif %} >
     22                <th scope="row" class="check-column">
     23                    <input name="{{PLUGIN}}-sites[]" id="{{site.id}}" value="{{site.id}}"
     24                           {% if site.active %}checked="checked"{% endif %} type="checkbox">
     25                </th>
     26
     27                <td class="plugin-title column-primary">
     28                    {% if site.active %}<label for="{{site.id}}"><strong>{{site.name}}</strong></label>{% endif %}
     29                    {% if !site.active %}<label for="{{site.id}}">{{site.name}}</label>{% endif %}
     30                </td>
     31
     32                <td class="column-description desc">
     33                    <div><p>{{site.description}}</p></div>
     34                </td>
    1435            </tr>
    1536            {% endforeach %}
     37
    1638            </tbody>
     39            <tfoot>
     40            <tr>
     41                <td id="cb" class="manage-column column-cb check-column">
     42                    <label class="screen-reader-text" for="cb-select-all-1">{'Check all'}</label>
     43                    <input id="cb-select-all-1" type="checkbox" title="{'Check all'}" {% if all_sites_checked %}checked="checked"{% endif %} />
     44                </td>
     45                <th scope="col" id="name" class="manage-column column-name column-primary">{'Site'}</th>
     46                <th scope="col" id="description" class="manage-column column-description">{'Description'}</th>
     47            </tr>
     48            </tfoot>
    1749        </table>
    1850
    19         <p class="submit"><input name="submit" id="submit" class="button button-primary" value="{'Save changes'}"
     51        <p class="submit"><input name="submit" id="submit" class="button button-primary" value="{'Save selection'}"
    2052                                 type="submit"></p>
    2153    </form>
  • wpo-checker/trunk/wpo-checker.php

    r2217761 r2241250  
    11<?php
     2declare( strict_types = 1 );
     3
    24/*
    35 * Plugin Name: WPO Checker
     
    1921}
    2022
     23if ( ! is_admin() ) {
     24    return;
     25}
     26
    2127require( __DIR__ . '/src/class-plugin.php' );
    2228
    23 add_action( 'init', new Plugin()  );
     29${'wpo-checker'} = new Plugin();
     30${'wpo-checker'}();
Note: See TracChangeset for help on using the changeset viewer.