Skip to content

Commit

Permalink
refactor functions.php
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedhk2 committed Oct 19, 2023
1 parent ee2b492 commit 20ce52d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 89 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<p align="center"><img src="https://github.com/wp-activators/.github/blob/main/FreePalestine.png" height="250"></p>


# WP Cerber Security Activator

WP Cerber Security, Anti-spam & Malware Scan Plugin Activator
177 changes: 97 additions & 80 deletions functions.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Requires at least: 3.1.0
* Requires at least: 5.9.0
* Requires PHP: 7.2
* Version: 230917
*/
Expand All @@ -10,24 +10,77 @@
if ( ! function_exists( 'wp_get_current_user' ) ) {
require_once( ABSPATH . 'wp-includes/pluggable.php' );
}
if ( ! function_exists( 'is_plugin_installed' ) ) {
function is_plugin_installed( $plugin ): bool {

/**
* @param string $name
* @param string $slug
* @param callable $callback
* @param int $priority
*
* @return void
* @author mohamedhk2
*/
$activator_inject_plugins_filter = function ( 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;
}

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;
}
global $totals;
if ( $is_current ) {
foreach ( $views as $key => $view ) {
$views[ $key ] = str_replace( ' class="current" aria-current="page"', null, $view );
}
}
$views[ $slug ] = sprintf(
'<a href="%s"%s>%s</a>',
esc_url( add_query_arg( 'plugin_status', $slug, 'plugins.php' ) ),
$is_current ? ' class="current" aria-current="page"' : null,
sprintf( '%s <span class="count">(%s)</span>', $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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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(
'<a href="%s"%s>%s</a>',
esc_url( add_query_arg( 'plugin_status', $slug, 'plugins.php' ) ),
$is_current ? ' class="current" aria-current="page"' : null,
sprintf( '%s <span class="count">(%s)</span>', $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' );
} );
];
21 changes: 12 additions & 9 deletions wp-cerber-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -51,7 +52,9 @@
}
}
}
require_once $patcher_file;
if ( ! class_exists( 'Mohamedhk2\PhpPatcher\Patcher' ) ) {
require_once $patcher_file;
}

use \Mohamedhk2\PhpPatcher\Patcher;

Expand Down

0 comments on commit 20ce52d

Please sign in to comment.