Skip to content

Commit

Permalink
Improve plugin structure, add new class for CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
joppuyo committed Mar 21, 2024
1 parent 833077f commit 96d3dc3
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 39 deletions.
39 changes: 39 additions & 0 deletions lib/Modules/CLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace NPX\DisableMediaPages\Modules;

class CLI
{
private static $instance = null;

public static function get_instance(): CLI
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

private function __construct()
{
add_filter('cli_init', [$this, 'cli_init']);
}

public function cli_init()
{
\WP_CLI::add_command('disable-media-pages mangle', [$this, 'mangle']);
\WP_CLI::add_command('disable-media-pages restore', [$this, 'restore']);
}

public function mangle()
{
\WP_CLI::success('Mangling media slugs');
}

public function restore()
{
\WP_CLI::success('Restoring media slugs');
}

}
49 changes: 49 additions & 0 deletions lib/Modules/Mangle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace NPX\DisableMediaPages\Modules;

use NPX\DisableMediaPages\Plugin;

class Mangle
{
private static $instance = null;

public static function get_instance(): Mangle
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

public function get_attachments_to_mangle()
{
global $wpdb;

$result = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_name NOT RLIKE '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$'"
);

return $result;
}

public function mangle_attachment($attachment_id)
{
$plugin = Plugin::get_instance();
$attachment = get_post($attachment_id);
$slug = $attachment->post_name;

$is_uuid = Plugin::is_uuid($slug);

if (!$is_uuid) {
$new_attachment = [
'ID' => $attachment->ID,
'post_name' => $plugin->generate_uuid_v4(),
];

wp_update_post($new_attachment);
}

}
}
51 changes: 12 additions & 39 deletions lib/Modules/REST.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ public function rest_api_get_status(WP_REST_Request $data)

public function rest_api_get_all_attachments(WP_REST_Request $data)
{
global $wpdb;

$result = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_name NOT RLIKE '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$'"
);
$mangle = Mangle::get_instance();

$result = $mangle->get_attachments_to_mangle();

$json = [
'posts' => $result,
Expand All @@ -144,31 +143,20 @@ public function rest_api_get_all_attachments(WP_REST_Request $data)

public function rest_api_process_attachment(WP_REST_Request $data)
{
$plugin = Plugin::get_instance();
$attachment = get_post($data->get_param('id'));
$slug = $attachment->post_name;
$mangle = Mangle::get_instance();

$is_uuid = Plugin::is_uuid($slug);

if (!$is_uuid) {
$new_attachment = [
'ID' => $attachment->ID,
'post_name' => $plugin->generate_uuid_v4(),
];
$post_id = $data->get_param('id');

wp_update_post($new_attachment);
}
$mangle->mangle_attachment($post_id);

return new WP_REST_Response([]);
}

public function rest_api_get_attachments_to_restore(WP_REST_Request $data)
{
global $wpdb;
$restore = Restore::get_instance();

$result = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_name RLIKE '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$' ORDER BY post_date ASC;"
);
$result = $restore->get_attachments_to_restore();

$json = [
'posts' => $result,
Expand All @@ -181,26 +169,11 @@ public function rest_api_get_attachments_to_restore(WP_REST_Request $data)

public function rest_api_restore_attachment(WP_REST_Request $data)
{
$plugin = Plugin::get_instance();

$post_id = $data->get_param('id');
$attachment = get_post($post_id);
$slug = $attachment->post_name;

$is_uuid = Plugin::is_uuid($slug);

if ($is_uuid) {
$new_slug = sanitize_title($attachment->post_title);

// Remove our filter so we get a real slug instead of UUID
remove_filter('wp_unique_post_slug', [$plugin, 'unique_slug'], 10);

$new_attachment = [
'ID' => $attachment->ID,
'post_name' => $new_slug,
];
wp_update_post($new_attachment);
}

$restore = Restore::get_instance();

$restore->restore_attachment($post_id);

return new WP_REST_Response([]);
}
Expand Down
55 changes: 55 additions & 0 deletions lib/Modules/Restore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace NPX\DisableMediaPages\Modules;

use NPX\DisableMediaPages\Plugin;

class Restore
{
private static $instance = null;

public static function get_instance(): Restore
{
if (static::$instance === null) {
static::$instance = new static();
}

return static::$instance;
}

public function get_attachments_to_restore()
{
global $wpdb;

$result = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_name RLIKE '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$' ORDER BY post_date ASC;"
);

return $result;
}

public function restore_attachment($attachment_id)
{

$plugin = Plugin::get_instance();

$attachment = get_post($attachment_id);
$slug = $attachment->post_name;

$is_uuid = Plugin::is_uuid($slug);

if ($is_uuid) {
$new_slug = sanitize_title($attachment->post_title);

// Remove our filter so we get a real slug instead of UUID
remove_filter('wp_unique_post_slug', [$plugin, 'unique_slug'], 10);

$new_attachment = [
'ID' => $attachment->ID,
'post_name' => $new_slug,
];
wp_update_post($new_attachment);
}

}
}
6 changes: 6 additions & 0 deletions lib/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NPX\DisableMediaPages;

use NPX\DisableMediaPages\Modules\Admin;
use NPX\DisableMediaPages\Modules\CLI;
use NPX\DisableMediaPages\Modules\REST;
use WP_Query;
use WP_REST_Request;
Expand All @@ -28,13 +29,16 @@ public function init()
add_filter('template_redirect', [$this, 'set_404']);
add_filter('redirect_canonical', [$this, 'redirect_canonical'], 0, 2);
add_filter('attachment_link', [$this, 'change_attachment_link'], 10, 2);

error_log(print_r('init', true));
}

public function __construct()
{
add_filter('init', [$this, 'init']);
REST::get_instance();
Admin::get_instance();
CLI::get_instance();
}

public static function debug(...$messages)
Expand All @@ -46,6 +50,8 @@ public static function debug(...$messages)

function set_404()
{
error_log(print_r('set_404', true));

if (is_attachment()) {
global $wp_query;
$wp_query->set_404();
Expand Down

0 comments on commit 96d3dc3

Please sign in to comment.