diff --git a/.distignore b/.distignore index 5ce9b23..588841c 100644 --- a/.distignore +++ b/.distignore @@ -8,6 +8,7 @@ node_modules package.json package-lock.json phpdoc.xml +phpstan.neon phpunit.xml.dist phpcs.ruleset.xml composer.lock diff --git a/.github/workflows/wordpress.yml b/.github/workflows/wordpress.yml index a04dd95..1ace947 100644 --- a/.github/workflows/wordpress.yml +++ b/.github/workflows/wordpress.yml @@ -52,12 +52,49 @@ jobs: - name: Install WordPress run: bash bin/install-wp-tests.sh wordpress root root 127.0.0.1:3306 ${{ matrix.wp }} - #- name: Check PHP syntax - # run: composer lint - - name: Run Unit test run: composer test + lint: + name: PHP Lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - name: Setup PHP with composer v2 + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + tools: composer + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Check PHP syntax + run: composer lint + + analyze: + name: Check PHP code quality + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - name: Setup PHP with composer v2 + uses: shivammathur/setup-php@v2 + with: + php-version: 7.4 + tools: composer + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + + - name: Run PHP Stan + run: composer analyze:github + assets: name: Assets Test runs-on: ubuntu-latest @@ -77,7 +114,7 @@ jobs: status-check: name: Status Check - needs: [ test, assets ] + needs: [ test, assets, lint, analyze ] runs-on: ubuntu-latest steps: - name: Display Status diff --git a/README.md b/README.md index d56ce72..306195e 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ Contributors: Takahashi_Fumiki, hametuha Tags: facebook,twitter,google,account,oauth,community,social,sns -Tested up to: 6.2 -Stable Tag: 5.0.0 -Requires at least: 5.6 +Tested up to: 6.6 +Stable Tag: 5.2.0 +Requires at least: 5.9 Requires PHP: 7.2 -License: GPL 2.0 or later +License: GPL2 or Later License URI: https://www.gnu.org/licenses/gpl-2.0.html Connect user accounts with significant web services like Facebook, Twitter, etc. Stand on the shoulders of giants! @@ -96,4 +96,4 @@ Here is a list of change logs. ---- -Please refer [changelog.md](https://github.com/fumikito/Gianism/blob/master/changelog.md) for older change logs, \ No newline at end of file +Please refer [changelog.md](https://github.com/fumikito/Gianism/blob/master/changelog.md) for older change logs, diff --git a/app/Gianism/Api/Ajax.php b/app/Gianism/Api/Ajax.php index 55b54e1..f679395 100644 --- a/app/Gianism/Api/Ajax.php +++ b/app/Gianism/Api/Ajax.php @@ -2,7 +2,6 @@ namespace Gianism\Api; -use Gianism\Pattern\Singleton; use Gianism\Plugins\AnalyticsFetcher; /** @@ -40,6 +39,7 @@ abstract protected function get_result(); * @param array $argument */ protected function __construct( array $argument = array() ) { + parent::__construct( $argument ); add_action( 'wp_ajax_' . static::ACTION, array( $this, 'ajax' ) ); if ( ! static::ONLY_MEMBER ) { add_action( 'wp_ajax_nopriv_' . static::ACTION, array( $this, 'ajax' ) ); @@ -65,12 +65,12 @@ protected function parse_result( array $result ) { */ public function ajax() { try { - if ( static::NONCE_ACTION && ! wp_verify_nonce( $this->get( '_wpnonce' ), static::NONCE_ACTION ) ) { - throw new \Exception( $this->_( 'You have no permission.' ), 403 ); + if ( static::NONCE_ACTION && ! wp_verify_nonce( filter_input( INPUT_GET, '_wpnonce' ), static::NONCE_ACTION ) ) { + throw new \Exception( __( 'You have no permission.', 'wp-gianism' ), 403 ); } $result = $this->get_result(); if ( ! is_array( $result ) ) { - throw new \Exception( $this->_( 'Wrong value is returned.' ), 500 ); + throw new \Exception( __( 'Wrong value is returned.', 'wp-gianism' ), 500 ); } $result = $this->parse_result( $result ); nocache_headers(); diff --git a/app/Gianism/Api/Ga.php b/app/Gianism/Api/Ga.php index 098c05f..9386cb4 100644 --- a/app/Gianism/Api/Ga.php +++ b/app/Gianism/Api/Ga.php @@ -52,7 +52,7 @@ protected function start_date() { if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $this->input->get( 'from' ) ) ) { return $this->input->get( 'from' ); } else { - return date_i18n( 'Y-m-d', strtotime( '1 month ago', current_time( 'timestamp' ) ) ); + return date_i18n( 'Y-m-d', strtotime( '1 month ago', time() ) ); } } diff --git a/app/Gianism/Api/ShortCodes.php b/app/Gianism/Api/ShortCodes.php index 496ea0a..532a1fe 100644 --- a/app/Gianism/Api/ShortCodes.php +++ b/app/Gianism/Api/ShortCodes.php @@ -2,7 +2,7 @@ namespace Gianism\Api; -use \Gianism\Pattern\Application; +use Gianism\Pattern\Application; /** * Short code API @@ -17,6 +17,7 @@ class ShortCodes extends Application { * @param array $argument */ public function __construct( array $argument = [] ) { + parent::__construct( $argument ); add_shortcode( 'gianism_login', [ $this, 'login_short_code' ] ); add_shortcode( 'gianism_connection', [ $this, 'profile_connection_short_code' ] ); } diff --git a/app/Gianism/Bootstrap.php b/app/Gianism/Bootstrap.php index 36e77ef..fb90d65 100644 --- a/app/Gianism/Bootstrap.php +++ b/app/Gianism/Bootstrap.php @@ -49,6 +49,7 @@ public static function init() { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); // Register assets add_action( 'init', array( $this, 'register_assets' ) ); // Admin page diff --git a/app/Gianism/Commands/CommandSkeleton.php b/app/Gianism/Commands/CommandSkeleton.php index b78ef6c..6404bf0 100644 --- a/app/Gianism/Commands/CommandSkeleton.php +++ b/app/Gianism/Commands/CommandSkeleton.php @@ -13,5 +13,4 @@ class CommandSkeleton extends \WP_CLI_Command { use AppBase; - } diff --git a/app/Gianism/Commands/TestCommand.php b/app/Gianism/Commands/TestCommand.php index a747d13..df66199 100644 --- a/app/Gianism/Commands/TestCommand.php +++ b/app/Gianism/Commands/TestCommand.php @@ -40,6 +40,7 @@ public function tweet( $args, $assoc ) { \WP_CLI::error( sprintf( '%s: %s', $tweet->get_error_code(), $tweet->get_error_message() ) ); } print_r( $tweet ); + // translators: %s is screen name. \WP_CLI::success( sprintf( __( 'Tweet has been sent as %s. Response message is above.', 'wp-gianism' ), $twitter->tw_screen_name ) ); } @@ -102,6 +103,7 @@ public function mentions( $args, $assoc ) { \WP_CLI::line( '' ); } } + // translators: %d is number of response. \WP_CLI::success( sprintf( _x( 'Got %d response.', 'CLI', 'wp-gianism' ), count( $response ) ) ); } @@ -121,6 +123,7 @@ public function analytics( $args, $assoc ) { $from = isset( $assoc['from'] ) ? $assoc['from'] : date_i18n( 'Y-m-d', strtotime( '7 days ago' ) ); $to = isset( $assoc['to'] ) ? $assoc['to'] : date_i18n( 'Y-m-d', strtotime( 'Yesterday' ) ); try { + // translators: %1$s and %2$s are date string. \WP_CLI::line( sprintf( __( 'Get popular pages from %1$s to %2$s.', 'wp-gianism' ), $from, $to ) ); $table = new Table(); $table->setHeaders( [ 'Page Path', 'PV' ] ); @@ -214,6 +217,7 @@ public function fb_instant_articles( $args, $assoc ) { $next_page = $edge->getCursor( 'after' ); $line = __( 'Successfully retrieved instant articles!', 'wp-gianism' ); if ( $next_page ) { + // translators: %s is next page id. \WP_CLI::success( $line . ' ' . sprintf( __( 'If you need more instant articles, set --after=%s', 'wp-gianism' ), $next_page ) ); } else { \WP_CLI::success( $line ); diff --git a/app/Gianism/Controller/Admin.php b/app/Gianism/Controller/Admin.php index d18c513..98669cc 100644 --- a/app/Gianism/Controller/Admin.php +++ b/app/Gianism/Controller/Admin.php @@ -35,6 +35,7 @@ class Admin extends AbstractController { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); if ( $this->option->is_network_activated() && ! is_main_site() ) { return; } @@ -108,7 +109,7 @@ public function invalid_option_notices() { return; } array_unshift( $this->invalid_options, '[Gianism]' ); - printf( '

