first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,194 @@
<?php
/**
* WP-Sweep WP-API
*
* @package wp-sweep
*/
/**
* Class WPSweep_Api
*/
class WPSweep_Api {
/**
* WP-Sweep WP Rest API namespace
*
* @var string
*/
private $namespace = 'sweep/v1';
/**
* List of sweeps
*
* @var array
*/
private $sweeps = array(
'revisions',
'auto_drafts',
'deleted_posts',
'unapproved_comments',
'spam_comments',
'deleted_comments',
'transient_options',
'orphan_postmeta',
'orphan_commentmeta',
'orphan_usermeta',
'orphan_termmeta',
'orphan_term_relationships',
'unused_terms',
'duplicated_postmeta',
'duplicated_commentmeta',
'duplicated_usermeta',
'duplicated_termmeta',
'optimize_database',
'oembed_postmeta',
);
/**
* Register WP-Sweep API Routes
*
* @since 1.1.0
*
* @access public
* @return void
*/
public function __construct() {
add_action(
'rest_api_init', function() {
register_rest_route(
$this->namespace, 'count/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'count' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
register_rest_route(
$this->namespace, 'details/(?P<name>\w+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'details' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
register_rest_route(
$this->namespace, 'sweep/(?P<name>\w+)', array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'sweep' ),
'permission_callback' => array( $this, 'permission_check' ),
'args' => array(
'name' => array(
'required' => true,
'validate_callback' => array( $this, 'is_sweep_name_valid' ),
),
),
)
);
}
);
}
/**
* Sweep item count
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function count( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$count = (int) $sweep->count( $params['name'] );
return new WP_REST_Response(
array(
'name' => $params['name'],
'count' => $count,
)
);
}
/**
* Sweep details
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function details( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$details = $sweep->details( $params['name'] );
return new WP_REST_Response(
array(
'name' => $params['name'],
'count' => count( $details ),
'data' => $details,
)
);
}
/**
* Lets do the sweeping
*
* @since 1.1.0
*
* @access public
* @param WP_REST_Request $request Request.
* @return WP_REST_Response
*/
public function sweep( $request ) {
$params = $request->get_params();
$sweep = new WPSweep();
$results = $sweep->sweep( $params['name'] );
return new WP_REST_Response(
array(
'success' => ! empty( $results ),
'name' => $params['name'],
'message' => empty( $results ) ? __( 'No items left to sweep.', 'wp-sweep' ) : $results,
)
);
}
/**
* Check whether a sweep is valid
*
* @since 1.1.0
*
* @access public
* @param string $name Sweep name.
* @return bool Is the sweep name valid?
*/
public function is_sweep_name_valid( $name ) {
return in_array( $name, $this->sweeps, true );
}
/**
* Check whether the function is allowed to be run. Must have either capabilities to enact action, or a valid nonce.
*
* @since 1.1.0
*
* @access public
* @return bool Does the user has access to sweep?
*/
public function permission_check() {
return current_user_can( 'activate_plugins' );
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* WP-Sweep WP-CLI
*
* @package wp-sweep
*/
/**
* Class WPSweep_Command
*/
class WPSweep_Command extends WP_CLI_Command {
/**
* Clean up unused, orphaned and duplicated data in your WordPress
*
* ## OPTIONS
*
* [--all]
* Sweep all the orphaned data at once.
*
* Name of the items selected individually
* Available Items =
* revisions
* auto_drafts
* deleted_posts
* unapproved_comments
* spam_comments
* deleted_comments
* transient_options
* orphan_postmeta
* orphan_commentmeta
* orphan_usermeta
* orphan_termmeta
* orphan_term_relationships
* unused_terms
* duplicated_postmeta
* duplicated_commentmeta
* duplicated_usermeta
* duplicated_termmeta
* optimize_database
* oembed_postmeta
*
* ## EXAMPLES
*
* 1. wp sweep --all
* - Run Sweep for all the items.
* 2. wp sweep revisions
* - Sweep only Revision
* 3. wp sweep revisions auto_drafts deleted_posts unapproved_comments spam_comments deleted_comments transient_options orphan_postmeta orphan_commentmeta orphan_usermeta orphan_termmeta orphan_term_relationships unused_terms duplicated_postmeta duplicated_commentmeta duplicated_usermeta duplicated_termmeta optimize_database oembed_postmet
* - Sweep the selected items
*
* @since 1.0.8
*
* @access public
*
* @param array $args array Arguments passed to command. Generally unused.
* @param array $assoc_args Parameters passed to command to be passed to callback.
* @return void
*/
public function __invoke( $args, $assoc_args ) {
$items = array();
$default_items = array(
'0' => 'revisions',
'1' => 'auto_drafts',
'2' => 'deleted_posts',
'3' => 'unapproved_comments',
'4' => 'spam_comments',
'5' => 'deleted_comments',
'6' => 'transient_options',
'7' => 'orphan_postmeta',
'8' => 'orphan_commentmeta',
'9' => 'orphan_usermeta',
'10' => 'orphan_termmeta',
'11' => 'orphan_term_relationships',
'12' => 'unused_terms',
'13' => 'duplicated_postmeta',
'14' => 'duplicated_commentmeta',
'15' => 'duplicated_usermeta',
'16' => 'duplicated_termmeta',
'17' => 'optimize_database',
'18' => 'oembed_postmeta',
);
if ( isset( $assoc_args['all'] ) && true === $assoc_args['all'] ) {
$this->run_sweep( $default_items );
WP_CLI::success( 'Sweep Complete' );
return;
} else {
foreach ( $default_items as $key => $item ) {
if ( in_array( $item, $args, true ) ) {
array_push( $items, $item );
}
}
$this->run_sweep( $items );
WP_CLI::success( 'Sweep Complete!' );
return;
}
}
/**
* Run WP-Sweep
*
* @since 1.0.8
*
* @access public
*
* @param array $items Sweep items.
* @return void
*/
public function run_sweep( $items ) {
$sweep = new WPSweep();
foreach ( $items as $key => $value ) {
$count = $sweep->count( $value );
if ( 0 !== $count && '0' !== $count ) {
$message = $sweep->sweep( $value );
WP_CLI::success( $message );
}
}
}
}