From 7c80e4d15e487c592362d2a9005d3f190130a82a Mon Sep 17 00:00:00 2001 From: Oscar Sanchez S Date: Sat, 22 Oct 2022 13:15:22 -0500 Subject: [PATCH] phpcbf fixes --- composer.json | 16 +- composer.lock | 338 ++++++++++++++++++++++++++-------- dexter.php | 10 +- inc/class-admin.php | 209 +++++++++++---------- inc/class-api.php | 140 +++++++------- inc/class-plugin.php | 145 ++++++++------- inc/class-pokemon-metabox.php | 290 +++++++++++++++-------------- inc/class-widget.php | 279 ++++++++++++++-------------- templates/settings-page.php | 44 ++--- 9 files changed, 850 insertions(+), 621 deletions(-) diff --git a/composer.json b/composer.json index 798dec7..15d8d79 100644 --- a/composer.json +++ b/composer.json @@ -7,11 +7,19 @@ } ], "require": { - "php": ">=5.2" + "php": ">=5.2", + "10up/phpcs-composer": "dev-master" + }, + "scripts": { + "lint": "phpcs dexter.php templates inc", + "lint-fix": "phpcbf dexter.php templates inc" + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } }, "require-dev": { - "wp-coding-standards/wpcs": "*", - "wimg/php-compatibility": "*", - "dealerdirect/phpcodesniffer-composer-installer": "^0.4.4" + "phpcompatibility/phpcompatibility-wp": "^2.1" } } diff --git a/composer.lock b/composer.lock index 6fbb9af..6d755a7 100644 --- a/composer.lock +++ b/composer.lock @@ -1,37 +1,72 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "16d2901e67e529125084e0c9380198d4", - "packages": [], - "packages-dev": [ + "content-hash": "eaf2bea0c3b50af38a0c45250a13e645", + "packages": [ + { + "name": "10up/phpcs-composer", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/10up/phpcs-composer.git", + "reference": "2f5c3608bc03fe1ca65acf462dd7b5008f6829a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/10up/phpcs-composer/zipball/2f5c3608bc03fe1ca65acf462dd7b5008f6829a0", + "reference": "2f5c3608bc03fe1ca65acf462dd7b5008f6829a0", + "shasum": "" + }, + "require": { + "dealerdirect/phpcodesniffer-composer-installer": "*", + "phpcompatibility/phpcompatibility-wp": "^2", + "squizlabs/php_codesniffer": "^3.4.0", + "wp-coding-standards/wpcs": "*" + }, + "default-branch": true, + "type": "phpcodesniffer-standard", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ephraim Gregor", + "email": "ephraim.gregor@10up.com" + } + ], + "support": { + "issues": "https://github.com/10up/phpcs-composer/issues", + "source": "https://github.com/10up/phpcs-composer/tree/master" + }, + "time": "2021-01-08T03:03:06+00:00" + }, { "name": "dealerdirect/phpcodesniffer-composer-installer", - "version": "v0.4.4", + "version": "v0.7.2", "source": { "type": "git", "url": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer.git", - "reference": "2e41850d5f7797cbb1af7b030d245b3b24e63a08" + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/2e41850d5f7797cbb1af7b030d245b3b24e63a08", - "reference": "2e41850d5f7797cbb1af7b030d245b3b24e63a08", + "url": "https://api.github.com/repos/Dealerdirect/phpcodesniffer-composer-installer/zipball/1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", + "reference": "1c968e542d8843d7cd71de3c5c9c3ff3ad71a1db", "shasum": "" }, "require": { - "composer-plugin-api": "^1.0", - "php": "^5.3|^7", - "squizlabs/php_codesniffer": "*" + "composer-plugin-api": "^1.0 || ^2.0", + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.0 || ^3.1.0 || ^4.0" }, "require-dev": { "composer/composer": "*", - "wimg/php-compatibility": "^8.0" - }, - "suggest": { - "dealerdirect/qa-tools": "All the PHP QA tools you'll need" + "php-parallel-lint/php-parallel-lint": "^1.3.1", + "phpcompatibility/php-compatibility": "^9.0" }, "type": "composer-plugin", "extra": { @@ -49,13 +84,17 @@ "authors": [ { "name": "Franck Nijhof", - "email": "f.nijhof@dealerdirect.nl", - "homepage": "http://workingatdealerdirect.eu", - "role": "Developer" + "email": "franck.nijhof@dealerdirect.com", + "homepage": "http://www.frenck.nl", + "role": "Developer / IT Manager" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Dealerdirect/phpcodesniffer-composer-installer/graphs/contributors" } ], "description": "PHP_CodeSniffer Standards Composer Installer Plugin", - "homepage": "http://workingatdealerdirect.eu", + "homepage": "http://www.dealerdirect.com", "keywords": [ "PHPCodeSniffer", "PHP_CodeSniffer", @@ -63,6 +102,7 @@ "codesniffer", "composer", "installer", + "phpcbf", "phpcs", "plugin", "qa", @@ -73,131 +113,266 @@ "stylecheck", "tests" ], - "time": "2017-12-06T16:27:17+00:00" + "support": { + "issues": "https://github.com/dealerdirect/phpcodesniffer-composer-installer/issues", + "source": "https://github.com/dealerdirect/phpcodesniffer-composer-installer" + }, + "time": "2022-02-04T12:51:07+00:00" }, { - "name": "squizlabs/php_codesniffer", - "version": "3.3.0", + "name": "phpcompatibility/php-compatibility", + "version": "9.3.5", "source": { "type": "git", - "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "d86873af43b4aa9d1f39a3601cc0cfcf02b25266" + "url": "https://github.com/PHPCompatibility/PHPCompatibility.git", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/d86873af43b4aa9d1f39a3601cc0cfcf02b25266", - "reference": "d86873af43b4aa9d1f39a3601cc0cfcf02b25266", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9fb324479acf6f39452e0655d2429cc0d3914243", + "reference": "9fb324479acf6f39452e0655d2429cc0d3914243", "shasum": "" }, "require": { - "ext-simplexml": "*", - "ext-tokenizer": "*", - "ext-xmlwriter": "*", - "php": ">=5.4.0" + "php": ">=5.3", + "squizlabs/php_codesniffer": "^2.3 || ^3.0.2" + }, + "conflict": { + "squizlabs/php_codesniffer": "2.6.2" }, "require-dev": { - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + "phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0" }, - "bin": [ - "bin/phpcs", - "bin/phpcbf" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.x-dev" - } + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." }, + "type": "phpcodesniffer-standard", "notification-url": "https://packagist.org/downloads/", "license": [ - "BSD-3-Clause" + "LGPL-3.0-or-later" ], "authors": [ { - "name": "Greg Sherwood", + "name": "Wim Godden", + "homepage": "https://github.com/wimg", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "homepage": "https://github.com/jrfnl", "role": "lead" + }, + { + "name": "Contributors", + "homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors" } ], - "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", - "homepage": "http://www.squizlabs.com/php-codesniffer", + "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.", + "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", "keywords": [ + "compatibility", "phpcs", "standards" ], - "time": "2018-06-06T23:58:19+00:00" + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibility/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibility" + }, + "time": "2019-12-27T09:44:58+00:00" }, { - "name": "wimg/php-compatibility", - "version": "8.1.0", + "name": "phpcompatibility/phpcompatibility-paragonie", + "version": "1.3.1", "source": { "type": "git", - "url": "https://github.com/wimg/PHPCompatibility.git", - "reference": "4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e" + "url": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie.git", + "reference": "ddabec839cc003651f2ce695c938686d1086cf43" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/wimg/PHPCompatibility/zipball/4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e", - "reference": "4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityParagonie/zipball/ddabec839cc003651f2ce695c938686d1086cf43", + "reference": "ddabec839cc003651f2ce695c938686d1086cf43", "shasum": "" }, "require": { - "php": ">=5.3", - "squizlabs/php_codesniffer": "^2.2 || ^3.0.2" - }, - "conflict": { - "squizlabs/php_codesniffer": "2.6.2" + "phpcompatibility/php-compatibility": "^9.0" }, "require-dev": { - "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0" + "dealerdirect/phpcodesniffer-composer-installer": "^0.7", + "paragonie/random_compat": "dev-master", + "paragonie/sodium_compat": "dev-master" }, "suggest": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3" + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." }, "type": "phpcodesniffer-standard", - "autoload": { - "psr-4": { - "PHPCompatibility\\": "PHPCompatibility/" + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-3.0-or-later" + ], + "authors": [ + { + "name": "Wim Godden", + "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" } + ], + "description": "A set of rulesets for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by the Paragonie polyfill libraries.", + "homepage": "http://phpcompatibility.com/", + "keywords": [ + "compatibility", + "paragonie", + "phpcs", + "polyfill", + "standards" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityParagonie" + }, + "time": "2021-02-15T10:24:51+00:00" + }, + { + "name": "phpcompatibility/phpcompatibility-wp", + "version": "2.1.3", + "source": { + "type": "git", + "url": "https://github.com/PHPCompatibility/PHPCompatibilityWP.git", + "reference": "d55de55f88697b9cdb94bccf04f14eb3b11cf308" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibilityWP/zipball/d55de55f88697b9cdb94bccf04f14eb3b11cf308", + "reference": "d55de55f88697b9cdb94bccf04f14eb3b11cf308", + "shasum": "" + }, + "require": { + "phpcompatibility/php-compatibility": "^9.0", + "phpcompatibility/phpcompatibility-paragonie": "^1.0" }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7" + }, + "suggest": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.7 || This Composer plugin will sort out the PHP_CodeSniffer 'installed_paths' automatically.", + "roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues." + }, + "type": "phpcodesniffer-standard", "notification-url": "https://packagist.org/downloads/", "license": [ - "LGPL-3.0" + "LGPL-3.0-or-later" ], "authors": [ { "name": "Wim Godden", "role": "lead" + }, + { + "name": "Juliette Reinders Folmer", + "role": "lead" } ], - "description": "A set of sniffs for PHP_CodeSniffer that checks for PHP version compatibility.", - "homepage": "http://techblog.wimgodden.be/tag/codesniffer/", + "description": "A ruleset for PHP_CodeSniffer to check for PHP cross-version compatibility issues in projects, while accounting for polyfills provided by WordPress.", + "homepage": "http://phpcompatibility.com/", "keywords": [ "compatibility", + "phpcs", + "standards", + "wordpress" + ], + "support": { + "issues": "https://github.com/PHPCompatibility/PHPCompatibilityWP/issues", + "source": "https://github.com/PHPCompatibility/PHPCompatibilityWP" + }, + "time": "2021-12-30T16:37:40+00:00" + }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.7.1", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1359e176e9307e906dc3d890bcc9603ff6d90619", + "reference": "1359e176e9307e906dc3d890bcc9603ff6d90619", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Greg Sherwood", + "role": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ "phpcs", "standards" ], - "time": "2017-12-27T21:58:38+00:00" + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2022-06-18T07:21:10+00:00" }, { "name": "wp-coding-standards/wpcs", - "version": "0.14.1", + "version": "2.3.0", "source": { "type": "git", - "url": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git", - "reference": "cf6b310caad735816caef7573295f8a534374706" + "url": "https://github.com/WordPress/WordPress-Coding-Standards.git", + "reference": "7da1894633f168fe244afc6de00d141f27517b62" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/WordPress-Coding-Standards/WordPress-Coding-Standards/zipball/cf6b310caad735816caef7573295f8a534374706", - "reference": "cf6b310caad735816caef7573295f8a534374706", + "url": "https://api.github.com/repos/WordPress/WordPress-Coding-Standards/zipball/7da1894633f168fe244afc6de00d141f27517b62", + "reference": "7da1894633f168fe244afc6de00d141f27517b62", "shasum": "" }, "require": { - "php": ">=5.3", - "squizlabs/php_codesniffer": "^2.9.0 || ^3.0.2" + "php": ">=5.4", + "squizlabs/php_codesniffer": "^3.3.1" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5 || ^0.6", + "phpcompatibility/php-compatibility": "^9.0", + "phpcsstandards/phpcsdevtools": "^1.0", + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" }, "suggest": { - "dealerdirect/phpcodesniffer-composer-installer": "^0.4.3" + "dealerdirect/phpcodesniffer-composer-installer": "^0.6 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically." }, "type": "phpcodesniffer-standard", "notification-url": "https://packagist.org/downloads/", @@ -207,7 +382,7 @@ "authors": [ { "name": "Contributors", - "homepage": "https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/graphs/contributors" + "homepage": "https://github.com/WordPress/WordPress-Coding-Standards/graphs/contributors" } ], "description": "PHP_CodeSniffer rules (sniffs) to enforce WordPress coding conventions", @@ -216,16 +391,25 @@ "standards", "wordpress" ], - "time": "2018-02-16T01:57:48+00:00" + "support": { + "issues": "https://github.com/WordPress/WordPress-Coding-Standards/issues", + "source": "https://github.com/WordPress/WordPress-Coding-Standards", + "wiki": "https://github.com/WordPress/WordPress-Coding-Standards/wiki" + }, + "time": "2020-05-13T23:57:56+00:00" } ], + "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "10up/phpcs-composer": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { "php": ">=5.2" }, - "platform-dev": [] + "platform-dev": [], + "plugin-api-version": "2.3.0" } diff --git a/dexter.php b/dexter.php index 71e4a6c..1ce4639 100644 --- a/dexter.php +++ b/dexter.php @@ -12,12 +12,14 @@ namespace WpDexter; -/** Prevent direct access to the file **/ -defined( 'ABSPATH' ) or die( 'Access denied' ); +/** + * Prevent direct access to the file +**/ +defined('ABSPATH') or die('Access denied'); -define( 'WP_DEXTER_PATH', plugin_dir_path( __FILE__ ) ); +define('WP_DEXTER_PATH', plugin_dir_path(__FILE__)); -require_once( WP_DEXTER_PATH . '/inc/class-plugin.php' ); +require_once WP_DEXTER_PATH . '/inc/class-plugin.php'; $plugin = Plugin::get_instance(); $plugin->init(); diff --git a/inc/class-admin.php b/inc/class-admin.php index e1a65ed..b2ace79 100644 --- a/inc/class-admin.php +++ b/inc/class-admin.php @@ -10,117 +10,126 @@ * * @package WpDexter */ -class Admin { +class Admin +{ - /** - * Instance of the plugin. - * - * @var object - */ - public $plugin; + /** + * Instance of the plugin. + * + * @var object + */ + public $plugin; - /** - * Plugin slug. - * - * @var string - */ - const SLUG = 'wp-dexter'; + /** + * Plugin slug. + * + * @var string + */ + const SLUG = 'wp-dexter'; - /** - * The nonce name. - * - * @var string - */ - const NONCE_NAME = 'pokemon_generation_nonce'; + /** + * The nonce name. + * + * @var string + */ + const NONCE_NAME = 'pokemon_generation_nonce'; - /** - * The nonce action - * - * @var string - */ - const NONCE_ACTION = 'pokemon_generation_update'; + /** + * The nonce action + * + * @var string + */ + const NONCE_ACTION = 'pokemon_generation_update'; - /** - * Instantiate this class. - * - * @param object $plugin Instance of the plugin - */ - public function __construct( $plugin ) { - $this->plugin = $plugin; - } + /** + * Instantiate this class. + * + * @param object $plugin Instance of the plugin + */ + public function __construct( $plugin ) + { + $this->plugin = $plugin; + } - /** - * Plugin admin page initializer. - */ - public function init() { - add_action( 'admin_menu', array( $this, 'admin_menus' ) ); - add_action( 'admin_post_wp-dexter-save', array( $this, 'save' ) ); - add_action( 'admin_head', array( $this, 'add_styles' ) ); - add_action( 'init', array( $this, 'textdomain' ) ); - } + /** + * Plugin admin page initializer. + */ + public function init() + { + add_action('admin_menu', array( $this, 'admin_menus' )); + add_action('admin_post_wp-dexter-save', array( $this, 'save' )); + add_action('admin_head', array( $this, 'add_styles' )); + add_action('init', array( $this, 'textdomain' )); + } - /** - * Loads the plugin Text Domain. Enables plugin translation. - */ - public function textdomain() { - load_plugin_textdomain( self::SLUG ); - } + /** + * Loads the plugin Text Domain. Enables plugin translation. + */ + public function textdomain() + { + load_plugin_textdomain(self::SLUG); + } - /** - * Creates the plugin's settings page in the settings menu. - */ - public function admin_menus() { - add_options_page( - __( 'WP-Dexter', 'wp-dexter' ), - __( 'WP-Dexter', 'wp-dexter' ), - 'manage_options', - 'wp-dexter', - array( $this, 'render_options_page' ) - ); - } + /** + * Creates the plugin's settings page in the settings menu. + */ + public function admin_menus() + { + add_options_page( + __('WP-Dexter', 'wp-dexter'), + __('WP-Dexter', 'wp-dexter'), + 'manage_options', + 'wp-dexter', + array( $this, 'render_options_page' ) + ); + } - /** - * Enqueues the CSS stylesheet. - */ - public function add_styles() { - if ( $this->is_dexter_settings_page() ) { - wp_enqueue_style( 'dexter_backend_css', plugins_url( '../dist/css/frontend.css', __FILE__ ), array(), Plugin::VERSION ); - } - } + /** + * Enqueues the CSS stylesheet. + */ + public function add_styles() + { + if ($this->is_dexter_settings_page() ) { + wp_enqueue_style('dexter_backend_css', plugins_url('../dist/css/frontend.css', __FILE__), array(), Plugin::VERSION); + } + } - /** - * Renders the main page. - */ - public function render_options_page() { - include( dirname( __FILE__ ) . '/../templates/settings-page.php' ); - } + /** + * Renders the main page. + */ + public function render_options_page() + { + include dirname(__FILE__) . '/../templates/settings-page.php'; + } - /** - * Checks if this is Dexter settings page. - * - * @return bool - */ - public function is_dexter_settings_page() { - if ( 'settings_page_wp-dexter' === get_current_screen()->id ) { - return true; - } - return false; - } + /** + * Checks if this is Dexter settings page. + * + * @return bool + */ + public function is_dexter_settings_page() + { + if ('settings_page_wp-dexter' === get_current_screen()->id ) { + return true; + } + return false; + } - /** - * Process and saves the form settings. - */ - public function save() { - $verify = ( - isset( $_POST['pokemon_generation'], $_POST[ self::NONCE_NAME ] ) - && - wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION ) - ); + /** + * Process and saves the form settings. + */ + public function save() + { + $verify = ( + isset($_POST['pokemon_generation'], $_POST[ self::NONCE_NAME ]) + && + wp_verify_nonce(sanitize_key(wp_unslash($_POST[ self::NONCE_NAME ])), self::NONCE_ACTION) + ); - if ( true === $verify ) { - update_option( 'wp_dexter_pokemon_generation', sanitize_text_field( wp_unslash( $_POST['pokemon_generation'] ) ) ); - wp_redirect( admin_url( 'options-general.php?page=wp-dexter' ) . '&updated=true' ); - exit; - } - } + if (true === $verify ) { + update_option('wp_dexter_pokemon_generation', sanitize_text_field(wp_unslash($_POST['pokemon_generation']))); + wp_redirect(admin_url('options-general.php?page=wp-dexter') . '&updated=true'); + exit; + } + } } diff --git a/inc/class-api.php b/inc/class-api.php index d26f59c..ffb52cd 100644 --- a/inc/class-api.php +++ b/inc/class-api.php @@ -14,79 +14,83 @@ * * @package WpDexter */ -class Api { - /** - * The URL where we request the Pokémon data from. - * - * @var string API_URL - */ - const API_URL = 'http://pokeapi.co/api/v2'; +class Api +{ + /** + * The URL where we request the Pokémon data from. + * + * @var string API_URL + */ + const API_URL = 'http://pokeapi.co/api/v2'; - /** - * Retrieves all the Pokémon data. - * - * @return array|mixed|object - */ - public function get_pokemon_data() { - // Default gen is 251 because it is the best! - $pokemon_generation = get_option( 'wp_dexter_pokemon_generation', 251 ); + /** + * Retrieves all the Pokémon data. + * + * @return array|mixed|object + */ + public function get_pokemon_data() + { + // Default gen is 251 because it is the best! + $pokemon_generation = get_option('wp_dexter_pokemon_generation', 251); - $pokemon_data = wp_cache_get( 'pokemon-data', 'wp-dexter' ); - if ( ! $pokemon_data ) { - $pokemon_data = wp_remote_get( self::API_URL . '/pokemon/' . rand( 1, $pokemon_generation ) ); - if ( 200 !== wp_remote_retrieve_response_code( $pokemon_data ) ) { - $pokemon_data = false; - } - wp_cache_set( 'pokemon-data', $pokemon_data, 'wp-dexter', 5 * MINUTE_IN_SECONDS ); - } + $pokemon_data = wp_cache_get('pokemon-data', 'wp-dexter'); + if (! $pokemon_data ) { + $pokemon_data = wp_remote_get(self::API_URL . '/pokemon/' . rand(1, $pokemon_generation)); + if (200 !== wp_remote_retrieve_response_code($pokemon_data) ) { + $pokemon_data = false; + } + wp_cache_set('pokemon-data', $pokemon_data, 'wp-dexter', 5 * MINUTE_IN_SECONDS); + } - return json_decode( wp_remote_retrieve_body( $pokemon_data ) ); - } + return json_decode(wp_remote_retrieve_body($pokemon_data)); + } - /** - * Return formatted generations. + /** + * Return formatted generations. * These don't need to be retrieved often, so cache is set for a month. - * - * @return array|false|mixed The pokemon generations formatted or false if pokeAPI is down. - */ - public function get_generations() { - $generations = wp_cache_get( 'pokemon-generations', 'wp-dexter' ); - if ( ! $generations ) { - $generations_data = wp_remote_get( self::API_URL . '/generation/' ); - if ( 200 === wp_remote_retrieve_response_code( $generations_data ) ) { - $generations_data = json_decode( wp_remote_retrieve_body( $generations_data ) ); - $generations = []; - $pokemon_counter = 0; - foreach ( $generations_data->results as $generation ) { - $generation = wp_remote_get( $generation->url ); - if ( 200 === wp_remote_retrieve_response_code( $generation ) ) { - $generation = json_decode( wp_remote_retrieve_body( $generation ) ); - $pokemon_counter += count( $generation->pokemon_species ); - $generations[] = [ - 'gen_name' => ucfirst( $generation->name ), - 'gen_number' => $pokemon_counter - ]; - } else { - return false; - } - } - wp_cache_set( 'pokemon-generations', $generations, 'wp-dexter', MONTH_IN_SECONDS ); - } else { - return false; - } - } + * + * @return array|false|mixed The pokemon generations formatted or false if pokeAPI is down. + */ + public function get_generations() + { + $generations = wp_cache_get('pokemon-generations', 'wp-dexter'); + if (! $generations ) { + $generations_data = wp_remote_get(self::API_URL . '/generation/'); + if (200 === wp_remote_retrieve_response_code($generations_data) ) { + $generations_data = json_decode(wp_remote_retrieve_body($generations_data)); + $generations = []; + $pokemon_counter = 0; + foreach ( $generations_data->results as $generation ) { + $generation = wp_remote_get($generation->url); + if (200 === wp_remote_retrieve_response_code($generation) ) { + $generation = json_decode(wp_remote_retrieve_body($generation)); + $pokemon_counter += count($generation->pokemon_species); + $generations[] = [ + 'gen_name' => ucfirst($generation->name), + 'gen_number' => $pokemon_counter + ]; + } else { + return false; + } + } + wp_cache_set('pokemon-generations', $generations, 'wp-dexter', MONTH_IN_SECONDS); + } else { + return false; + } + } - return $generations; - } + return $generations; + } - /** - * Display a message if pokeAPI is down. - */ - public function api_failed_message() { - ?> -
-

-
- +
+

+
+ load_files(); - $this->load_classes(); - $this->add_actions(); - } + /** + * Initializes the plugin by loading files and classes. + */ + public function init() + { + $this->load_files(); + $this->load_classes(); + $this->add_actions(); + } - /** - * Loads plugin files. - */ - public function load_files() { - require_once( dirname( __FILE__ ) . '/class-admin.php'); - require_once( dirname( __FILE__ ) . '/class-api.php'); - require_once( dirname( __FILE__ ) . '/class-pokemon-metabox.php'); - require_once( dirname( __FILE__ ) . '/class-widget.php' ); - } + /** + * Loads plugin files. + */ + public function load_files() + { + include_once dirname(__FILE__) . '/class-admin.php'; + include_once dirname(__FILE__) . '/class-api.php'; + include_once dirname(__FILE__) . '/class-pokemon-metabox.php'; + include_once dirname(__FILE__) . '/class-widget.php'; + } - /** - * Instantiates the plugin classes in $components . - */ - public function load_classes() { - $this->components = new \stdClass(); - $this->components->api = new Api(); - $this->components->admin_page = new Admin( $this ); - $this->components->admin_page->init(); - $this->components->metabox = new Pokemon_Metabox( $this ); + /** + * Instantiates the plugin classes in $components . + */ + public function load_classes() + { + $this->components = new \stdClass(); + $this->components->api = new Api(); + $this->components->admin_page = new Admin($this); + $this->components->admin_page->init(); + $this->components->metabox = new Pokemon_Metabox($this); $this->components->metabox->init(); - } + } - /** - * Adds plugin actions. - */ - public function add_actions() { - add_action( 'widgets_init', array( $this, 'register_widget' ) ); - } + /** + * Adds plugin actions. + */ + public function add_actions() + { + add_action('widgets_init', array( $this, 'register_widget' )); + } - /** - * Registers the plugin widget. - */ - public function register_widget() { - register_widget( __NAMESPACE__ . '\Widget' ); - } + /** + * Registers the plugin widget. + */ + public function register_widget() + { + register_widget(__NAMESPACE__ . '\Widget'); + } } diff --git a/inc/class-pokemon-metabox.php b/inc/class-pokemon-metabox.php index 2d204a0..edb3032 100644 --- a/inc/class-pokemon-metabox.php +++ b/inc/class-pokemon-metabox.php @@ -10,145 +10,155 @@ * * @package WpDexter */ -class Pokemon_Metabox { - - /** - * Instance of the plugin. - * - * @var object - */ - public $plugin; - - /** - * Stores the Pokémon data. - * - * @var object - */ - public $pokemon_data; - - /** - * Pokemon_Metabox constructor. - * - * Instantiates this class and loads the Pokémon data retrieved from the API. - * - * @param $plugin - */ - public function __construct( $plugin ) { - $this->plugin = $plugin; - $this->pokemon_data = $this->plugin->components->api->get_pokemon_data(); - } - - public function init() { - add_action( 'init', [ $this, 'register_block'] ); - } - - /** - * Registers out Pokémon block - */ - public function register_block() { - register_block_type( - WP_DEXTER_PATH . '/js/blocks', - [ - 'render_callback' => [ $this, 'render_block_callback' ], - ] - - ); - } - - /** - * Render callback method for the block - * - * @param array $attributes The blocks attributes - * - * @return string The rendered block markup. - */ - public function render_block_callback( $attributes ) { - $html = ''; - if ( ! empty ( $attributes['pokemonNumber'] ) && is_numeric( $attributes['pokemonNumber'] ) ) : - ob_start(); ?> -
- - - -

- - -

- - -

- - -

- - -

- -
- -
- - -

pokemon_data->name ) ); ?>

- -
-

- -
- -

pokemon_data->id ) ); ?>