%s

', implode( '
', $this->invalid_options ) ); + printf( '

%s

', wp_kses_post( implode( '
', $this->invalid_options ) ) ); } /** @@ -148,7 +149,7 @@ public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $stat if ( preg_match( '#href="https://gianism.info"#', $value ) ) { $plugin_meta[ $index ] = preg_replace_callback( '#href="https://gianism.info"#', - function( $matches ) { + function ( $matches ) { return sprintf( 'href="%s"', esc_url( diff --git a/app/Gianism/Controller/Login.php b/app/Gianism/Controller/Login.php index 8af9abb..a696b48 100644 --- a/app/Gianism/Controller/Login.php +++ b/app/Gianism/Controller/Login.php @@ -40,6 +40,7 @@ class Login extends AbstractController { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); if ( $this->option->is_enabled() ) { // Only for account holder @@ -86,14 +87,17 @@ public function login_form( $before = '', $after = '', $register = false, $redir $class_name[] = 'public-style'; break; } - $class_name = empty( $class_name ) ? '' : sprintf( ' class="%s"', implode( ' ', $class_name ) ); + $class_name = $class_name ? '' : sprintf( ' class="%s"', esc_attr( implode( ' ', $class_name ) ) ); $before = sprintf( '
', $class_name ); } if ( empty( $after ) ) { $after = '
'; } - if ( '' === $redirect_to && ( $redirect_query = $this->input->get( 'redirect_to' ) ) ) { - $redirect_to = $redirect_query; + if ( '' === $redirect_to ) { + $redirect_query = $this->input->get( 'redirect_to' ); + if ( $redirect_query ) { + $redirect_to = $redirect_query; + } } echo $before; /** @@ -151,5 +155,4 @@ public function woo_form() { } $this->login_form( '', '', false, $redirect, 'woo-account' ); } - } diff --git a/app/Gianism/Controller/Network.php b/app/Gianism/Controller/Network.php index 525470a..fc3cd90 100644 --- a/app/Gianism/Controller/Network.php +++ b/app/Gianism/Controller/Network.php @@ -21,6 +21,7 @@ class Network extends AbstractController { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); // Add network notice add_action( 'admin_notices', [ $this, 'network_notice' ] ); // Set role. diff --git a/app/Gianism/Controller/Profile.php b/app/Gianism/Controller/Profile.php index d786b3e..27b8cc4 100644 --- a/app/Gianism/Controller/Profile.php +++ b/app/Gianism/Controller/Profile.php @@ -19,6 +19,7 @@ class Profile extends AbstractController { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); // If not enabled, skip. if ( ! $this->option->is_enabled() ) { return; diff --git a/app/Gianism/Controller/ProfileChecker.php b/app/Gianism/Controller/ProfileChecker.php index 018a491..e92b661 100644 --- a/app/Gianism/Controller/ProfileChecker.php +++ b/app/Gianism/Controller/ProfileChecker.php @@ -12,16 +12,17 @@ class ProfileChecker extends AbstractController { public function __construct( array $argument = [] ) { + parent::__construct( $argument ); add_action( 'rest_api_init', [ $this, 'register_rest' ] ); add_action( 'init', - function() { + function () { if ( ! is_user_logged_in() ) { return; } add_action( 'template_redirect', - function() { + function () { if ( $this->should_redirect() ) { $this->redirect(); } elseif ( $this->should_show_popup() ) { @@ -69,8 +70,11 @@ public function should_redirect() { */ public function default_url() { $url = get_edit_profile_url(); - if ( gianism_woocommerce_detected() && ( $page = wc_get_page_permalink( 'myaccount' ) ) ) { - $url = get_permalink( $page ); + if ( gianism_woocommerce_detected() ) { + $page = wc_get_page_permalink( 'myaccount' ); + if ( $page ) { + $url = get_permalink( $page ); + } } return $url; } @@ -122,10 +126,10 @@ public function register_rest() { [ 'methods' => 'GET', 'args' => [], - 'permission_callback' => function() { + 'permission_callback' => function () { return is_user_logged_in(); }, - 'callback' => function( \WP_REST_Request $request ) { + 'callback' => function ( \WP_REST_Request $request ) { $error = $this->get_error( get_current_user_id() ); $response = [ 'errors' => $error->get_error_messages(), @@ -192,6 +196,7 @@ public function show_popup() { if ( ! $error->get_error_messages() ) { return; } + // translators: %s is URL $message = sprintf( __( 'You have an incomplete profile. To access full features of this site, please fill your profile here.', 'wp-gianism' ), $this->redirect_url() ); $message = apply_filters( 'gianism_profile_error_popup', $message, $this->redirect_url(), $error ); $this->add_message( $message, true ); @@ -211,7 +216,7 @@ public function is_excluded_paths( $path, $excluded = null ) { } $patterns = array_filter( array_map( - function( $line ) { + function ( $line ) { return trim( $line ); }, preg_split( '#[\r\n]#u', $excluded ) diff --git a/app/Gianism/Controller/Rewrite.php b/app/Gianism/Controller/Rewrite.php index 505da3a..2bcedb6 100644 --- a/app/Gianism/Controller/Rewrite.php +++ b/app/Gianism/Controller/Rewrite.php @@ -40,6 +40,7 @@ class Rewrite extends AbstractController { * @param array $argument */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); // Add query vars add_filter( 'query_vars', [ $this, 'filter_vars' ] ); // Instance all instances @@ -73,7 +74,7 @@ protected function __construct( array $argument = [] ) { */ $this->rewrites = apply_filters( 'gianism_rewrite_rules', $rewrites ); // Hook for rewrite rules - add_action( 'rewrite_rules_array', [ $this, 'rewrite_rules_array' ] ); + add_filter( 'rewrite_rules_array', [ $this, 'rewrite_rules_array' ] ); // Check if rewrite rules are satisfied add_action( 'admin_init', [ $this, 'check_rewrite' ] ); // WP_Query @@ -133,7 +134,7 @@ public function rewrite_rules_array( array $rules ) { public function check_rewrite() { $registered_rewrites = $this->option->get( 'rewrite_rules' ); foreach ( $this->rewrites as $reg => $replaced ) { - if ( ! isset( $registered_rewrites[ $reg ] ) || $replaced != $registered_rewrites[ $reg ] ) { + if ( ! isset( $registered_rewrites[ $reg ] ) || $replaced !== $registered_rewrites[ $reg ] ) { flush_rewrite_rules(); } } @@ -145,30 +146,24 @@ public function check_rewrite() { * @param \WP_Query $wp_query */ public function hijack_query( \WP_Query &$wp_query ) { - if ( ! is_admin() && $wp_query->is_main_query() - && ( $service = $wp_query->get( 'gianism_service' ) ) - && ( $action = $wp_query->get( 'gianism_action' ) ) - ) { + $service = $wp_query->get( 'gianism_service' ); + $action = $wp_query->get( 'gianism_action' ); + if ( ! is_admin() && $wp_query->is_main_query() && $service && $action ) { /** * Convert rewrite rule to service name * * @since 3.0.0 - * @filter gianism_filter_service_prefix - * @param $prefix - * @return string + * @param string $service */ $filtered_service = apply_filters( 'gianism_filter_service_prefix', $service ); - if ( false !== array_search( $service, $this->prefixes ) && ( $instance = $this->service->get( $filtered_service ) ) ) { + if ( in_array( $service, $this->prefixes, true ) && ( $this->service->get( $filtered_service ) ) ) { nocache_headers(); /** @var AbstractService $instance */ // Parse Request - $instance->parse_request( $action, $wp_query ); + $this->service->get( $filtered_service )->parse_request( $action, $wp_query ); } else { $wp_query->set_404(); } } } - - - } diff --git a/app/Gianism/Cron/Daily.php b/app/Gianism/Cron/Daily.php index 5c3da53..a3be77e 100644 --- a/app/Gianism/Cron/Daily.php +++ b/app/Gianism/Cron/Daily.php @@ -49,6 +49,7 @@ abstract class Daily extends AnalyticsFetcher { * @param array $argument */ protected function __construct( array $argument = array() ) { + parent::__construct( $argument ); if ( ! wp_next_scheduled( $this->get_action() ) ) { wp_schedule_event( $this->build_timestamp(), static::INTERVAL, $this->get_action() ); } diff --git a/app/Gianism/Helper/Input.php b/app/Gianism/Helper/Input.php index eaeda4b..bbdb9b9 100644 --- a/app/Gianism/Helper/Input.php +++ b/app/Gianism/Helper/Input.php @@ -11,15 +11,6 @@ */ class Input extends Singleton { - /** - * Constructor - * - * @param array $argument - */ - public function __construct( array $argument = array() ) { - // Do nothing because it's empty singleton - } - /** * Short hand for $_GET * @@ -94,19 +85,19 @@ public function nonce_action( $action ) { } /** - * Short hand for wp_die + * Shorthand for wp_die * * @param string $message - * @param int $status_code - * @param bool $return + * @param int $status_code + * @param bool $return_link */ - public function wp_die( $message, $status_code = 500, $return = true ) { + public function wp_die( $message, $status_code = 500, $return_link = true ) { wp_die( $message, get_status_header_desc( $status_code ) . ' | ' . get_bloginfo( 'name' ), [ 'response' => (int) $status_code, - 'back_link' => (bool) $return, + 'back_link' => (bool) $return_link, ] ); } diff --git a/app/Gianism/Helper/MessageHelper.php b/app/Gianism/Helper/MessageHelper.php index ca41dfe..454a025 100644 --- a/app/Gianism/Helper/MessageHelper.php +++ b/app/Gianism/Helper/MessageHelper.php @@ -13,17 +13,17 @@ trait MessageHelper { /** * Add message to show * - * @param string $string - * @param bool $error + * @param string $text + * @param bool $error * @return bool */ - protected function add_message( $string, $error = false ) { + protected function add_message( $text, $error = false ) { $key = 'gianism_' . ( $error ? 'error' : 'updated' ); if ( isset( $_COOKIE[ $key ] ) && ! empty( $_COOKIE[ $key ] ) ) { $messages = json_decode( stripcslashes( $_COOKIE[ $key ] ), true ); - $messages[] = $string; + $messages[] = $text; } else { - $messages = array( $string ); + $messages = array( $text ); } return gianism_set_cookie( $key, rawurlencode( json_encode( $messages ) ), time() + 180, '', false ); } diff --git a/app/Gianism/Helper/Monitor.php b/app/Gianism/Helper/Monitor.php index 9336c4b..6742113 100644 --- a/app/Gianism/Helper/Monitor.php +++ b/app/Gianism/Helper/Monitor.php @@ -5,6 +5,11 @@ use Gianism\Pattern\Singleton; +/** + * Class for monitor social network + * + * @todo Implement this class + */ class Monitor extends Singleton { @@ -21,8 +26,7 @@ class Monitor extends Singleton { * @param array $argument */ protected function __construct( array $argument = array() ) { + parent::__construct( $argument ); add_filter( 'cron_schedules', array( $this, 'cron_schedules' ) ); } - - } diff --git a/app/Gianism/Helper/Option.php b/app/Gianism/Helper/Option.php index 87af794..eaabecb 100644 --- a/app/Gianism/Helper/Option.php +++ b/app/Gianism/Helper/Option.php @@ -25,7 +25,8 @@ */ class Option extends Singleton { - use i18n, MessageHelper; + use i18n; + use MessageHelper; /** * Action name which fires on updating option @@ -76,6 +77,7 @@ class Option extends Singleton { * @param array $argument Settings array. */ protected function __construct( array $argument = [] ) { + parent::__construct( $argument ); $this->values = $this->get( $this->key, [] ); foreach ( $this->default_option as $key => $value ) { if ( ! isset( $this->values[ $key ] ) ) { @@ -88,11 +90,11 @@ protected function __construct( array $argument = [] ) { * Set default option * * @param string $key - * @param mixed $default + * @param mixed $default_value */ - public function set_default( $key, $default ) { + public function set_default( $key, $default_value ) { if ( ! isset( $this->values[ $key ] ) ) { - $this->values[ $key ] = $default; + $this->values[ $key ] = $default_value; } } @@ -284,13 +286,10 @@ public function button_types() { public function has_invalid_option( $name ) { switch ( $name ) { case 'google_redirect': - $option = $this->get( $this->key, [] ); - + $saved_option = $this->get( $this->key, [] ); return isset( $saved_option['ggl_redirect_uri'] ) && ! empty( $saved_option['ggl_redirect_uri'] ); - break; default: return false; - break; } } @@ -360,7 +359,7 @@ public function is_network_activated() { if ( ! is_multisite() ) { return false; } - return in_array( gianism_root_dir() . '/wp-gianism.php', wp_get_active_network_plugins() ); + return in_array( gianism_root_dir() . '/wp-gianism.php', wp_get_active_network_plugins(), true ); } /** diff --git a/app/Gianism/Helper/ServiceManager.php b/app/Gianism/Helper/ServiceManager.php index f65707d..7f288a2 100644 --- a/app/Gianism/Helper/ServiceManager.php +++ b/app/Gianism/Helper/ServiceManager.php @@ -12,7 +12,8 @@ */ class ServiceManager extends Singleton { - use i18n, ExtensionManager; + use i18n; + use ExtensionManager; /** * @var array @@ -41,6 +42,7 @@ class ServiceManager extends Singleton { */ final protected function __construct( array $argument ) { // Do nothing + parent::__construct( $argument ); } /** @@ -165,5 +167,4 @@ public function get_plugins() { } return $plugins; } - } diff --git a/app/Gianism/Helper/Session.php b/app/Gianism/Helper/Session.php index c396855..64952b8 100644 --- a/app/Gianism/Helper/Session.php +++ b/app/Gianism/Helper/Session.php @@ -39,6 +39,7 @@ public function start() { * @return bool */ public function is_available() { + // @phpstan-ignore isset.variable return isset( $_COOKIE ); } @@ -146,6 +147,4 @@ public function __get( $name ) { break; } } - - } diff --git a/app/Gianism/Pattern/AbstractController.php b/app/Gianism/Pattern/AbstractController.php index 16c6fc7..3accad6 100644 --- a/app/Gianism/Pattern/AbstractController.php +++ b/app/Gianism/Pattern/AbstractController.php @@ -10,5 +10,4 @@ abstract class AbstractController extends Singleton { use AppBase; - } diff --git a/app/Gianism/Pattern/AbstractNotice.php b/app/Gianism/Pattern/AbstractNotice.php index 78ffec9..dcb05bf 100644 --- a/app/Gianism/Pattern/AbstractNotice.php +++ b/app/Gianism/Pattern/AbstractNotice.php @@ -140,8 +140,8 @@ public function invalid_option_notices() { ?>

