From 40a4c65b0e0a5437719549a62eff9c0e168c8604 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 15 Jan 2018 11:21:21 +0100 Subject: [PATCH] Fix coding standards issues and make plugin fully translatable. --- load-files.php | 13 +- phpcs.xml | 10 +- src/ruleset-wordpress.xml | 16 +- src/wpcli.php | 9 +- src/wpephpcompat.php | 147 ++++++++------- uninstall.php | 6 + wpengine-phpcompat.php | 364 ++++++++++++++++++++++---------------- 7 files changed, 336 insertions(+), 229 deletions(-) diff --git a/load-files.php b/load-files.php index c46d301..13d3481 100644 --- a/load-files.php +++ b/load-files.php @@ -1,6 +1,15 @@ */tests/* */vendor/* - + + + @@ -13,6 +15,12 @@ + + + + + + diff --git a/src/ruleset-wordpress.xml b/src/ruleset-wordpress.xml index a07bb11..dfdcf8c 100644 --- a/src/ruleset-wordpress.xml +++ b/src/ruleset-wordpress.xml @@ -4,6 +4,20 @@ WordPress-Core Custom - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/wpcli.php b/src/wpcli.php index c6e9cd3..cdc4de9 100644 --- a/src/wpcli.php +++ b/src/wpcli.php @@ -1,4 +1,11 @@ 'Test compatibility with different PHP versions.', - 'synopsis' => array( + 'synopsis' => array( array( 'type' => 'positional', 'name' => 'version', diff --git a/src/wpephpcompat.php b/src/wpephpcompat.php index 6b2ead3..a168162 100644 --- a/src/wpephpcompat.php +++ b/src/wpephpcompat.php @@ -1,6 +1,15 @@ '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#jetpack - '*/wordfence/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#wordfence-security - '*/woocommerce/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#woocommerce - '*/wp-migrate-db/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#wp-migrate-db - '*/easy-digital-downloads/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#easy-digital-downloads - '*/updraftplus/*' => '7.0', - '*/megamenu/*' => '7.0', - '*/tablepress/*' => '7.0', - '*/myMail/*' => '7.0', - '*/wp-spamshield/*' => '7.0', + '*/jetpack/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#jetpack + '*/wordfence/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#wordfence-security + '*/woocommerce/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#woocommerce + '*/wp-migrate-db/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#wp-migrate-db + '*/easy-digital-downloads/*' => '7.0', // https://github.com/wpengine/phpcompat/wiki/Results#easy-digital-downloads + '*/updraftplus/*' => '7.0', + '*/megamenu/*' => '7.0', + '*/tablepress/*' => '7.0', + '*/myMail/*' => '7.0', + '*/wp-spamshield/*' => '7.0', '*/vendor/stripe/stripe-php/lib/StripeObject.php' => '7.0', // https://github.com/wpengine/phpcompat/issues/89 - '*/gravityforms/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/85 - '*/download-monitor/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/84 - '*/query-monitor/*' => '7.0', // https://wordpress.org/support/topic/false-positive-showing-query-monitor-as-not-php-7-compatible/ - '*/bbpress/*' => '7.0', // https://wordpress.org/support/topic/false-positive-showing-bbpress-as-not-php-7-compatible/ - '*/comet-cache/*' => '7.0', // https://wordpress.org/support/topic/false-positive-comet-cache/ - '*/comment-mail/*' => '7.0', // https://wordpress.org/support/topic/false-positive-comment-mail/ + '*/gravityforms/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/85 + '*/download-monitor/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/84 + '*/query-monitor/*' => '7.0', // https://wordpress.org/support/topic/false-positive-showing-query-monitor-as-not-php-7-compatible/ + '*/bbpress/*' => '7.0', // https://wordpress.org/support/topic/false-positive-showing-bbpress-as-not-php-7-compatible/ + '*/comet-cache/*' => '7.0', // https://wordpress.org/support/topic/false-positive-comet-cache/ + '*/comment-mail/*' => '7.0', // https://wordpress.org/support/topic/false-positive-comment-mail/ '*/social-networks-auto-poster-facebook-twitter-g/*' => '7.0', // https://wordpress.org/plugins/social-networks-auto-poster-facebook-twitter-g/ - '*/mailpoet/*' => '7.0', // https://wordpress.org/support/topic/false-positive-mailpoet-3-not-compatible-with-php7/ - '*/give/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/148 - '*/woocommerce-pdf-invoices-packing-slips/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/160 - '*/iwp-client/*' => '7.0', // https://wordpress.org/support/topic/iwp-client-and-php-7-compatibility/ + '*/mailpoet/*' => '7.0', // https://wordpress.org/support/topic/false-positive-mailpoet-3-not-compatible-with-php7/ + '*/give/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/148 + '*/woocommerce-pdf-invoices-packing-slips/*' => '7.0', // https://github.com/wpengine/phpcompat/issues/160 + '*/iwp-client/*' => '7.0', // https://wordpress.org/support/topic/iwp-client-and-php-7-compatibility/ ); /** + * Constructor. + * + * @since 1.0.0 + * * @param string $dir Base plugin directory. */ - function __construct( $dir ) { + public function __construct( $dir ) { $this->base = $dir; - $this->cli = new PHP_CodeSniffer_CLI(); + $this->cli = new PHP_CodeSniffer_CLI(); } /** - * Start the testing process. + * Starts the testing process. * - * @since 1.0.0 - * @return null + * @since 1.0.0 */ public function start_test() { - $this->debug_log( 'startScan: ' . isset( $_POST['startScan'] ) ); /** @@ -137,7 +143,7 @@ public function start_test() { $timestamp = wp_next_scheduled( 'wpephpcompat_start_test_cron' ); - if ( false == $timestamp ) { + if ( false === (bool) $timestamp ) { wp_schedule_single_event( time() + $timeout, 'wpephpcompat_start_test_cron' ); } return; @@ -159,7 +165,8 @@ public function start_test() { update_option( 'wpephpcompat.only_active', $this->only_active, false ); $this->debug_log( 'Generating directory list.' ); - //Add plugins and themes. + + // Add plugins and themes. $this->generate_directory_list(); $count_jobs = wp_count_posts( 'wpephpcompat_jobs' ); @@ -167,7 +174,7 @@ public function start_test() { } else { // Get scan settings from database. $this->test_version = get_option( 'wpephpcompat.test_version' ); - $this->only_active = get_option( 'wpephpcompat.only_active' ); + $this->only_active = get_option( 'wpephpcompat.only_active' ); } $args = array( @@ -176,6 +183,7 @@ public function start_test() { 'orderby' => 'title', 'order' => 'ASC', ); + $directories = get_posts( $args ); $this->debug_log( count( $directories ) . ' plugins left to process.' ); @@ -216,7 +224,7 @@ public function start_test() { if ( $count > 2 ) { // If we've already tried twice, skip it. $scan_results .= __( 'The plugin/theme was skipped as it was too large to scan before the server killed the process.', 'php-compatibility-checker' ) . "\n\n"; - update_option( 'wpephpcompat.scan_results', $scan_results , false ); + update_option( 'wpephpcompat.scan_results', $scan_results, false ); wp_delete_post( $directory->ID ); $count = 0; $this->debug_log( 'Skipped: ' . $directory->post_title ); @@ -240,12 +248,13 @@ public function start_test() { if ( ! empty( $update ) ) { $version = get_post_meta( $directory->ID, 'version', true ); + $scan_results .= 'Update Available: ' . $update . '; Current Version: ' . $version . ";\n"; } $scan_results .= "\n"; - update_option( 'wpephpcompat.scan_results', $scan_results , false ); + update_option( 'wpephpcompat.scan_results', $scan_results, false ); wp_delete_post( $directory->ID ); } @@ -258,11 +267,13 @@ public function start_test() { } /** - * Runs the actual PHPCompatibility test. - * - * @since 1.0.0 - * @return string Scan results. - */ + * Runs the actual PHPCompatibility test. + * + * @since 1.0.0 + * + * @param string $dir Directory to scan. + * @return string Scan results. + */ public function process_file( $dir ) { $this->values['files'] = $dir; $this->values['testVersion'] = $this->test_version; @@ -289,9 +300,10 @@ public function process_file( $dir ) { } /** - * Generate a list of ignored files and directories. + * Generates a list of ignored files and directories. * * @since 1.0.3 + * * @return array An array containing files and directories that should be ignored. */ public function generate_ignored_list() { @@ -314,11 +326,10 @@ public function generate_ignored_list() { } /** - * Generate a list of directories to scan and populate the queue. - * - * @since 1.0.0 - * @return null - */ + * Generates a list of directories to scan and populate the queue. + * + * @since 1.0.0 + */ public function generate_directory_list() { if ( ! function_exists( 'get_plugins' ) ) { @@ -332,7 +343,7 @@ public function generate_directory_list() { $update_plugins = get_site_transient( 'update_plugins' ); foreach ( $all_plugins as $k => $v ) { - //Exclude our plugin. + // Exclude our plugin. if ( 'PHP Compatibility Checker' === $v['Name'] ) { continue; } @@ -342,7 +353,7 @@ public function generate_directory_list() { // Get array of active plugins. $active_plugins = get_option( 'active_plugins' ); - if ( ! in_array( $k, $active_plugins ) ) { + if ( ! in_array( $k, $active_plugins, true ) ) { continue; } } @@ -379,7 +390,7 @@ public function generate_directory_list() { foreach ( $all_themes as $k => $v ) { if ( 'yes' === $this->only_active ) { $current_theme = wp_get_theme(); - if ( $all_themes[ $k ]->Name != $current_theme->Name ) { + if ( $all_themes[ $k ]->Name !== $current_theme->Name ) { continue; } } @@ -416,10 +427,9 @@ public function clean_report( $report ) { } /** - * Remove all database entries created by the scan. + * Removes all database entries created by the scan. * - * @since 1.0.0 - * @return null + * @since 1.0.0 */ public function clean_after_scan() { // Delete options created during the scan. @@ -430,11 +440,12 @@ public function clean_after_scan() { // Clear scheduled cron. wp_clear_scheduled_hook( 'wpephpcompat_start_test_cron' ); - //Make sure all directories are removed from the queue. + // Make sure all directories are removed from the queue. $args = array( 'posts_per_page' => -1, 'post_type' => 'wpephpcompat_jobs', ); + $directories = get_posts( $args ); foreach ( $directories as $directory ) { @@ -443,7 +454,7 @@ public function clean_after_scan() { } /** - * Add a path to the wpephpcompat_jobs custom post type. + * Adds a path to the wpephpcompat_jobs custom post type. * * @param string $name Plugin or theme name. * @param string $path Full path to the plugin or theme directory. @@ -451,27 +462,27 @@ public function clean_after_scan() { */ private function add_directory( $name, $path ) { $dir = array( - 'post_title' => $name, - 'post_content' => $path, - 'post_status' => 'publish', - 'post_author' => 1, - 'post_type' => 'wpephpcompat_jobs', + 'post_title' => $name, + 'post_content' => $path, + 'post_status' => 'publish', + 'post_author' => 1, + 'post_type' => 'wpephpcompat_jobs', ); return wp_insert_post( $dir ); } /** - * Log to the error log if WP_DEBUG is enabled. + * Logs to the error log if WP_DEBUG is enabled. * - * @since 1.0.0 - * @param string $message Message to log. - * @return null + * @since 1.0.0 + * + * @param string $message Message to log. */ private function debug_log( $message ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true && ! $this->is_command_line() ) { if ( is_array( $message ) || is_object( $message ) ) { - error_log( print_r( $message , true ) ); + error_log( print_r( $message, true ) ); } else { error_log( 'WPE PHP Compatibility: ' . $message ); } @@ -485,6 +496,6 @@ private function debug_log( $message ) { * @return boolean Returns true if the request came from the command line. */ private function is_command_line() { - return defined( 'WP_CLI' ) || defined( 'PHPUNIT_TEST' ) || php_sapi_name() == 'cli'; + return defined( 'WP_CLI' ) || defined( 'PHPUNIT_TEST' ) || php_sapi_name() === 'cli'; } } diff --git a/uninstall.php b/uninstall.php index b9ae18e..56dca15 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,4 +1,10 @@ init(); } + return self::$instance; } /** - * Initialize hooks and setup environment variables. + * Initializes hooks and setup environment variables. * * @since 0.1.0 + * @static */ public static function init() { + $instance = self::instance(); // Build our tools page. - add_action( 'admin_menu', array( self::instance(), 'create_menu' ) ); + add_action( 'admin_menu', array( $instance, 'create_menu' ) ); // Load our JavaScript. - add_action( 'admin_enqueue_scripts', array( self::instance(), 'admin_enqueue' ) ); + add_action( 'admin_enqueue_scripts', array( $instance, 'admin_enqueue' ) ); // The action to run the compatibility test. - add_action( 'wp_ajax_wpephpcompat_start_test', array( self::instance(), 'start_test' ) ); - add_action( 'wp_ajax_wpephpcompat_check_status', array( self::instance(), 'check_status' ) ); - add_action( 'wpephpcompat_start_test_cron', array( self::instance(), 'start_test' ) ); - add_action( 'wp_ajax_wpephpcompat_clean_up', array( self::instance(), 'clean_up' ) ); + add_action( 'wp_ajax_wpephpcompat_start_test', array( $instance, 'start_test' ) ); + add_action( 'wp_ajax_wpephpcompat_check_status', array( $instance, 'check_status' ) ); + add_action( 'wpephpcompat_start_test_cron', array( $instance, 'start_test' ) ); + add_action( 'wp_ajax_wpephpcompat_clean_up', array( $instance, 'clean_up' ) ); // Create custom post type. - add_action( 'init', array( self::instance(), 'create_job_queue' ) ); + add_action( 'init', array( $instance, 'create_job_queue' ) ); // Handle activation notice. - register_activation_hook( __FILE__, array( self::instance(), 'set_activation_notice_flag' ) ); - add_action( 'admin_notices', array( self::instance(), 'maybe_show_activation_notice' ) ); + register_activation_hook( __FILE__, array( $instance, 'set_activation_notice_flag' ) ); + add_action( 'admin_notices', array( $instance, 'maybe_show_activation_notice' ) ); // Add plugin action link. - add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( self::instance(), 'filter_plugin_links' ) ); + add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $instance, 'filter_plugin_links' ) ); } /** - * Return an array of available PHP versions to test. + * Returns an array of available PHP versions to test. + * + * @since 1.0.0 + * + * @return array Associative array of available PHP versions. */ function get_phpversions() { $versions = array( @@ -102,14 +133,14 @@ function get_phpversions() { } /** - * Start the test! + * Starts the test. + * + * @since 1.0.0 * - * @since 1.0.0 * @action wp_ajax_wpephpcompat_start_test * @action wpephpcompat_start_test_cron - * @return null */ - function start_test() { + public function start_test() { if ( current_user_can( WPEPHPCOMPAT_CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { global $wpdb; @@ -146,22 +177,23 @@ function start_test() { } /** - * Check the progress or result of the tests. + * Checks the progress or result of the tests. * * @todo Use heartbeat API. * @since 1.0.0 + * * @action wp_ajax_wpephpcompat_check_status - * @return null */ - function check_status() { + public function check_status() { if ( current_user_can( WPEPHPCOMPAT_CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { - $scan_status = get_option( 'wpephpcompat.status' ); - $count_jobs = wp_count_posts( 'wpephpcompat_jobs' ); - $total_jobs = get_option( 'wpephpcompat.numdirs' ); + $scan_status = get_option( 'wpephpcompat.status' ); + $count_jobs = wp_count_posts( 'wpephpcompat_jobs' ); + $total_jobs = get_option( 'wpephpcompat.numdirs' ); $test_version = get_option( 'wpephpcompat.test_version' ); - $only_active = get_option( 'wpephpcompat.only_active' ); + $only_active = get_option( 'wpephpcompat.only_active' ); $active_job = false; + $jobs = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'wpephpcompat_jobs', @@ -184,8 +216,8 @@ function check_status() { // If the scan is still running. if ( $scan_status ) { - $to_encode['results'] = '0'; - $to_encode['progress'] = ( ( $total_jobs - $count_jobs->publish ) / $total_jobs) * 100; + $to_encode['results'] = '0'; + $to_encode['progress'] = ( ( $total_jobs - $count_jobs->publish ) / $total_jobs ) * 100; } else { // Else return the results and clean up! $scan_results = get_option( 'wpephpcompat.scan_results' ); @@ -200,14 +232,14 @@ function check_status() { } /** - * Make an Ajax call to start the scan in the background. + * Makes an Ajax call to start the scan in the background. * * @since 1.3.2 - * @param string $test_version Version of PHP to test. - * @param string $only_active Scan only active plugins or all? - * @return null + * + * @param string $test_version Version of PHP to test. + * @param string $only_active Whether to scan only active plugins or all. */ - function fork_scan( $test_version, $only_active ) { + public function fork_scan( $test_version, $only_active ) { $query = array( 'action' => 'wpephpcompat_start_test', ); @@ -215,7 +247,7 @@ function fork_scan( $test_version, $only_active ) { // Keep track of these variables. $body = array( 'test_version' => $test_version, - 'only_active' => $only_active, + 'only_active' => $only_active, ); // Instantly return! @@ -234,12 +266,13 @@ function fork_scan( $test_version, $only_active ) { } /** - * Remove all database options from the database. + * Removes all database options from the database. * * @since 1.3.2 + * * @action wp_ajax_wpephpcompat_clean_up */ - function clean_up() { + public function clean_up() { if ( current_user_can( WPEPHPCOMPAT_CAPABILITY ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { $wpephpc = new WPEPHPCompat( dirname( __FILE__ ) ); $wpephpc->clean_after_scan(); @@ -249,48 +282,47 @@ function clean_up() { } /** - * Create custom post type to store the directories we need to process. + * Creates custom post type to store the directories we need to process. * * @since 1.0.0 - * @return null */ - function create_job_queue() { - register_post_type( 'wpephpcompat_jobs', - array( - 'labels' => array( - 'name' => __( 'Jobs' ), - 'singular_name' => __( 'Job' ), - ), - 'public' => false, + public function create_job_queue() { + register_post_type( 'wpephpcompat_jobs', array( + 'labels' => array( + 'name' => __( 'Jobs', 'php-compatibility-checker' ), + 'singular_name' => __( 'Job', 'php-compatibility-checker' ), + ), + 'public' => false, 'has_archive' => false, - ) - ); + ) ); } /** - * Enqueue our JavaScript and CSS. + * Enqueues our JavaScript and CSS. * * @since 1.0.0 + * * @action admin_enqueue_scripts - * @return null + * + * @param string $hook Current page hook name. */ - function admin_enqueue( $hook ) { + public function admin_enqueue( $hook ) { // Only enqueue these assets on the settings page. if ( $this->page !== $hook ) { return; } - // Styles + // Styles. wp_enqueue_style( 'wpephpcompat-style', plugins_url( '/src/css/style.css', __FILE__ ) ); - // Scripts + // Scripts. wp_enqueue_script( 'wpephpcompat-handlebars', plugins_url( '/src/js/handlebars.js', __FILE__ ), array( 'jquery' ) ); wp_enqueue_script( 'wpephpcompat-download', plugins_url( '/src/js/download.min.js', __FILE__ ) ); wp_enqueue_script( 'wpephpcompat', plugins_url( '/src/js/run.js', __FILE__ ), array( 'jquery', 'wpephpcompat-handlebars', 'wpephpcompat-download' ) ); /** - * i18n strings + * Strings for i18n. * * These translated strings can be access in jquery with window.wpephpcompat object. */ @@ -312,10 +344,10 @@ function admin_enqueue( $hook ) { * Add the settings page to the wp-admin menu. * * @since 1.0.0 + * * @action admin_menu - * @return null */ - function create_menu() { + public function create_menu() { // Create Tools sub-menu. $this->page = add_submenu_page( 'tools.php', __( 'PHP Compatibility', 'php-compatibility-checker' ), __( 'PHP Compatibility', 'php-compatibility-checker' ), WPEPHPCOMPAT_CAPABILITY, WPEPHPCOMPAT_ADMIN_PAGE_SLUG, array( self::instance(), 'settings_page' ) ); } @@ -324,38 +356,39 @@ function create_menu() { * Render method for the settings page. * * @since 1.0.0 - * @return null */ - function settings_page() { - // Discovers last options used. + public function settings_page() { + // Discover last options used. $test_version = get_option( 'wpephpcompat.test_version' ); - $only_active = get_option( 'wpephpcompat.only_active' ); + $only_active = get_option( 'wpephpcompat.only_active' ); - // Determine if current site is a WP Engine customer + // Determine if current site is a WP Engine customer. $is_wpe_customer = ! empty( $_SERVER['IS_WPE'] ) && $_SERVER['IS_WPE']; $phpversions = $this->get_phpversions(); // Assigns defaults for the scan if none are found in the database. $test_version = ( ! empty( $test_version ) ) ? $test_version : '7.0'; - $only_active = ( ! empty( $only_active ) ) ? $only_active : 'yes'; + $only_active = ( ! empty( $only_active ) ) ? $only_active : 'yes'; - // Content variables - $url_get_hosting = esc_url( 'https://wpeng.in/5a0336/' ); - $url_wpe_agency_partners = esc_url( 'https://wpeng.in/fa14e4/' ); + // Content variables. + $url_get_hosting = esc_url( 'https://wpeng.in/5a0336/' ); + $url_wpe_agency_partners = esc_url( 'https://wpeng.in/fa14e4/' ); $url_wpe_customer_upgrade = esc_url( 'https://wpeng.in/407b79/' ); - $url_wpe_logo = esc_url( 'https://wpeng.in/22f22b/' ); - $url_codeable_submit = esc_url( 'https://codeable.io/wp-admin/admin-ajax.php?action=wp_engine_phpcompat' ); + $url_wpe_logo = esc_url( 'https://wpeng.in/22f22b/' ); + $url_codeable_submit = esc_url( 'https://codeable.io/wp-admin/admin-ajax.php?action=wp_engine_phpcompat' ); + + $update_url = site_url( 'wp-admin/update-core.php', 'admin' ); ?>
-

+


-

+

@@ -365,7 +398,8 @@ function settings_page() { $version ) { printf( '
', $version, checked( $test_version, $version, false ), $name ); - } ?> + } + ?> @@ -394,16 +428,21 @@ function settings_page() {
-
- - -
- - -
-
- -

- -

- - -

- - - - - -

- - -
-
- - - - -
- + +
+ + +
+
+ +

+ +

+ + +

+ + + + + +

+ + +
+
+ + + +
+
- -