-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve plugin structure, add new class for CLI commands
- Loading branch information
Showing
5 changed files
with
161 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters