Skip to content

Commit

Permalink
07-06-2022
Browse files Browse the repository at this point in the history
= 1.1.0 =
[Added] AJAXified install/activate/deactivate/update/delete buttons/features.
[Added] Bottom pagination
[Fixed] Design of main search bar
  • Loading branch information
smileBeda committed Jun 7, 2022
1 parent c59094d commit b180ede
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 81 deletions.
12 changes: 6 additions & 6 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ Donate link: https://paypal.me/tukutoi
Tags: directory, plugins
Requires at least: 1.0.0
Tested up to: 4.9.15
Stable tag: 1.0.1
Stable tag: 1.1.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Enables a ClassicPress Plugin Screen to download and install ClassicPress Plugins.
Enables a ClassicPress Plugin Screen to browse, install, activate, deactivate, update, delete and paginate ClassicPress Plugins.

== Description ==

Install and activate like any other plugin.

Navigate to Dashboard > Plugins > CP Plugins and start Installing Plugins.
If the plugin installed succesfully, the button "Activate" will appear, which will lead to the "inactive" plugins screen to activate it.
An active Plugin will show a "Active" (green background) hint.
An error is shown if the plugin for some reason could not be downloaded/installed.
You can install, activate, deactivate, update, delete, and also search Plugins from within the same screen.
The Directory results are cached locally for fast performance, and you can refresh the local cache on the click of a button.

It has a pagination and a total plugins display to navigate (15 plugins a time) through the assets.
A "more info" will display all information known to ClassicPress about the plugin and developer.

The plugin requires wp_remote_get and file_put_contents to work properly on the server.
The plugin does not take any responsibility for Plugins downloaded from the ClassicPress Directory.
The plugin does not take any responsibility for Plugins downloaded from the ClassicPress Directory.
113 changes: 111 additions & 2 deletions admin/class-cp-plgn-drctry-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,114 @@ public function install_cp_plugin() {

}

/**
* Update a Plugin.
*/
public function update_cp_plugin() {

if ( ! isset( $_POST['nonce'] )
|| empty( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'cp-plgn-drctry-nonce' ) ) {
die( 'Invalid or missing Nonce!' );
}

if ( ! isset( $_POST['slug'] ) ) {
wp_send_json( 'Something went wrong' );
}

/**
* We cannot use Upgrader Class, because CP has no way of
* selecting custom file URL. Only WP Can do that.
*
* Thus we need to manually deactivate, delete, and then install the plugin.
*/
deactivate_plugins( sanitize_text_field( wp_unslash( $_POST['slug'] ) ), true );
delete_plugins( array( sanitize_text_field( wp_unslash( $_POST['slug'] ) ) ) );
$this->install_cp_plugin();

}

/**
* Delete a Plugin.
*/
public function delete_cp_plugin() {

if ( ! isset( $_POST['nonce'] )
|| empty( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'cp-plgn-drctry-nonce' ) ) {
die( 'Invalid or missing Nonce!' );
}

if ( ! isset( $_POST['slug'] ) ) {
wp_send_json( 'Something went wrong' );
}

/**
* This returns true on success, false if $Plugin is empty,
* null if creds are missing, WP Error on failure.
*/
$deleted = delete_plugins( array( sanitize_text_field( wp_unslash( $_POST['slug'] ) ) ) );

if ( true !== $deleted ) {
// creds are missing.
$deleted = 'This plugin couldn\'t be updated.';
}

wp_send_json( $deleted );

}

/**
* Deactivate a Plugin.
*/
public function deactivate_cp_plugin() {

if ( ! isset( $_POST['nonce'] )
|| empty( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'cp-plgn-drctry-nonce' ) ) {
die( 'Invalid or missing Nonce!' );
}

if ( ! isset( $_POST['slug'] ) ) {
wp_send_json( 'Something went wrong' );
}

/**
* This function does not return anything.
* We have no way of knowing whether the plugin was deactivated or not.
* We however reload the page in JS after this operation, so the new status will tell.
*/
deactivate_plugins( sanitize_text_field( wp_unslash( $_POST['slug'] ) ), true );

wp_send_json( 'Plugin Possibly Updated' );

}

