Skip to content

Commit

Permalink
30-06-2022
Browse files Browse the repository at this point in the history
### 1.2.0
[Added] GitHub Repo Sync for (TukuToi) Plugins
[Added] Total Page Number on pagination
[Added] Improved Error notices.
[Changed] Moved the "report this plugin" to the left in the cards
  • Loading branch information
smileBeda authored Jun 30, 2022
2 parents 8f6d9ca + de8efed commit 0c1694a
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.DS_Store
24 changes: 18 additions & 6 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ Donate link: https://paypal.me/tukutoi
Tags: directory, plugins
Requires at least: 1.0.0
Tested up to: 4.9.15
Stable tag: 1.1.4
Stable tag: 1.2.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Enables a ClassicPress Plugin Screen to browse, install, activate, deactivate, update, delete and paginate ClassicPress Plugins.
Adds a new screen in a ClassicPress Install where you can browse, install, activate, deactivate, update, delete and paginate Plugins listed on the ClassicPress Directory *or* on GitHub (read more about this additional repository below).

== Description ==
== Usage ==

Install and activate like any other plugin.

Navigate to Dashboard > Plugins > CP Plugins and start Installing Plugins.
You can install, activate, deactivate, update, delete, and also search Plugins from within the same screen.
Navigate to Dashboard > Plugins > Manage CP Plugins and start managing ClassicPress Plugins.
You can install, activate, deactivate, update, delete, and also search Plugins all 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.

== Plugins not listed in the ClassicPress Directory ==

It is possible to manage plugins that are not listed in the ClassicPress Directory with this plugin as well.
The conditions for this to work are:
- the GitHub stored Plugin MUST have a tag `classicpress-plugin`
- the GitHub Repository MUST have a valid Release tag named witha SemVer release version (like `1.0.0`) and Public Release with a manually uploaded Release Asset in Zip Format. This ZIP MUST be uploaded to the release section for `Attach binaries by dropping them here or selecting them.`
- currently only plugins stored by the TukuToi Organization are available - in the next release, a setting will be offered to end users in order to register any organziation or user.