- -

pokemon_data->height ) ); ?>

- -

pokemon_data->weight ) ); ?>

- -

pokemon_data->types[0]->type->name ) ); ?>

- plugin = $plugin; + $this->pokemon_data = $this->plugin->components->api->get_pokemon_data(); + } + + public function init() + { + add_action('init', [ $this, 'register_block']); + } + + /** + * Registers out Pokémon block + */ + public function register_block() + { + register_block_type( + WP_DEXTER_PATH . '/js/blocks', + [ + 'render_callback' => [ $this, 'render_block_callback' ], + ] + ); + } + + /** + * Render callback method for the block + * + * @param array $attributes The blocks attributes + * + * @return string The rendered block markup. + */ + public function render_block_callback( $attributes ) + { + $html = ''; + if (! empty($attributes['pokemonNumber']) && is_numeric($attributes['pokemonNumber']) ) : + ob_start(); ?> +
+ + + +

+ + +

+ + +

+ + +

+ + +

+ +
+ +
+ + +

pokemon_data->name)); ?>

+ +
+

+ +
+ +

pokemon_data->id)); ?>

+ +

pokemon_data->height)); ?>

+ +

pokemon_data->weight)); ?>

+ +

pokemon_data->types[0]->type->name)); ?>

+ __( 'Display Pokemon data on your site!', 'wp-dexter' ), - 'customize_selective_refresh' => true, - ) - ); - } - - /** - * Output Widget Form. - * - * @param array $instance - * @return void - */ - public function form( $instance ) { - // Set widget defaults - $defaults = array( - 'title' => '', - 'show_number_checkbox' => '', - 'show_type_checkbox' => '', - 'show_weight_checkbox' => '', - 'show_height_checkbox' => '', - ); - // Parse current settings with defaults - extract( wp_parse_args( ( array ) $instance, $defaults ) ); ?> - -

- - -

- -

- /> - -

- -

- /> - -

- -

- /> - -

- -

- /> - -

- '; - // Display widget title if defined - if ( $title ) { - echo $before_title . $title . $after_title; - } - echo '
'; - - //Display meta_box - if ( ! $plugin->components->metabox->pokemon_data ) { - $plugin->components->metabox->api_failed_message(); - } else { - $plugin->components->metabox->display_meta_box(); - - if ( $show_number_checkbox ) { - $plugin->components->metabox->show_number(); - } - - if ( $show_type_checkbox ) { - $plugin->components->metabox->show_type(); - } - - if ( $show_weight_checkbox ) { - $plugin->components->metabox->show_weight(); - } - - if ( $show_height_checkbox ) { - $plugin->components->metabox->show_height(); - } - } - - echo $after_widget; - } +class Widget extends \WP_Widget +{ + + /** + * WP-Dexter ID Base. + * + * @var string + */ + const ID_BASE = 'dexter_widget'; + + /** + * Widget constructor. Instantiates the class. + */ + function __construct() + { + parent::__construct( + self::ID_BASE, + __('Dexter', 'wp-dexter'), + array( + 'description' => __('Display Pokemon data on your site!', 'wp-dexter'), + 'customize_selective_refresh' => true, + ) + ); + } + + /** + * Output Widget Form. + * + * @param array $instance + * @return void + */ + public function form( $instance ) + { + // Set widget defaults + $defaults = array( + 'title' => '', + 'show_number_checkbox' => '', + 'show_type_checkbox' => '', + 'show_weight_checkbox' => '', + 'show_height_checkbox' => '', + ); + // Parse current settings with defaults + extract(wp_parse_args(( array ) $instance, $defaults)); ?> + +

+ + +

+ +

+ /> + +

+ +

+ /> + +

+ +

+ /> + +

+ +

+ /> + +

+ '; + // Display widget title if defined + if ($title ) { + echo $before_title . $title . $after_title; + } + echo '
'; + + //Display meta_box + if (! $plugin->components->metabox->pokemon_data ) { + $plugin->components->metabox->api_failed_message(); + } else { + $plugin->components->metabox->display_meta_box(); + + if ($show_number_checkbox ) { + $plugin->components->metabox->show_number(); + } + + if ($show_type_checkbox ) { + $plugin->components->metabox->show_type(); + } + + if ($show_weight_checkbox ) { + $plugin->components->metabox->show_weight(); + } + + if ($show_height_checkbox ) { + $plugin->components->metabox->show_height(); + } + } + + echo $after_widget; + } } diff --git a/templates/settings-page.php b/templates/settings-page.php index 1e48800..c277089 100644 --- a/templates/settings-page.php +++ b/templates/settings-page.php @@ -7,30 +7,30 @@ namespace WpDexter; -if ( ! ( $this instanceof Admin ) ) { - return; +if (! ( $this instanceof Admin ) ) { + return; } -$wp_dexter_pokemon_generation = get_option( 'wp_dexter_pokemon_generation', 251 ); +$wp_dexter_pokemon_generation = get_option('wp_dexter_pokemon_generation', 251); ?>
-

-
-

-

-
- - - - - -
-
+

+
+

+

+
+ + + + + +
+