- - message(); ?> + + message() ); ?>

get_var( $wpdb->prepare( $sql, $key, $value ) ); } - } diff --git a/app/Gianism/Pattern/Singleton.php b/app/Gianism/Pattern/Singleton.php index 71b6486..34022f1 100644 --- a/app/Gianism/Pattern/Singleton.php +++ b/app/Gianism/Pattern/Singleton.php @@ -22,9 +22,10 @@ abstract class Singleton { * Constructor * * @param array $argument + * @phpstan-ignore constructor.unusedParameter */ protected function __construct( array $argument = [] ) { - // Do nothing. + // Override something. } /** @@ -42,5 +43,4 @@ final public static function get_instance( array $argument = [] ) { return self::$instances[ $class_name ]; } - } diff --git a/app/Gianism/Plugins/Analytics.php b/app/Gianism/Plugins/Analytics.php index 4cc19fd..857789a 100644 --- a/app/Gianism/Plugins/Analytics.php +++ b/app/Gianism/Plugins/Analytics.php @@ -95,6 +95,7 @@ public function __construct( array $argument = [] ) { // Do nothing if Google is not enabled. return; } + parent::__construct( $argument ); // Add request handler add_action( 'gianism_extra_action', [ $this, 'handle_default' ], 10, 3 ); // Register Ajax @@ -104,8 +105,8 @@ public function __construct( array $argument = [] ) { add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); add_filter( 'gianism_setting_screen_views', - function( $views, $slug ) { - if ( 'gianism' == $slug ) { + function ( $views, $slug ) { + if ( 'gianism' === $slug ) { $views['analytics'] = sprintf( ' %s', $this->_( 'Google Analytics' ) ); } return $views; @@ -328,7 +329,7 @@ public function table_create_url( $redirect ) { * @param $hook_suffix */ public function enqueue_scripts( $hook_suffix ) { - if ( 'settings_page_gianism' == $hook_suffix ) { + if ( 'settings_page_gianism' === $hook_suffix ) { // Script wp_enqueue_script( 'gianism-analytics-helper', $this->url . 'assets/js/admin-analytics-helper.js', [ 'jquery-form' ], $this->version, true ); wp_localize_script( @@ -355,7 +356,7 @@ public function ga_ajax() { } // Check data to retrieve $result = null; - switch ( $target = $this->input->get( 'target' ) ) { + switch ( $this->input->get( 'target' ) ) { case 'account': $properties = $this->ga ->management_webproperties @@ -376,7 +377,6 @@ public function ga_ajax() { break; default: throw new \Exception( $this->_( 'Invalid action.' ), 500 ); - break; } } catch ( \Exception $e ) { $json = array( @@ -385,7 +385,7 @@ public function ga_ajax() { 'message' => $e->getMessage(), ); } - echo json_encode( $json ); + wp_send_json( $json ); exit; } @@ -419,7 +419,7 @@ public function boot_auto_cron() { $this->crons = apply_filters( 'gianism_analytics_auto_loader_class', [], 'cron' ); // Parse directory $classes = $scan( $template_dir ); - if ( $template_dir != $stylesheet_dir ) { + if ( $template_dir !== $stylesheet_dir ) { $classes = array_merge( $classes, $scan( $stylesheet_dir ) ); } if ( ! empty( $classes ) ) { @@ -574,6 +574,4 @@ public function __get( $name ) { break; } } - - } diff --git a/app/Gianism/Plugins/AnalyticsFetcher.php b/app/Gianism/Plugins/AnalyticsFetcher.php index b8e146b..18faebe 100644 --- a/app/Gianism/Plugins/AnalyticsFetcher.php +++ b/app/Gianism/Plugins/AnalyticsFetcher.php @@ -28,7 +28,7 @@ class AnalyticsFetcher extends Singleton { * @return string */ protected function today() { - return date_i18n( 'Y-m-d', current_time( 'timestamp' ) ); + return date_i18n( 'Y-m-d' ); } /** @@ -55,11 +55,11 @@ protected function save( $date, $id, $value ) { /** * Fetch data from Google Analytics API * - * @param string $start_date Date string - * @param string $end_date Date string - * @param string $metrics CSV of metrics E.g., 'ga:visits,ga:pageviews' - * @param array $params Option params below - * @param bool $throw If set to true, throws exception + * @param string $start_date Date string + * @param string $end_date Date string + * @param string $metrics CSV of metrics E.g., 'ga:visits,ga:pageviews' + * @param array $params Option params below + * @param bool $throw_exception If set to true, throws exception * * @opt_param string dimensions A comma-separated list of Analytics dimensions. E.g., 'ga:browser,ga:city'. * @opt_param string filters A comma-separated list of dimension or metric filters to be applied to Analytics data. @@ -71,7 +71,7 @@ protected function save( $date, $id, $value ) { * @return array * @throws \Exception */ - public function fetch( $start_date, $end_date, $metrics, $params = [], $throw = false ) { + public function fetch( $start_date, $end_date, $metrics, $params = [], $throw_exception = false ) { try { if ( ! $this->ga || ! $this->view_id ) { throw new \Exception( 'Google Analytics is not connected.', 500 ); @@ -83,7 +83,7 @@ public function fetch( $start_date, $end_date, $metrics, $params = [], $throw = return []; } } catch ( \Exception $e ) { - if ( $throw ) { + if ( $throw_exception ) { throw $e; } else { error_log( sprintf( '[Gianism GA Error %s] %s', $e->getCode(), $e->getMessage() ) ); @@ -130,5 +130,4 @@ public function __get( $name ) { break; } } - } diff --git a/app/Gianism/Plugins/Bot.php b/app/Gianism/Plugins/Bot.php index f8e43e5..38a8fb5 100644 --- a/app/Gianism/Plugins/Bot.php +++ b/app/Gianism/Plugins/Bot.php @@ -58,6 +58,7 @@ protected function __construct( array $argument = array() ) { if ( ! $this->plugin_enabled() ) { return; } + parent::__construct( $argument ); // Post type add_action( 'init', array( $this, 'register_post_type' ) ); add_action( "manage_{$this->post_type}_posts_custom_column", array( $this, 'custom_columns' ), 10, 2 ); @@ -114,7 +115,7 @@ public function register_post_type() { * @return string */ public function enter_title_here( $title, $post ) { - if ( $post->post_type == $this->post_type ) { + if ( $post->post_type === $this->post_type ) { $title = $this->_( 'Enter this bots name. Ex: Promotion Campaign 2014 mid' ); } @@ -128,7 +129,7 @@ public function enter_title_here( $title, $post ) { * @param \WP_Post $post */ public function save_post( $post_id, $post ) { - if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) || $this->post_type != $post->post_type ) { + if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) || $this->post_type !== $post->post_type ) { return; } if ( ! wp_verify_nonce( $this->input->post( '_gianismnonce' ), 'gianism_twitter_bot' ) ) { @@ -159,9 +160,9 @@ public function save_post( $post_id, $post ) { * @param string $suffix */ public function admin_enqueue_scripts( $suffix ) { - if ( false !== array_search( $suffix, array( 'post.php', 'post-new.php' ) ) ) { + if ( in_array( $suffix, array( 'post.php', 'post-new.php' ), true ) ) { $screen = get_current_screen(); - if ( $this->post_type == $screen->post_type ) { + if ( $this->post_type === $screen->post_type ) { wp_enqueue_script( 'gianism-twitter-bot-helper', $this->url . 'assets/js/admin-twitter-bot-helper.js', array( 'jquery-effects-highlight' ), $this->version, true ); wp_localize_script( 'gianism-twitter-bot-helper', @@ -181,7 +182,7 @@ public function admin_enqueue_scripts( $suffix ) { * @param \WP_Post $post */ public function edit_form_after_title( $post ) { - if ( $this->post_type == $post->post_type ) { + if ( $this->post_type === $post->post_type ) { wp_nonce_field( 'gianism_twitter_bot', '_gianismnonce', false ); include $this->dir . '/templates/edit/bot.php'; } @@ -204,7 +205,7 @@ public function get_schedule( $post ) { SQL; $results = $this->db->get_results( $this->db->prepare( $query, $post->ID, $this->time_key . '_%' ) ); $times = array(); - for ( $i = 1; $i <= 7; $i ++ ) { + for ( $i = 1; $i <= 7; $i++ ) { $times[ $this->time_key . '_' . $i ] = array(); } foreach ( $results as $result ) { @@ -297,7 +298,7 @@ public function register_cron() { function ( $matches ) { return $matches[1] . '0:00'; }, - date_i18n( 'Y-m-d H:i:00', current_time( 'timestamp' ) + 60 * 10 ) + date_i18n( 'Y-m-d H:i:00', time() + 60 * 10 ) ), 'U' ); @@ -335,7 +336,7 @@ public function execute_cron() { } $meta_key = $this->time_key . '_' . $date; // Get posts - $now = current_time( 'timestamp' ); + $now = time(); $before = $now - 60 * 9; $now = date_i18n( 'H:i:00', $now ); $before = date_i18n( 'H:i:00', $before ); @@ -388,7 +389,7 @@ public function get_columns( $columns ) { $new_columns = array(); foreach ( $columns as $key => $column ) { $new_columns[ $key ] = $column; - if ( 'date' == $key ) { + if ( 'date' === $key ) { $new_columns['end_date'] = $this->_( 'End Date' ); } } @@ -407,7 +408,7 @@ public function custom_columns( $column_name, $post_id ) { case 'end_date': $limit = $this->cron_limit( $post_id ); if ( preg_match( '/[0-9]{4}-[0-9]{2}-[0-9]{2}/u', $limit ) ) { - echo mysql2date( $this->option->get( 'date_format' ), $limit . ' 00:00:00' ); + echo esc_html( mysql2date( $this->option->get( 'date_format' ), $limit . ' 00:00:00' ) ); } else { echo '---'; } @@ -456,7 +457,7 @@ function ( $args, $content = '' ) use ( $gianism ) { ), $args ); - $left = strtotime( $args['limit'] ) - current_time( 'timestamp' ); + $left = strtotime( $args['limit'] ) - time(); if ( $left / ( 60 * 60 * 24 * 30 ) > 1 ) { return sprintf( $args['placeholder'], sprintf( $gianism->_( '%s months' ), floor( $left / ( 60 * 60 * 24 * 30 ) ) ) ); } elseif ( $left / ( 60 * 60 * 24 ) > 1 ) { diff --git a/app/Gianism/Plugins/PluginBase.php b/app/Gianism/Plugins/PluginBase.php index e61ce94..7d5c221 100644 --- a/app/Gianism/Plugins/PluginBase.php +++ b/app/Gianism/Plugins/PluginBase.php @@ -49,6 +49,4 @@ public function __get( $name ) { break; } } - - } diff --git a/app/Gianism/Service/AbstractService.php b/app/Gianism/Service/AbstractService.php index c8cdfe0..1872c0a 100644 --- a/app/Gianism/Service/AbstractService.php +++ b/app/Gianism/Service/AbstractService.php @@ -17,6 +17,7 @@ * @property-read string $service_name * @property-read bool $enabled */ +#[\AllowDynamicProperties] abstract class AbstractService extends Application { /** @@ -58,6 +59,7 @@ abstract class AbstractService extends Application { * @param array $argument */ protected function __construct( array $argument = array() ) { + parent::__construct( $argument ); // Setup name if ( empty( $this->verbose_service_name ) ) { $this->verbose_service_name = $this->service_name; @@ -192,16 +194,17 @@ public function parse_request( $action, \WP_Query &$wp_query ) { $wp_query->set_404(); return; } - if ( 'default' != $action && ! $this->input->verify_nonce( $this->input->nonce_action( "{$this->service_name}_{$action}" ) ) ) { + if ( 'default' !== $action && ! $this->input->verify_nonce( $this->input->nonce_action( "{$this->service_name}_{$action}" ) ) ) { // If not default, nonce required. $this->input->wp_die( __( 'This request seems to be a wrong access. Please try again.', 'wp-gianism' ), 403 ); } - if ( 'default' != $action && method_exists( get_called_class(), $method ) ) { + if ( 'default' !== $action && method_exists( get_called_class(), $method ) ) { // Method found, just call $this->{$method}( $wp_query ); } else { // Else, call default. - if ( $specified_action = $this->session->get( 'action' ) ) { + $specified_action = $this->session->get( 'action' ); + if ( $specified_action ) { // If session is set, override with it. $action = $specified_action; } @@ -233,6 +236,7 @@ protected function handle_connect( \WP_Query $wp_query ) { } // Is user connected already? if ( $this->is_connected( get_current_user_id() ) ) { + // translators: %s is service name. throw new \Exception( sprintf( __( 'You are already connected with %s', 'wp-gianism' ), $this->verbose_service_name ) ); } // Set redirect URL @@ -273,10 +277,12 @@ protected function handle_disconnect( \WP_Query $wp_query ) { } // Has connection? if ( ! $this->is_connected( get_current_user_id() ) ) { + // translators: %s is service name. throw new \Exception( sprintf( __( 'Your account is not connected with %s', 'wp-gianism' ), $this->verbose_service_name ) ); } // O.K. $this->disconnect( get_current_user_id() ); + // translators: %s is service name. $this->add_message( sprintf( __( 'Your account is now unlinked from %s.', 'wp-gianism' ), $this->verbose_service_name ) ); // Redirect wp_redirect( $this->filter_redirect( $redirect_url, 'disconnect' ) ); @@ -307,7 +313,8 @@ public function handle_login( \WP_Query $wp_query ) { 'redirect_to' => $this->input->get( 'redirect_to' ), 'action' => 'login', ]; - if ( ( $blog_id = $this->input->get( 'blog_id' ) ) ) { + $blog_id = $this->input->get( 'blog_id' ); + if ( $blog_id ) { // Store blog id if blog id is specified. $session['blog_id'] = (int) $blog_id; } @@ -376,9 +383,11 @@ public function profile_connect( \WP_User $user ) { public function connection_message( $context = 'connected' ) { switch ( $context ) { case 'connected': + // translators: %s is service name. return sprintf( __( 'Your account is already connected with %s account.', 'wp-gianism' ), $this->verbose_service_name ); break; default: // Disconnected + // translators: %1$s is service name, %2$s is site name. return sprintf( __( 'Connecting with %1$s, you can login with %2$s via %1$s without password or email address.', 'wp-gianism' ), $this->verbose_service_name, get_bloginfo( 'name' ) ); break; } @@ -400,32 +409,21 @@ public function login_form( $is_register = false, $redirect_to = '', $context = /** * Returns redirect to url if set. * - * @param string $default + * @param string $default_url * @param array $args * * @return string */ - protected function get_redirect_to( $default, $args = array() ) { + protected function get_redirect_to( $default_url, $args = array() ) { + $redirect_to = $default_url; if ( isset( $_REQUEST['redirect_to'] ) ) { $domain = $_SERVER['SERVER_NAME']; if ( preg_match( "/^(https?:\/\/{$domain}|\/)/", $_REQUEST['redirect_to'] ) ) { $redirect_to = $_REQUEST['redirect_to']; if ( ! empty( $args ) ) { - $redirect_to .= ( false !== strpos( $redirect_to, '?' ) ) ? '&' : '?'; - $counter = 0; - foreach ( $args as $key => $val ) { - if ( 0 == $counter ) { - $redirect_to .= '&'; - } - $redirect_to .= $key . '=' . rawurlencode( $val ); - $counter ++; - } + $redirect_to = add_query_arg( $args, $redirect_to ); } - } else { - $redirect_to = $default; } - } else { - $redirect_to = $default; } return $this->filter_redirect( $redirect_to, 'default' ); } @@ -499,8 +497,9 @@ protected function filter_redirect( $url, $context ) { * @filter gianism_redirect_to * @param string $url The URL user will be redirect to. * @param string $service 'facebook', 'twitter', and so on. + * @param string $context The context of this redirect. */ - return apply_filters( 'gianism_redirect_to', $url, $this->service, $context ); + return apply_filters( 'gianism_redirect_to', $url, $this->service_name, $context ); } /** @@ -565,9 +564,12 @@ public function button( $text, $href, $icon_name = true, array $class_names = [ $icon = ''; } // If SVG exists, use it for public button style. - if ( in_array( 'wpg-guideline-button', $class_names ) && ( $file = $this->svg_path() ) ) { - $icon = $this->url . '/assets/img/brands/' . $file; - $icon = sprintf( '', esc_url( $icon ) ); + if ( in_array( 'wpg-guideline-button', $class_names, true ) ) { + $file = $this->svg_path(); + if ( $file ) { + $icon = $this->url . '/assets/img/brands/' . $file; + $icon = sprintf( '', esc_url( $icon ) ); + } } $class_attr = implode( ' ', @@ -619,6 +621,7 @@ function ( $attr ) { * @return string */ protected function login_label( $register = false, $context = '' ) { + // translators: %s is service name. return sprintf( __( 'Log in with %s', 'wp-gianism' ), $this->verbose_service_name ); } @@ -709,6 +712,7 @@ public function connect_button( $redirect_to = '' ) { $args = array( 'gianism-ga-category' => "gianism/{$this->service_name}", 'gianism-ga-action' => 'connect', + // translators: %s is service name. 'gianism-ga-label' => sprintf( __( 'Connect %s', 'wp-gianism' ), $this->verbose_service_name ), ); @@ -736,7 +740,9 @@ public function disconnect_button( $redirect_to = '' ) { $args = array( 'gianism-ga-category' => "gianism/{$this->service_name}", 'gianism-ga-action' => 'disconnect', + // translators: %s is service name. 'gianism-ga-label' => sprintf( __( 'Disconnect %s', 'wp-gianism' ), $this->verbose_service_name ), + // translators: %s is service name. 'gianism-confirm' => sprintf( __( 'You really disconnect from %s? If so, please be sure about your credential(email, password), or else you might not be able to login again.', 'wp-gianism' ), $this->verbose_service_name ), ); @@ -846,6 +852,7 @@ protected function valid_username_from_mail( $email ) { * @return string */ protected function api_error_string() { + // translators: %s is service name. return sprintf( __( '%s API returns error.', 'wp-gianism' ), $this->verbose_service_name ); } @@ -855,6 +862,7 @@ protected function api_error_string() { * @return string */ protected function duplicate_account_string() { + // translators: %s is service name. return sprintf( __( 'This %s account is already connected with others.', 'wp-gianism' ), $this->verbose_service_name ); } @@ -864,6 +872,7 @@ protected function duplicate_account_string() { * @param string $who */ protected function welcome( $who ) { + // translators: %s is user name. $this->add_message( sprintf( __( 'Welcome, %s!', 'wp-gianism' ), $who ) ); } @@ -910,6 +919,7 @@ protected function kill_wrong_access() { */ protected function test_user_can_register() { if ( ! $this->user_can_register() ) { + // translators: %s is service name. throw new \Exception( sprintf( __( 'Registration via %s is not allowed.', 'wp-gianism' ), $this->verbose_service_name ) ); } @@ -959,6 +969,7 @@ protected function get_response( $endpoint, $request = '', $method = 'POST', $js case 'PUT': case 'PATCH': curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $method ); + // Other, same as POST. case 'POST': curl_setopt( $ch, CURLOPT_POST, true ); if ( is_array( $request ) ) { @@ -970,6 +981,7 @@ protected function get_response( $endpoint, $request = '', $method = 'POST', $js break; case 'DELETE': curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' ); + // Other, same as GET. case 'GET': $args = array(); if ( is_array( $request ) ) { @@ -981,7 +993,6 @@ protected function get_response( $endpoint, $request = '', $method = 'POST', $js break; default: return array(); - break; } curl_setopt( $ch, CURLOPT_URL, $endpoint ); if ( ! empty( $additional_headers ) ) { @@ -991,7 +1002,6 @@ protected function get_response( $endpoint, $request = '', $method = 'POST', $js curl_close( $ch ); return json_decode( $response ); - } /** @@ -1036,6 +1046,7 @@ public function confirmation_message( $context = 'login' ) { */ public function target_credentials( $context = 'login' ) { return [ + // translators: %s is serfice name. 'id' => sprintf( __( '%s User ID', 'wp-gianism' ), $this->verbose_service_name ), 'profile' => __( 'Profile', 'wp-gianism' ), 'email' => __( 'Email', 'wp-gianism' ), @@ -1064,13 +1075,10 @@ public function __get( $name ) { $segments = explode( '\\', get_called_class() ); return strtolower( $segments[ count( $segments ) - 1 ] ); - break; case 'enabled': return $this->option->is_enabled( $this->service_name ); - break; default: return parent::__get( $name ); - break; } } } diff --git a/app/Gianism/Service/Facebook.php b/app/Gianism/Service/Facebook.php index b315a3b..35e2aad 100644 --- a/app/Gianism/Service/Facebook.php +++ b/app/Gianism/Service/Facebook.php @@ -1,7 +1,6 @@ %s', $this->_( 'Facebook API' ) ); } return $views; @@ -224,8 +223,8 @@ public function handle_admin( \WP_Query $wp_query ) { if ( $this->input->request( 'publish' ) ) { add_filter( 'gianism_facebook_permissions', - function( $permission, $action ) { - if ( 'admin' == $action ) { + function ( $permission, $action ) { + if ( 'admin' === $action ) { $permission[] = 'publish_actions'; } return $permission; @@ -256,7 +255,7 @@ function( $permission, $action ) { protected function handle_default( $action ) { global $wpdb; // Get common values - $redirect_url = $this->session->get( 'redirect_to' ); + $redirect_url = (string) $this->session->get( 'redirect_to' ); // Process actions switch ( $action ) { case 'login': // Make user login @@ -267,8 +266,9 @@ protected function handle_default( $action ) { } // Get user ID $user = $this->get_returned_user(); - // If user doesn't exists, try to register. - if ( ! ( $user_id = $this->get_meta_owner( $this->umeta_id, $user['id'] ) ) ) { + // If user doesn't exist, try to register. + $user_id = $this->get_meta_owner( $this->umeta_id, $user['id'] ); + if ( ! $user_id ) { // Test $this->test_user_can_register(); // Check email @@ -287,11 +287,11 @@ protected function handle_default( $action ) { * There might be no available string for login name, so use Facebook id for login. * * @filter gianism_register_name - * @param string $user_login - * @param string $service - * @param mixed $data User data or something. It varies by service. + * @param string $user_login user login name. + * @param string $service Service name. + * @param mixed $user User data or something. It varies by service. */ - $user_name = apply_filters( 'gianism_register_name', 'fb-' . $user['id'], $this->service, $user ); + $user_name = apply_filters( 'gianism_register_name', 'fb-' . $user['id'], $this->service_name, $user ); // Check if username exists $user_id = wp_create_user( $user_name, wp_generate_password(), $email ); if ( is_wp_error( $user_id ) ) { @@ -302,7 +302,7 @@ protected function handle_default( $action ) { $wpdb->users, [ 'display_name' => $user['name'], - // 'user_url' => $user['link'], // Deprecated because of REST API 3 udpdate: https://developers.facebook.com/blog/post/2018/05/01/enhanced-developer-app-review-and-graph-api-3.0-now-live/ + // 'user_url' => $user['link'], // Deprecated because of REST API 3 udpdate: https://developers.facebook.com/blog/post/2018/05/01/enhanced-developer-app-review-and-graph-api-3.0-now-live/ ], [ 'ID' => $user_id, @@ -346,9 +346,10 @@ protected function handle_default( $action ) { if ( ! isset( $user['email'] ) || ! is_email( $user['email'] ) ) { throw new \Exception( $this->mail_fail_string() ); } - $email = $user['email']; + $email = $user['email']; + $email_owner = $this->mail_owner( $email ); // Check if other user has these as meta_value - if ( ( $email_owner = $this->mail_owner( $email ) ) && get_current_user_id() != $email_owner ) { + if ( $email_owner && get_current_user_id() !== $email_owner ) { throw new \Exception( $this->duplicate_account_string() ); } // Now let's save user_data @@ -397,6 +398,7 @@ protected function handle_default( $action ) { $long_token = $oauth->getLongLivedAccessToken( $token ); // O.K. Token ready and save it. update_option( 'gianism_facebook_admin_token', $long_token ); + // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested update_option( 'gianism_facebook_admin_refreshed', current_time( 'timestamp' ) ); $this->add_message( $this->_( 'Access token is saved.' ) ); } catch ( \Exception $e ) { @@ -412,7 +414,7 @@ protected function handle_default( $action ) { * @action gianism_extra_action * @param string $service_name facebook, google, etc. * @param string $action - * @param string $args + * @param array{redirect_to:string} $args */ do_action( 'gianism_extra_action', @@ -425,7 +427,6 @@ protected function handle_default( $action ) { $this->input->wp_die( sprintf( $this->_( 'Sorry, but wrong access. Please go back to %s.' ), home_url( '/' ), get_bloginfo( 'name' ) ), 500, false ); break; } - } /** @@ -473,7 +474,7 @@ public function get_admin_connect_link( $require_publish = false ) { * Update admin account id. */ public function update_facebook_admin() { - if ( 'gianism' == $this->input->get( 'page' ) && wp_verify_nonce( $this->input->post( '_wpnonce' ), 'gianism_fb_account' ) ) { + if ( 'gianism' === $this->input->get( 'page' ) && wp_verify_nonce( $this->input->post( '_wpnonce' ), 'gianism_fb_account' ) ) { update_option( 'gianism_facebook_admin_id', $this->input->post( 'fb_account_id' ) ); $this->add_message( $this->_( 'Saved facebook account to use.' ) ); wp_redirect( admin_url( 'options-general.php?page=gianism&view=fb-api' ) ); @@ -489,7 +490,8 @@ public function update_facebook_admin() { */ public function get_returned_user() { $redirect_helper = $this->api->getRedirectLoginHelper(); - if ( ! ( $access_token = $redirect_helper->getAccessToken( $this->get_redirect_endpoint() ) ) ) { + $access_token = $redirect_helper->getAccessToken( $this->get_redirect_endpoint() ); + if ( ! $access_token ) { throw new \Exception( $redirect_helper->getError(), $redirect_helper->getErrorCode() ); } $user = $this->get_user_profile( 'login', $access_token ); @@ -507,7 +509,8 @@ public function get_returned_user() { * @return int */ public function mail_owner( $email ) { - if ( $owner = email_exists( $email ) ) { + $owner = email_exists( $email ); + if ( $owner ) { return $owner; } return $this->get_meta_owner( $this->umeta_mail, $email ); @@ -652,7 +655,8 @@ public function __get( $name ) { break; case 'admin': if ( is_null( $this->_admin_api ) ) { - if ( ! $this->fb_use_api || ! ( $token = $this->option->get( 'gianism_facebook_admin_token', false ) ) ) { + $token = $this->option->get( 'gianism_facebook_admin_token', false ); + if ( ! $this->fb_use_api || ! $token ) { return new \WP_Error( 404, $this->_( 'Token is not set. Please get it.' ) ); } try { @@ -666,6 +670,7 @@ public function __get( $name ) { ); // Check last updated $updated = $this->option->get( 'gianism_facebook_admin_refreshed', 0 ); + // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested if ( ! $updated || current_time( 'timestamp' ) > $updated + ( 60 * 60 * 24 * 60 ) ) { return new \WP_Error( 410, $this->_( 'Token is outdated. Please update it.' ) ); } @@ -742,12 +747,12 @@ public function get_graph_version() { */ public function get_current_page_api() { $page_id = $this->admin_id; - if ( 'me' == $page_id ) { + if ( 'me' === $page_id ) { return new \WP_Error( 500, __( 'Page is not set.', 'wp-gianism' ) ); } $token = ''; foreach ( $this->admin_pages as $page ) { - if ( $page_id == $page['id'] ) { + if ( $page_id === $page['id'] ) { $token = $page['token']; break; } @@ -826,5 +831,4 @@ public function fan_gate_helper() { // Do nothing _deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); } - } diff --git a/app/Gianism/Service/Google.php b/app/Gianism/Service/Google.php index a132ef8..1a904fd 100644 --- a/app/Gianism/Service/Google.php +++ b/app/Gianism/Service/Google.php @@ -102,8 +102,8 @@ protected function __construct( array $argument = [] ) { // Filter rewrite name add_filter( 'gianism_filter_service_prefix', - function( $prefix ) { - if ( 'google-auth' == $prefix ) { + function ( $prefix ) { + if ( 'google-auth' === $prefix ) { $prefix = 'google'; } return $prefix; @@ -203,7 +203,7 @@ protected function handle_default( $action ) { // Check if other user has these as meta_value $email = $profile['email']; $email_owner = $this->get_meta_owner( $this->umeta_account, $email ); - if ( $email_owner && ( get_current_user_id() != $email_owner ) ) { + if ( $email_owner && ( get_current_user_id() !== $email_owner ) ) { throw new \Exception( $this->duplicate_account_string() ); } // Now let's save user data @@ -310,6 +310,7 @@ protected function get_api_url( $action ) { } protected function login_label( $register = false, $context = '' ) { + // translators: %s is service name. return sprintf( _x( 'Sign in with %s', 'login label', 'wp-gianism' ), $this->verbose_service_name ); } diff --git a/app/Gianism/Service/Line.php b/app/Gianism/Service/Line.php index 502685d..4a043f8 100644 --- a/app/Gianism/Service/Line.php +++ b/app/Gianism/Service/Line.php @@ -45,8 +45,8 @@ protected function __construct( array $argument = [] ) { // Filter rewrite name add_filter( 'gianism_filter_service_prefix', - function( $prefix ) { - if ( 'line-auth' == $prefix ) { + function ( $prefix ) { + if ( 'line-auth' === $prefix ) { $prefix = 'line'; } return $prefix; @@ -81,7 +81,8 @@ protected function get_api_url( $action ) { 'state' => $state, // 'prompt' => 'consent', // For Debug by displaying consent screen always. ]; - if ( ( $prompt = $this->line_add_friend_prompt ) ) { + $prompt = $this->line_add_friend_prompt; + if ( $prompt ) { $params['bot_prompt'] = $prompt; } /** @@ -290,7 +291,8 @@ public function handle_default( $action ) { 'redirect_to' => $redirect_url, ] ); - $this->input->wp_die( sprintf( __( 'Sorry, but wrong access. Please go back to %2$s.', 'wp-gianism' ), home_url( '/' ), get_bloginfo( 'name' ) ), 500, false ); + // translators: %1$s is URL, %2$s is a site name. + $this->input->wp_die( sprintf( __( 'Sorry, but wrong access. Please go back to %2$s.', 'wp-gianism' ), esc_url( home_url( '/' ) ), get_bloginfo( 'name' ) ), 500, false ); break; } } diff --git a/app/Gianism/Service/NoMailService.php b/app/Gianism/Service/NoMailService.php index 5a49ef7..1681318 100644 --- a/app/Gianism/Service/NoMailService.php +++ b/app/Gianism/Service/NoMailService.php @@ -67,7 +67,7 @@ protected function wp_mail( $user_id, $subject, $message, $headers = '', $attach } /** - * Override default wp_mai + * Override default wp_mail * * @param array $args * @@ -76,7 +76,8 @@ protected function wp_mail( $user_id, $subject, $message, $headers = '', $attach final public function mail_handler( $args ) { $tos = []; foreach ( is_array( $args['to'] ) ? $args['to'] : [ $args['to'] ] as $to ) { - if ( $this->is_pseudo_mail( $to ) && ( $user_id = email_exists( $to ) ) ) { + $user_id = email_exists( $to ); + if ( $this->is_pseudo_mail( $to ) && $user_id ) { // Send mail $this->wp_mail( $user_id, $args['subject'], $args['message'], $args['headers'], $args['attachments'] ); } else { diff --git a/app/Gianism/Service/Twitter.php b/app/Gianism/Service/Twitter.php index 04b0c1a..2000098 100644 --- a/app/Gianism/Service/Twitter.php +++ b/app/Gianism/Service/Twitter.php @@ -118,7 +118,7 @@ protected function __construct( array $argument = array() ) { if ( version_compare( phpversion(), '5.5.0', '<' ) && $this->enabled ) { add_action( 'admin_notices', - function() { + function () { printf( '

%s

', sprintf( $this->_( 'Twitter Login requires PHP5.5 and over but yours is %s. Every twitter login will be failed.' ), phpversion() ) @@ -261,7 +261,7 @@ protected function handle_default( $action ) { $screen_name = $access_token['screen_name']; // Check if other user has registered $id_owner = $this->get_meta_owner( $this->umeta_id, $twitter_id ); - if ( $id_owner && ( get_current_user_id() != $id_owner ) ) { + if ( $id_owner && ( get_current_user_id() !== $id_owner ) ) { throw new \Exception( $this->duplicate_account_string() ); } // O.K. @@ -427,7 +427,8 @@ public function get_oauth( $oauth_token = null, $oauth_token_secret = null ) { * Tweet with Owner ID * * @see https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets - * @param string $string + * + * @param string $text * @param null $deprecated Since 5.1.0 * @param array $options * @param string $token @@ -435,12 +436,12 @@ public function get_oauth( $oauth_token = null, $oauth_token_secret = null ) { * * @return object|\WP_Error Json format object. */ - public function tweet( $string, $deprecated = null, array $options = [], $token = '', $secret = '' ) { + public function tweet( $text, $deprecated = null, array $options = [], $token = '', $secret = '' ) { return $this->call_api( 'tweets', array_merge( [ - 'text' => $string, + 'text' => $text, ], $options ), @@ -454,14 +455,15 @@ public function tweet( $string, $deprecated = null, array $options = [], $token /** * Tweet with media * - * @param string $string + * @param string $text * @param array $medias * @param TwitterOAuth $oauth * @param string $token * @param string $secret + * * @return object|\WP_Error JSON format object */ - public function tweet_with_media( $string, array $medias, $oauth = null, $token = '', $secret = '' ) { + public function tweet_with_media( $text, array $medias, $oauth = null, $token = '', $secret = '' ) { $media_ids = []; foreach ( $medias as $media ) { $media_id = $this->upload( $media, $oauth ); @@ -473,7 +475,7 @@ public function tweet_with_media( $string, array $medias, $oauth = null, $token return new \WP_Error( 500, __( 'Failed to upload media', 'wp-gianism' ) ); } return $this->tweet( - $string, + $text, $oauth, [ 'media' => [ @@ -511,7 +513,7 @@ public function upload( $path_or_id, $oauth = null ) { return new \WP_Error( 404, __( 'File not found.', 'wp-gianism' ) ); } $path = sys_get_temp_dir() . '/' . tmpfile() . '-' . basename( $path_or_id ); - if ( ! @file_put_contents( $path, $file ) ) { + if ( ! file_put_contents( $path, $file ) ) { return new \WP_Error( 500, __( 'Failed to download media', 'wp-gianism' ) ); } $object = $path; diff --git a/app/Gianism/UI/Screen.php b/app/Gianism/UI/Screen.php index d9c4ae7..ca5fec4 100644 --- a/app/Gianism/UI/Screen.php +++ b/app/Gianism/UI/Screen.php @@ -93,7 +93,7 @@ public function switch_button( $name, $current_value, $value = 1 ) { ?>
> + value="">