/**
* Activate a Plugin.
*/
public function activate_cp_plugin() {

if ( ! isset( $_POST['nonce'] )
|| empty( $_POST['nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'cp-plgn-drctry-nonce' ) ) {
die( 'Invalid or missing Nonce!' );
}

if ( ! isset( $_POST['slug'] ) ) {
wp_send_json( 'Something went wrong' );
}

/**
* The function returns a WP error if something went wrong,
* null otherwise.
*/
$activated = activate_plugin( sanitize_text_field( wp_unslash( $_POST['slug'] ) ) );

wp_send_json( $activated );

}

/**
* Creates the submenu item and calls on the Submenu Page object to render
* the actual contents of the page.
Expand All @@ -149,10 +257,11 @@ public function add_plugins_list() {
add_submenu_page(
'plugins.php',
esc_html__( 'ClassicPress Plugins', 'cp-plgn-drctry' ),
esc_html__( 'Add CP Plugin', 'cp-plgn-drctry' ),
esc_html__( 'Manage CP Plugins', 'cp-plgn-drctry' ),
'install_plugins',
'cp-plugins',
array( $this, 'render' ),
3
);

}
Expand All @@ -166,7 +275,7 @@ public function render() {
<div class="wrap">
<h1><?php esc_html_e( 'ClassicPress Plugins', 'cp-plgn-drctry' ); ?></h1>
<p><?php esc_html_e( 'Browse, Install and Activate ClassicPress Plugins', 'cp-plgn-drctry' ); ?></p>
<div class="error" style="display:none;"></div>
<div class="notice notice-error" style="display:none;"></div>
<?php $this->cp_dir->list_plugins(); ?>
</div>
<?php
Expand Down
118 changes: 72 additions & 46 deletions admin/class-cp-plgn-drctry-cp-dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,59 @@ public function __construct( $plugin_name, $plugin_prefix, $version ) {
$this->version = $version;
}

/**
* Chunk plugins into "pages" and return as grid/list.
*/
public function list_plugins() {

// All Plugins.
$plugins = $this->get_plugins();
$has_update = $this->has_update( $plugins );
// Maybe search.
$plugins = $this->search_plugins( $plugins );
// Paginate them by 15.
$paginated = array_chunk( $plugins, 15, false );
// Last page is amount of array keys - 1 (because of start at 0).
$last = count( $paginated ) - 1;
// Check if the current page is a paged URL.
$paged = 0;
if ( isset( $_GET['paged'] )
&& isset( $_GET['tkt_page_nonce'] )
&& wp_verify_nonce( sanitize_key( wp_unslash( $_GET['tkt_page_nonce'] ) ), 'tkt_page_nonce' )
) {
$paged = (int) $_GET['paged'];
}
// Build "prev" link "paged" value.
$prev = filter_var(
$paged - 1,
FILTER_VALIDATE_INT,
array(
'options' => array(
'default' => 0,
'min_range' => 0,
'max_range' => $last,
),
)
);
// Build "next" link "paged" value.
$next = filter_var(
$paged + 1,
FILTER_VALIDATE_INT,
array(
'options' => array(
'default' => 0,
'min_range' => 1,
'max_range' => $last,
),
)
);
// Get current chunk of plugins.
$current_plugins = $paginated[ $paged ];
// Render everything in HTML.
include( __DIR__ . '/partials/cp-plgn-drctry-admin-display.php' );

}

/**
* CP Way of getting File Contents.
*/
Expand Down Expand Up @@ -105,7 +158,7 @@ private function put_file_contents( $contents ) {
*
* @return array $plugins An array of all plugins objects.
*/
public function get_plugins() {
private function get_plugins() {

$plugins = array();
$all_plugins = array();
Expand Down Expand Up @@ -248,54 +301,27 @@ private function search_form() {
}

/**
* Chunk plugins into "pages" and return as grid/list.
* Helper function to check if plugin has update.
*
* @param object $plugins All Plugin Objects in array.
* @return bool $is_installed If the plugin is installed or not.
*/
public function list_plugins() {
private function has_update( $plugins ) {

// All Plugins.
$plugins = $this->get_plugins();
// Maybe search.
$plugins = $this->search_plugins( $plugins );
// Paginate them by 15.
$paginated = array_chunk( $plugins, 15, false );
// Last page is amount of array keys - 1 (because of start at 0).
$last = count( $paginated ) - 1;
// Check if the current page is a paged URL.
$paged = 0;
if ( isset( $_GET['paged'] )
&& isset( $_GET['tkt_page_nonce'] )
&& wp_verify_nonce( sanitize_key( wp_unslash( $_GET['tkt_page_nonce'] ) ), 'tkt_page_nonce' )
) {
$paged = (int) $_GET['paged'];
$updates = array();
foreach ( $plugins as $plugin ) {
if ( $this->check_plugin_installed( $plugin ) ) {

$current_installed_version = get_plugins()[ $this->plugin_slug( $plugin ) ]['Version'];
$remote_version = $plugin->current_version;
$has_update = version_compare( $current_installed_version, $remote_version );
if ( -1 === $has_update ) {
$updates[ $this->plugin_slug( $plugin ) ] = array( $current_installed_version, $remote_version );
}
}
}
// Build "prev" link "paged" value.
$prev = filter_var(
$paged - 1,
FILTER_VALIDATE_INT,
array(
'options' => array(
'default' => 0,
'min_range' => 0,
'max_range' => $last,
),
)
);
// Build "next" link "paged" value.
$next = filter_var(
$paged + 1,
FILTER_VALIDATE_INT,
array(
'options' => array(
'default' => 0,
'min_range' => 1,
'max_range' => $last,
),
)
);
// Get current chunk of plugins.
$current_plugins = $paginated[ $paged ];
// Render everything in HTML.
include( __DIR__ . '/partials/cp-plgn-drctry-admin-display.php' );

return $updates;

}

Expand Down
11 changes: 9 additions & 2 deletions admin/css/cp-plgn-drctry-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
align-items: center;
justify-content: space-between;
}
.alignleft.actions .button.no-margin{
.button.no-margin{
margin: unset;
}
.tkt-src-form > input {
height: 28px;
margin: 3px 8px 0 0;
}
.tkt-current-page {
display: inline-block;
Expand All @@ -56,4 +57,10 @@
font-weight: 400;
text-align: center;
}
}
.tablenav.top,
.aligncenter.form {
display: flex;
justify-content: space-between;
align-items: center;
height: unset;
}
Loading

0 comments on commit b180ede

Please sign in to comment.