== Disclaimers ==
- The plugin does not take any responsibility for Plugins downloaded from the ClassicPress Directory or GitHub.
- The ClassicPress Plugin Repository is not always well maintained by the Developers who list their plugins. They forget often to bump the Version Number of their Plugins. This means, you *might* not see an update, even if there is one, or you might see an update to a certain version and get an update to a much higher version.
- If a GitHub stored plugin is not following above (MUST) clauses, it will not be possible for this plugin to find, pull or else manage such repos.
50 changes: 28 additions & 22 deletions admin/class-cp-plgn-drctry-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function enqueue_scripts( $hook_suffix ) {
array(
'ajax_url' => esc_url( admin_url( 'admin-ajax.php' ) ),
'admin_url' => esc_url( get_admin_url( null, '', 'admin' ) ),
'nonce' => wp_create_nonce( 'cp-plgn-drctry-nonce' ),
'nonce' => wp_create_nonce( 'updates' ),
)
);
}
Expand All @@ -120,9 +120,9 @@ public function enqueue_scripts( $hook_suffix ) {
*/
public function install_cp_plugin( $overwrite = false ) {

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

Expand All @@ -148,9 +148,9 @@ public function install_cp_plugin( $overwrite = false ) {
*/
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' ) ) {
if ( ! isset( $_POST['_ajax_nonce'] )
|| empty( $_POST['_ajax_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ), 'updates' ) ) {
die( 'Invalid or missing Nonce!' );
}

Expand All @@ -176,25 +176,31 @@ public function update_cp_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['_ajax_nonce'] )
|| empty( $_POST['_ajax_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ), 'updates' ) ) {
die( 'Invalid or missing Nonce!' );
}

if ( ! isset( $_POST['slug'] ) ) {
if ( ! isset( $_POST['plugin'] ) ) {
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'] ) ) ) );
$deleted = delete_plugins( array( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ) );

if ( true !== $deleted ) {
if ( false === $deleted ) {
// creds are missing.
$deleted = 'This plugin couldn\'t be updated.';
$deleted = 'The Plugin Slug is missing from delete_plugins() function.';
} elseif ( null === $deleted ) {
$deleted = 'Filesystem Credentials are required. You are not allowed to perform this action.';
} elseif ( is_wp_error( $deleted ) ) {
$deleted = 'There has been an error. Please check the error logs.';
} elseif ( true !== $deleted ) {
$deleted = 'Unknown error occurred';
}

wp_send_json( $deleted );
Expand All @@ -206,9 +212,9 @@ public function delete_cp_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' ) ) {
if ( ! isset( $_POST['_ajax_nonce'] )
|| empty( $_POST['_ajax_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ), 'updates' ) ) {
die( 'Invalid or missing Nonce!' );
}

Expand All @@ -232,9 +238,9 @@ public function deactivate_cp_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' ) ) {
if ( ! isset( $_POST['_ajax_nonce'] )
|| empty( $_POST['_ajax_nonce'] )
|| ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_nonce'] ) ), 'updates' ) ) {
die( 'Invalid or missing Nonce!' );
}

Expand Down Expand Up @@ -279,7 +285,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="notice notice-error" style="display:none;"></div>
<div class="notice notice-error" id="cp-plgn-drctry-error" style="display:none;"></div>
<?php $this->cp_dir->list_plugins(); ?>
</div>
<?php
Expand Down
38 changes: 33 additions & 5 deletions admin/class-cp-plgn-drctry-cp-dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct( $plugin_name, $plugin_prefix, $version ) {
$this->plugin_name = $plugin_name;
$this->plugin_prefix = $plugin_prefix;
$this->version = $version;

}

/**
Expand All @@ -69,6 +70,11 @@ public function list_plugins() {

// All Plugins.
$plugins = $this->get_plugins();
// If $plugins is empty, there was an error. Abort.
if ( empty( $plugins ) ) {
return '';
}

$has_update = $this->has_update( $plugins );
// Maybe search.
$plugins = $this->search_plugins( $plugins );
Expand Down Expand Up @@ -110,6 +116,7 @@ public function list_plugins() {
);
// Get current chunk of plugins.
$current_plugins = $paginated[ $paged ];

// Render everything in HTML.
include( __DIR__ . '/partials/cp-plgn-drctry-admin-display.php' );

Expand Down Expand Up @@ -166,8 +173,10 @@ private function get_plugins() {
/**
* If cache not yet built or refreshing cache.
*/

if ( 0 === filesize( __DIR__ . '/partials/cp-plugins.txt' )
|| ( isset( $_GET['refresh'] )
|| (
isset( $_GET['refresh'] )
&& isset( $_GET['tkt_nonce'] )
&& wp_verify_nonce( sanitize_key( wp_unslash( $_GET['tkt_nonce'] ) ), 'tkt-refresh-data' )
&& 1 === (int) $_GET['refresh']
Expand All @@ -179,6 +188,9 @@ private function get_plugins() {
$this->put_file_contents( '' );
}

// get github plugins.
$git_plugins = new Cp_Plgn_Drctry_GitHub( $this->plugin_name, $this->plugin_prefix, $this->version );

// get first page.
$plugins = wp_remote_get( 'https://directory.classicpress.net/api/plugins/' );

Expand Down Expand Up @@ -215,14 +227,30 @@ private function get_plugins() {
// Merge plugins into main plugins array.
$all_plugins = array_merge( $all_plugins, $current_page_plugins_body->data );

} else {

echo '<script>jQuery("#cp-plgn-drctry-error").css("display","block").html("<p>' . esc_js( __( 'We could not reach some sub-page of the ClassicPress Directory API. It is possible you have reached the ClassicPress Directory API Limits.', 'cp-plgn-drctry' ) ) . '</p>");</script>';
error_log( print_r( $current_page_plugins, true ) );

}
}
} else {

echo '<div class="notice notice-error"><p>' . esc_html__( 'We could not reach the ClassicPress Directory API. It is possible you reached the limits of the ClassicPress Directory API.', 'cp-plgn-drctry' ) . '</p></div>';
error_log( print_r( $plugins, true ) );

// Re-encode all plugins to JSON.
$plugins = wp_json_encode( $all_plugins );
$this->put_file_contents( $plugins );
}

// insert git plugins here.
$plugins = array_merge( $all_plugins, $git_plugins->get_git_plugins() );
// Re-encode all plugins to JSON.
if ( ! empty( $plugins ) ) {
$plugins = wp_json_encode( $plugins );
} else {
$plugins = '';
}
$this->put_file_contents( $plugins );

}

// Get data from cache.
Expand Down Expand Up @@ -258,7 +286,7 @@ private function search_plugins( $plugins ) {
$plugins = $found_plugins;
} else {
// Nothing wrong with this, since it is hardcoded no need to escape.
echo '<script>jQuery(".error").css("display","block").html("' . esc_js( __( 'Nothing Found', 'cp-plgn-drctry' ) ) . '");</script>';
echo '<script>jQuery(".notice-error").css("display","block").html("' . esc_js( __( 'Nothing Found', 'cp-plgn-drctry' ) ) . '");</script>';
}
}

Expand Down
Loading

0 comments on commit 0c1694a

Please sign in to comment.