From 20ce52d092e2eb329aa9b539d604133a4294fa56 Mon Sep 17 00:00:00 2001 From: mohamedhk2 Date: Thu, 19 Oct 2023 01:07:07 +0100 Subject: [PATCH] refactor functions.php --- README.md | 3 + functions.php | 177 ++++++++++++++++++++++------------------ wp-cerber-activator.php | 21 +++-- 3 files changed, 112 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index 8843365..b6cb20d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +

+ + # WP Cerber Security Activator WP Cerber Security, Anti-spam & Malware Scan Plugin Activator diff --git a/functions.php b/functions.php index 38af7bd..390f6b0 100644 --- a/functions.php +++ b/functions.php @@ -1,6 +1,6 @@ $view ) { + $views[ $key ] = str_replace( ' class="current" aria-current="page"', null, $view ); + } + } + $views[ $slug ] = sprintf( + '%s', + esc_url( add_query_arg( 'plugin_status', $slug, 'plugins.php' ) ), + $is_current ? ' class="current" aria-current="page"' : null, + sprintf( '%s (%s)', $name, $totals[ $slug ] ) + ); + + return $views; + }, $priority ); +}; + +$activator_inject_plugins_filter( 'WP Activators', 'wp-activators', function ( $plugin ) { + return $plugin['Author'] === 'mohamedhk2' && str_ends_with( $plugin['Name'], ' Activator' ); +} ); + +return [ + 'is_plugin_installed' => $is_plugin_installed = function ( $plugin ): bool { $installed_plugins = get_plugins(); return isset( $installed_plugins[ $plugin ] ); - } -} -if ( ! function_exists( 'activator_admin_notice_ignored' ) ) { - function activator_admin_notice_ignored(): bool { + }, + 'activator_admin_notice_ignored' => function (): bool { global $pagenow; $action = $_REQUEST['action'] ?? ''; return $pagenow == 'update.php' && in_array( $action, [ 'install-plugin', 'upload-plugin' ], true ); - } -} -if ( ! function_exists( 'activator_admin_notice_plugin_install' ) ) { - function activator_admin_notice_plugin_install( string $plugin, ?string $wp_plugin_id, string $plugin_name, string $activator_name, string $domain ): bool { - if ( ! is_plugin_installed( $plugin ) ) { + }, + 'activator_admin_notice_plugin_install' => function ( string $plugin, ?string $wp_plugin_id, string $plugin_name, string $activator_name, string $domain ) use ( $is_plugin_installed ): bool { + if ( ! $is_plugin_installed( $plugin ) ) { if ( ! current_user_can( 'install_plugins' ) ) { return true; } @@ -48,10 +101,8 @@ function activator_admin_notice_plugin_install( string $plugin, ?string $wp_plug } return false; - } -} -if ( ! function_exists( 'activator_admin_notice_plugin_activate' ) ) { - function activator_admin_notice_plugin_activate( string $plugin, string $activator_name, string $domain ): bool { + }, + 'activator_admin_notice_plugin_activate' => function ( string $plugin, string $activator_name, string $domain ): bool { if ( ! is_plugin_active( $plugin ) ) { if ( ! current_user_can( 'activate_plugins' ) ) { return true; @@ -81,80 +132,46 @@ function activator_admin_notice_plugin_activate( string $plugin, string $activat } return false; - } -} -if ( ! function_exists( 'activator_json_response' ) ) { - function activator_json_response( $data ) { + }, + 'activator_json_response' => function ( $data ) { return [ 'response' => [ 'code' => 200, 'message' => 'OK' ], 'body' => json_encode( $data ) ]; - } -} -if ( ! function_exists( 'activator_private_property' ) ) { - /** - * @throws ReflectionException - */ - function activator_private_property( object $object, string $property ) { + }, + 'activator_private_property' => function ( object $object, string $property ) { $reflectionProperty = new \ReflectionProperty( get_class( $object ), $property ); $reflectionProperty->setAccessible( true ); return $reflectionProperty->getValue( $object ); - } -} -if ( ! function_exists( 'activator_inject_plugins_filter' ) ) { - /** - * @param string $name - * @param string $slug - * @param callable $callback - * @param int $priority - * - * @return void - * @author mohamedhk2 - */ - function activator_inject_plugins_filter( string $name, string $slug, callable $callback, int $priority = 99 ) { - $is_current = isset( $_REQUEST['plugin_status'] ) && $_REQUEST['plugin_status'] === $slug; - add_filter( 'plugins_list', function ( $plugins ) use ( $name, $slug, $is_current, $callback ) { - /** - * @see \WP_Plugins_List_Table::prepare_items - * @see \WP_Plugins_List_Table::get_sortable_columns - */ - global $status; - if ( $is_current ) { - $status = $slug; - } - $inj_plugins = array_filter( $plugins['all'], $callback ); - if ( ! empty( $inj_plugins ) ) { - $plugins[ $slug ] = $inj_plugins; - } + }, + 'activator_inject_plugins_filter' => $activator_inject_plugins_filter, + 'activator_download_file' => function ( $url, $file_path ) { + $contents = file_get_contents( $url ); + if ( $contents ) { + put_content: + $put_contents = file_put_contents( $file_path, $contents ); + if ( $put_contents === false ) { + unlink( $file_path ); - return $plugins; - }, $priority ); - add_filter( 'views_plugins', function ( $views ) use ( $name, $slug, $is_current ) { - /** - * @see \WP_Plugins_List_Table::views - * @see \WP_Plugins_List_Table::get_views - */ - if ( ! isset( $views[ $slug ] ) ) { - return $views; + return false; } - global $totals; - if ( $is_current ) { - foreach ( $views as $key => $view ) { - $views[ $key ] = str_replace( ' class="current" aria-current="page"', null, $view ); - } + } else { + $res = wp_remote_get( $url ); + if ( ! is_wp_error( $res ) && ( $res['response']['code'] == 200 ) ) { + $contents = $res['body']; + goto put_content; + } else { + return false; } - $views[ $slug ] = sprintf( - '%s', - esc_url( add_query_arg( 'plugin_status', $slug, 'plugins.php' ) ), - $is_current ? ' class="current" aria-current="page"' : null, - sprintf( '%s (%s)', $name, $totals[ $slug ] ) - ); + } - return $views; - }, $priority ); + return true; + }, + 'activator_serialize_response' => function ( $data ): array { + return [ + 'response' => [ 'code' => 200, 'message' => 'OK' ], + 'body' => serialize( $data ) + ]; } -} -activator_inject_plugins_filter( 'WP Activators', 'wp-activator', function ( $plugin ) { - return $plugin['Author'] === 'mohamedhk2' && str_ends_with( $plugin['Name'], ' Activator' ); -} ); +]; diff --git a/wp-cerber-activator.php b/wp-cerber-activator.php index 4003561..78551f2 100644 --- a/wp-cerber-activator.php +++ b/wp-cerber-activator.php @@ -5,21 +5,22 @@ * Plugin Name: WP Cerber Security Activator * Plugin URI: https://github.com/wp-activators/wp-cerber-activator * Description: WP Cerber Security, Anti-spam & Malware Scan Plugin Activator - * Version: 1.1.0 - * Requires at least: 3.1.0 + * Version: 1.2.0 + * Requires at least: 5.9.0 * Requires PHP: 7.2 * Author: mohamedhk2 * Author URI: https://github.com/mohamedhk2 **/ defined( 'ABSPATH' ) || exit; -const WP_CERBER_ACTIVATOR_NAME = 'WP Cerber Security Activator'; -const WP_CERBER_ACTIVATOR_DOMAIN = 'wp-cerber-activator'; -require_once __DIR__ . DIRECTORY_SEPARATOR . 'functions.php'; +$WP_CERBER_ACTIVATOR_NAME = 'WP Cerber Security Activator'; +$WP_CERBER_ACTIVATOR_DOMAIN = 'wp-cerber-activator'; +$functions = require_once __DIR__ . DIRECTORY_SEPARATOR . 'functions.php'; +extract( $functions ); if ( - activator_admin_notice_ignored() - || activator_admin_notice_plugin_install( 'wp-cerber/wp-cerber.php', null, 'WP Cerber Security, Anti-spam & Malware Scan', WP_CERBER_ACTIVATOR_NAME, WP_CERBER_ACTIVATOR_DOMAIN ) - || activator_admin_notice_plugin_activate( 'wp-cerber/wp-cerber.php', WP_CERBER_ACTIVATOR_NAME, WP_CERBER_ACTIVATOR_DOMAIN ) + $activator_admin_notice_ignored() + || $activator_admin_notice_plugin_install( 'wp-cerber/wp-cerber.php', null, 'WP Cerber Security, Anti-spam & Malware Scan', $WP_CERBER_ACTIVATOR_NAME, $WP_CERBER_ACTIVATOR_DOMAIN ) + || $activator_admin_notice_plugin_activate( 'wp-cerber/wp-cerber.php', $WP_CERBER_ACTIVATOR_NAME, $WP_CERBER_ACTIVATOR_DOMAIN ) ) { return; } @@ -51,7 +52,9 @@ } } } -require_once $patcher_file; +if ( ! class_exists( 'Mohamedhk2\PhpPatcher\Patcher' ) ) { + require_once $patcher_file; +} use \Mohamedhk2\PhpPatcher\Patcher;