@wp-operations/wp-ops 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/package.json +6 -2
- package/src/commands/platform.js +58 -2
- package/src/commands/start.js +2 -2
- package/src/main.js +19 -8
- package/wp-operations/CHANGELOG.md +14 -0
- package/wp-operations/README.md +39 -0
- package/wp-operations/agents/builder/AGENT.md +15 -0
- package/wp-operations/assets/admin.css +631 -0
- package/wp-operations/assets/admin.js +614 -0
- package/wp-operations/assets/app/app.css +1 -0
- package/wp-operations/assets/app/app.js +70 -0
- package/wp-operations/assets/app/fonts/inter-medium.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-regular.woff +0 -0
- package/wp-operations/assets/app/fonts/inter-semibold.woff +0 -0
- package/wp-operations/assets/app/login.css +42 -0
- package/wp-operations/assets/brand/icon.svg +20 -0
- package/wp-operations/assets/brand/mark.svg +19 -0
- package/wp-operations/assets/editors/audiobook.css +311 -0
- package/wp-operations/assets/editors/audiofile.css +286 -0
- package/wp-operations/assets/editors/course.css +701 -0
- package/wp-operations/assets/editors/course.js +698 -0
- package/wp-operations/assets/editors/faq.css +258 -0
- package/wp-operations/assets/editors/faq.js +196 -0
- package/wp-operations/assets/editors/interactive.css +220 -0
- package/wp-operations/assets/editors/interactive.js +378 -0
- package/wp-operations/assets/editors/media-sources.css +3 -0
- package/wp-operations/assets/editors/media-sources.js +40 -0
- package/wp-operations/assets/editors/open-media.css +114 -0
- package/wp-operations/assets/editors/open-media.js +182 -0
- package/wp-operations/assets/editors/thumbnails.css +335 -0
- package/wp-operations/assets/editors/thumbnails.js +447 -0
- package/wp-operations/assets/editors/translator.css +818 -0
- package/wp-operations/functions.php +24 -0
- package/wp-operations/index.php +14 -0
- package/wp-operations/platform.json +1836 -0
- package/wp-operations/skills/ai-development-guardrails/SKILL.md +49 -0
- package/wp-operations/skills/gutenberg-reference/SKILL.md +45 -0
- package/wp-operations/skills/js-tooling-for-themes/SKILL.md +51 -0
- package/wp-operations/skills/tailwind-for-wp/SKILL.md +62 -0
- package/wp-operations/skills/wp-clean-classic-themes/SKILL.md +112 -0
- package/wp-operations/skills/wp-psr4-plugins/SKILL.md +2043 -0
- package/wp-operations/skills/wp-security/SKILL.md +90 -0
- package/wp-operations/src/Admin.php +78 -0
- package/wp-operations/src/App/Access.php +57 -0
- package/wp-operations/src/App/Admin.php +442 -0
- package/wp-operations/src/App/App.php +157 -0
- package/wp-operations/src/App/Apps.php +379 -0
- package/wp-operations/src/App/Bundle.php +111 -0
- package/wp-operations/src/App/Contents.php +676 -0
- package/wp-operations/src/App/Engine.php +554 -0
- package/wp-operations/src/App/Github.php +308 -0
- package/wp-operations/src/App/Module.php +23 -0
- package/wp-operations/src/App/Ops.php +470 -0
- package/wp-operations/src/App/Packages.php +289 -0
- package/wp-operations/src/App/Rest.php +539 -0
- package/wp-operations/src/App/Sandboxes.php +206 -0
- package/wp-operations/src/App/Store.php +104 -0
- package/wp-operations/src/App/Usage.php +268 -0
- package/wp-operations/src/Config.php +249 -0
- package/wp-operations/src/Content/Editor.php +223 -0
- package/wp-operations/src/Content/Fields.php +681 -0
- package/wp-operations/src/Content/Scoring.php +253 -0
- package/wp-operations/src/Content/Structure.php +293 -0
- package/wp-operations/src/Content/Types.php +150 -0
- package/wp-operations/src/Content/Uploads.php +50 -0
- package/wp-operations/src/Editors/AudiobookAuthor.php +61 -0
- package/wp-operations/src/Editors/AudiobookChapters.php +632 -0
- package/wp-operations/src/Editors/AudiobookCover.php +235 -0
- package/wp-operations/src/Editors/Audiofile.php +462 -0
- package/wp-operations/src/Editors/Course.php +784 -0
- package/wp-operations/src/Editors/Ebook.php +242 -0
- package/wp-operations/src/Editors/Editors.php +44 -0
- package/wp-operations/src/Editors/Faq.php +237 -0
- package/wp-operations/src/Editors/Files.php +139 -0
- package/wp-operations/src/Editors/Interactive/Builder.php +103 -0
- package/wp-operations/src/Editors/Interactive/Card.php +76 -0
- package/wp-operations/src/Editors/Interactive/Editor.php +42 -0
- package/wp-operations/src/Editors/Interactive/Field.php +272 -0
- package/wp-operations/src/Editors/Interactive/Map.php +84 -0
- package/wp-operations/src/Editors/Interactive/Models.php +227 -0
- package/wp-operations/src/Editors/Interactive/Report.php +59 -0
- package/wp-operations/src/Editors/Interactive/Save.php +323 -0
- package/wp-operations/src/Editors/Interactive/Scoring.php +244 -0
- package/wp-operations/src/Editors/Interactive/Structure.php +195 -0
- package/wp-operations/src/Editors/Legal.php +235 -0
- package/wp-operations/src/Editors/OpenMedia.php +140 -0
- package/wp-operations/src/Editors/OriginalTitle.php +112 -0
- package/wp-operations/src/Editors/Sources.php +28 -0
- package/wp-operations/src/Editors/Thumbnails/Admin.php +306 -0
- package/wp-operations/src/Editors/Thumbnails/Ajax.php +163 -0
- package/wp-operations/src/Editors/Thumbnails/Box.php +188 -0
- package/wp-operations/src/Editors/Thumbnails/Storage.php +228 -0
- package/wp-operations/src/Editors/Translator/Admin.php +913 -0
- package/wp-operations/src/Editors/Translator/Api.php +63 -0
- package/wp-operations/src/Editors/Translator/Bar.php +83 -0
- package/wp-operations/src/Editors/Translator/Fields.php +273 -0
- package/wp-operations/src/Editors/Translator/Storage.php +131 -0
- package/wp-operations/src/Editors/Translator/Thumbnail.php +215 -0
- package/wp-operations/src/Identity.php +104 -0
- package/wp-operations/src/Languages.php +45 -0
- package/wp-operations/src/Lockdown.php +85 -0
- package/wp-operations/src/Modules/Apps/Admin.php +472 -0
- package/wp-operations/src/Modules/Apps/Api.php +193 -0
- package/wp-operations/src/Modules/Apps/Apps.php +279 -0
- package/wp-operations/src/Modules/Apps/Formatters.php +478 -0
- package/wp-operations/src/Modules/Apps/Schemes.php +248 -0
- package/wp-operations/src/Modules/Apps/Table.php +101 -0
- package/wp-operations/src/Modules/Thumbnails.php +68 -0
- package/wp-operations/src/Modules/Translations.php +133 -0
- package/wp-operations/src/Platform.php +59 -0
- package/wp-operations/src/Setup.php +273 -0
- package/wp-operations/src/Status.php +99 -0
- package/wp-operations/src/Storage/Client.php +168 -0
- package/wp-operations/src/Storage/Offload.php +120 -0
- package/wp-operations/src/Storage/Settings.php +166 -0
- package/wp-operations/src/Storage/Storage.php +35 -0
- package/wp-operations/src/Updates.php +151 -0
- package/wp-operations/style.css +11 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors;
|
|
3
|
+
|
|
4
|
+
use Platform\Platform;
|
|
5
|
+
|
|
6
|
+
defined( 'ABSPATH' ) || exit;
|
|
7
|
+
|
|
8
|
+
final class OpenMedia {
|
|
9
|
+
|
|
10
|
+
public static function boot(): void {
|
|
11
|
+
add_action( 'rest_api_init', [ __CLASS__, 'register' ] );
|
|
12
|
+
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'assets' ], 20 );
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public static function assets( string $hook ): void {
|
|
16
|
+
if ( ! current_user_can( 'upload_files' ) || ! in_array( $hook, [ 'post.php', 'post-new.php', 'upload.php' ], true ) ) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
wp_enqueue_media();
|
|
21
|
+
wp_enqueue_style( 'opm', Platform::url( 'assets/editors/open-media.css' ), [], Platform::asset_version( 'assets/editors/open-media.css' ) );
|
|
22
|
+
wp_enqueue_script( 'opm', Platform::url( 'assets/editors/open-media.js' ), [ 'media-views' ], Platform::asset_version( 'assets/editors/open-media.js' ), true );
|
|
23
|
+
wp_localize_script( 'opm', 'opm', [ 'nonce' => wp_create_nonce( 'wp_rest' ), 'root' => esc_url_raw( rest_url( 'open-media/v1' ) ) ] );
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
public static function perm(): bool {
|
|
28
|
+
return current_user_can( 'upload_files' );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
public static function register(): void {
|
|
32
|
+
$shared = [
|
|
33
|
+
'permission_callback' => [ self::class, 'perm' ],
|
|
34
|
+
'show_in_index' => false,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
register_rest_route( 'open-media/v1', '/search', array_merge( $shared, [
|
|
38
|
+
'methods' => 'GET',
|
|
39
|
+
'callback' => [ self::class, 'search' ],
|
|
40
|
+
'args' => [
|
|
41
|
+
'q' => [ 'required' => true, 'sanitize_callback' => 'sanitize_text_field' ],
|
|
42
|
+
'page' => [ 'default' => 1, 'sanitize_callback' => 'absint' ],
|
|
43
|
+
],
|
|
44
|
+
] ) );
|
|
45
|
+
|
|
46
|
+
register_rest_route( 'open-media/v1', '/download', array_merge( $shared, [
|
|
47
|
+
'methods' => 'POST',
|
|
48
|
+
'callback' => [ self::class, 'download' ],
|
|
49
|
+
] ) );
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public static function search( \WP_REST_Request $r ): \WP_REST_Response|\WP_Error {
|
|
53
|
+
$remote = wp_remote_get(
|
|
54
|
+
add_query_arg( [
|
|
55
|
+
'q' => $r->get_param( 'q' ),
|
|
56
|
+
'page' => $r->get_param( 'page' ),
|
|
57
|
+
'page_size' => 20,
|
|
58
|
+
], 'https://api.openverse.org/v1/images/' ),
|
|
59
|
+
[ 'timeout' => 15, 'user-agent' => 'wp-operations' ]
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if ( is_wp_error( $remote ) ) {
|
|
63
|
+
return new \WP_Error( 'ov_error', $remote->get_error_message(), [ 'status' => 502 ] );
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
$code = (int) wp_remote_retrieve_response_code( $remote );
|
|
67
|
+
if ( $code !== 200 ) {
|
|
68
|
+
return new \WP_Error( 'ov_error', 'Openverse returned ' . $code, [ 'status' => 502 ] );
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
$body = json_decode( wp_remote_retrieve_body( $remote ), true );
|
|
72
|
+
|
|
73
|
+
return new \WP_REST_Response( [
|
|
74
|
+
'count' => absint( $body['result_count'] ?? 0 ),
|
|
75
|
+
'results' => array_map( static fn( $img ) => [
|
|
76
|
+
'thumb' => esc_url_raw( $img['thumbnail'] ?? $img['url'] ?? '' ),
|
|
77
|
+
'url' => esc_url_raw( $img['url'] ?? '' ),
|
|
78
|
+
'title' => sanitize_text_field( $img['title'] ?? '' ),
|
|
79
|
+
'creator' => sanitize_text_field( $img['creator'] ?? '' ),
|
|
80
|
+
], $body['results'] ?? [] ),
|
|
81
|
+
] );
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public static function download( \WP_REST_Request $r ): \WP_REST_Response|\WP_Error {
|
|
85
|
+
$url = esc_url_raw( $r->get_param( 'url' ) ?? '' );
|
|
86
|
+
$title = sanitize_text_field( $r->get_param( 'title' ) ?? '' );
|
|
87
|
+
$creator = sanitize_text_field( $r->get_param( 'creator' ) ?? '' );
|
|
88
|
+
|
|
89
|
+
if ( ! $url || ! str_starts_with( $url, 'https://' ) ) {
|
|
90
|
+
return new \WP_Error( 'bad_url', 'A valid HTTPS URL is required.', [ 'status' => 400 ] );
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
94
|
+
require_once ABSPATH . 'wp-admin/includes/image.php';
|
|
95
|
+
require_once ABSPATH . 'wp-admin/includes/media.php';
|
|
96
|
+
|
|
97
|
+
$remote = wp_remote_get( $url, [
|
|
98
|
+
'timeout' => 30,
|
|
99
|
+
'user-agent' => 'wp-operations',
|
|
100
|
+
] );
|
|
101
|
+
|
|
102
|
+
if ( is_wp_error( $remote ) ) {
|
|
103
|
+
return new \WP_Error( 'dl_fail', $remote->get_error_message(), [ 'status' => 502 ] );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
$body = wp_remote_retrieve_body( $remote );
|
|
107
|
+
if ( empty( $body ) ) {
|
|
108
|
+
return new \WP_Error( 'dl_empty', 'Image download returned empty body.', [ 'status' => 502 ] );
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
$ct = strtok( wp_remote_retrieve_header( $remote, 'content-type' ) ?: 'image/jpeg', ';' );
|
|
112
|
+
$ext = explode( '/', trim( $ct ) )[1] ?? 'jpg';
|
|
113
|
+
$ext = preg_replace( '/[^a-z0-9]/', '', strtolower( $ext ) );
|
|
114
|
+
$slug = $title ? sanitize_file_name( $title ) : 'openverse-' . time();
|
|
115
|
+
$bits = wp_upload_bits( $slug . '.' . $ext, null, $body );
|
|
116
|
+
|
|
117
|
+
if ( ! empty( $bits['error'] ) ) {
|
|
118
|
+
return new \WP_Error( 'upload_fail', $bits['error'], [ 'status' => 500 ] );
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
$att_id = wp_insert_attachment( [
|
|
122
|
+
'post_title' => $title ?: $slug,
|
|
123
|
+
'post_mime_type' => $ct,
|
|
124
|
+
'post_status' => 'inherit',
|
|
125
|
+
], $bits['file'] );
|
|
126
|
+
|
|
127
|
+
if ( is_wp_error( $att_id ) ) return $att_id;
|
|
128
|
+
|
|
129
|
+
if ( $creator ) {
|
|
130
|
+
update_post_meta( $att_id, '_wp_attachment_image_alt', 'Photo by ' . $creator );
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
$meta = wp_generate_attachment_metadata( $att_id, $bits['file'] );
|
|
134
|
+
if ( ! empty( $meta ) ) {
|
|
135
|
+
wp_update_attachment_metadata( $att_id, $meta );
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return new \WP_REST_Response( wp_prepare_attachment_for_js( $att_id ) );
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors;
|
|
3
|
+
|
|
4
|
+
defined( 'ABSPATH' ) || exit;
|
|
5
|
+
|
|
6
|
+
final class OriginalTitle {
|
|
7
|
+
public function __construct() {
|
|
8
|
+
add_action('add_meta_boxes', array($this, 'add_original_title_metabox'));
|
|
9
|
+
add_action('save_post', array($this, 'save_original_title_meta'));
|
|
10
|
+
add_filter('manage_audiobook_posts_columns', array($this, 'add_original_title_column'));
|
|
11
|
+
add_action('manage_audiobook_posts_custom_column', array($this, 'display_original_title_column'), 10, 2);
|
|
12
|
+
add_filter('manage_edit-audiobook_sortable_columns', array($this, 'make_original_title_sortable'));
|
|
13
|
+
add_action('pre_get_posts', array($this, 'original_title_orderby'));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public function add_original_title_metabox() {
|
|
17
|
+
add_meta_box(
|
|
18
|
+
'original_title_meta',
|
|
19
|
+
__( 'Original Title' ),
|
|
20
|
+
array($this, 'original_title_metabox_callback'),
|
|
21
|
+
'audiobook',
|
|
22
|
+
'normal',
|
|
23
|
+
'high'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public function original_title_metabox_callback($post) {
|
|
28
|
+
wp_nonce_field('original_title_nonce', 'original_title_nonce_field');
|
|
29
|
+
|
|
30
|
+
$original_title = get_post_meta($post->ID, '_original_title', true);
|
|
31
|
+
?>
|
|
32
|
+
<table class="form-table">
|
|
33
|
+
<tr>
|
|
34
|
+
<th scope="row">
|
|
35
|
+
<label for="original_title"><?php _e( 'Original Title:' ); ?></label>
|
|
36
|
+
</th>
|
|
37
|
+
<td>
|
|
38
|
+
<input type="text"
|
|
39
|
+
id="original_title"
|
|
40
|
+
name="original_title"
|
|
41
|
+
value="<?php echo esc_attr($original_title); ?>"
|
|
42
|
+
class="regular-text"
|
|
43
|
+
placeholder="<?php _e( 'Enter the original title of the audiobook' ); ?>" />
|
|
44
|
+
<p class="description">
|
|
45
|
+
<?php _e( 'The original title of this audiobook (if different from the current title/different language).' ); ?>
|
|
46
|
+
</p>
|
|
47
|
+
</td>
|
|
48
|
+
</tr>
|
|
49
|
+
</table>
|
|
50
|
+
<?php
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public function save_original_title_meta($post_id) {
|
|
54
|
+
if (!isset($_POST['original_title_nonce_field']) ||
|
|
55
|
+
!wp_verify_nonce($_POST['original_title_nonce_field'], 'original_title_nonce')) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (!current_user_can('edit_post', $post_id)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (isset($_POST['original_title'])) {
|
|
68
|
+
$original_title = sanitize_text_field($_POST['original_title']);
|
|
69
|
+
update_post_meta($post_id, '_original_title', $original_title);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public function add_original_title_column($columns) {
|
|
74
|
+
$new_columns = array();
|
|
75
|
+
foreach ($columns as $key => $value) {
|
|
76
|
+
$new_columns[$key] = $value;
|
|
77
|
+
if ($key === 'title') {
|
|
78
|
+
$new_columns['original_title'] = __( 'Original Title' );
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return $new_columns;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public function display_original_title_column($column, $post_id) {
|
|
85
|
+
if ($column === 'original_title') {
|
|
86
|
+
$original_title = get_post_meta($post_id, '_original_title', true);
|
|
87
|
+
if (!empty($original_title)) {
|
|
88
|
+
echo esc_html($original_title);
|
|
89
|
+
} else {
|
|
90
|
+
echo '<span style="color: #999;">—</span>';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public function make_original_title_sortable($columns) {
|
|
96
|
+
$columns['original_title'] = 'original_title';
|
|
97
|
+
return $columns;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
public function original_title_orderby($query) {
|
|
101
|
+
if (!is_admin()) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
$orderby = $query->get('orderby');
|
|
106
|
+
|
|
107
|
+
if ($orderby === 'original_title') {
|
|
108
|
+
$query->set('meta_key', '_original_title');
|
|
109
|
+
$query->set('orderby', 'meta_value');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors;
|
|
3
|
+
|
|
4
|
+
use Platform\Config;
|
|
5
|
+
use Platform\Platform;
|
|
6
|
+
|
|
7
|
+
defined( 'ABSPATH' ) || exit;
|
|
8
|
+
|
|
9
|
+
final class Sources {
|
|
10
|
+
|
|
11
|
+
const KINDS = [ 'course' ];
|
|
12
|
+
|
|
13
|
+
public static function boot(): void {
|
|
14
|
+
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'assets' ], 30 );
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public static function assets( string $hook ): void {
|
|
18
|
+
$type = Config::type( (string) get_post_type() ?: (string) ( $_GET['post_type'] ?? '' ) );
|
|
19
|
+
|
|
20
|
+
if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) || ! $type || ! in_array( $type['editor'] ?? '', self::KINDS, true ) ) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
wp_enqueue_media();
|
|
25
|
+
wp_enqueue_style( 'platform-media-sources', Platform::url( 'assets/editors/media-sources.css' ), [], Platform::asset_version( 'assets/editors/media-sources.css' ) );
|
|
26
|
+
wp_enqueue_script( 'platform-media-sources', Platform::url( 'assets/editors/media-sources.js' ), [ 'jquery', 'media-views' ], Platform::asset_version( 'assets/editors/media-sources.js' ), true );
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
namespace Platform\Editors\Thumbnails;
|
|
3
|
+
|
|
4
|
+
use Platform\Config;
|
|
5
|
+
use Platform\Languages;
|
|
6
|
+
use Platform\Platform;
|
|
7
|
+
|
|
8
|
+
defined( 'ABSPATH' ) || exit;
|
|
9
|
+
|
|
10
|
+
class Admin {
|
|
11
|
+
public static function register(): void {
|
|
12
|
+
add_action( 'admin_menu', [ __CLASS__, 'register_pages' ] );
|
|
13
|
+
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_assets' ] );
|
|
14
|
+
add_action( 'admin_post_thumbnails_save_group', [ __CLASS__, 'handle_save_group' ] );
|
|
15
|
+
add_action( 'admin_init', [ __CLASS__, 'handle_delete_group' ] );
|
|
16
|
+
add_action( 'admin_init', [ __CLASS__, 'handle_clone_group' ] );
|
|
17
|
+
add_filter( 'parent_file', [ __CLASS__, 'set_active_parent' ] );
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static function set_active_parent( string $parent_file ): string {
|
|
21
|
+
global $plugin_page;
|
|
22
|
+
if ( $plugin_page === 'thumbnail-group-form' ) {
|
|
23
|
+
$plugin_page = 'thumbnails-manager';
|
|
24
|
+
}
|
|
25
|
+
return $parent_file;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public static function register_pages(): void {
|
|
29
|
+
$module = (array) Config::module( 'thumbnails' );
|
|
30
|
+
$label = (string) ( $module['label'] ?? 'Thumbnails' );
|
|
31
|
+
add_menu_page(
|
|
32
|
+
$label,
|
|
33
|
+
$label,
|
|
34
|
+
'manage_options',
|
|
35
|
+
'thumbnails-manager',
|
|
36
|
+
[ __CLASS__, 'render_list' ],
|
|
37
|
+
(string) ( $module['icon'] ?? 'dashicons-format-image' ),
|
|
38
|
+
$module['position'] ?? 78
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
add_submenu_page(
|
|
42
|
+
null,
|
|
43
|
+
'Add / Edit Group',
|
|
44
|
+
'Add / Edit Group',
|
|
45
|
+
'manage_options',
|
|
46
|
+
'thumbnail-group-form',
|
|
47
|
+
[ __CLASS__, 'render_form' ]
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public static function enqueue_assets( string $hook ): void {
|
|
52
|
+
$pages = [ 'toplevel_page_thumbnails-manager', 'admin_page_thumbnail-group-form' ];
|
|
53
|
+
if ( ! in_array( $hook, $pages, true ) ) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
wp_enqueue_media();
|
|
58
|
+
|
|
59
|
+
$css_ver = Platform::asset_version( 'assets/editors/thumbnails.css' );
|
|
60
|
+
$js_ver = Platform::asset_version( 'assets/editors/thumbnails.js' );
|
|
61
|
+
|
|
62
|
+
wp_enqueue_style(
|
|
63
|
+
'thumbnails-admin',
|
|
64
|
+
Platform::url( 'assets/editors/thumbnails.css' ),
|
|
65
|
+
[],
|
|
66
|
+
$css_ver
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
wp_enqueue_script(
|
|
70
|
+
'thumbnails-admin',
|
|
71
|
+
Platform::url( 'assets/editors/thumbnails.js' ),
|
|
72
|
+
[ 'jquery' ],
|
|
73
|
+
$js_ver,
|
|
74
|
+
true
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
$languages = Languages::all();
|
|
78
|
+
$site_lang = Languages::default();
|
|
79
|
+
|
|
80
|
+
$taxonomies = [];
|
|
81
|
+
foreach ( self::get_service_post_types() as $slug => $obj ) {
|
|
82
|
+
$taxs = get_object_taxonomies( $slug, 'objects' );
|
|
83
|
+
$taxonomies[ $slug ] = [];
|
|
84
|
+
foreach ( $taxs as $tax_slug => $tax_obj ) {
|
|
85
|
+
$terms = get_terms( [ 'taxonomy' => $tax_slug, 'hide_empty' => false ] );
|
|
86
|
+
$term_list = [];
|
|
87
|
+
if ( ! is_wp_error( $terms ) ) {
|
|
88
|
+
$term_list = array_map( fn( $t ) => [
|
|
89
|
+
'id' => (int) $t->term_id,
|
|
90
|
+
'name' => $t->name,
|
|
91
|
+
], $terms );
|
|
92
|
+
}
|
|
93
|
+
$taxonomies[ $slug ][ $tax_slug ] = [
|
|
94
|
+
'label' => $tax_obj->label,
|
|
95
|
+
'terms' => $term_list,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
wp_localize_script( 'thumbnails-admin', 'ThumbnailsData', [
|
|
101
|
+
'ajax_url' => admin_url( 'admin-ajax.php' ),
|
|
102
|
+
'nonce' => wp_create_nonce( 'thumbnails_ajax' ),
|
|
103
|
+
'languages' => $languages,
|
|
104
|
+
'site_lang' => $site_lang,
|
|
105
|
+
'taxonomies' => $taxonomies,
|
|
106
|
+
] );
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public static function render_list(): void {
|
|
110
|
+
$groups = Storage::get_groups();
|
|
111
|
+
?>
|
|
112
|
+
<div class="wrap">
|
|
113
|
+
<h1 class="wp-heading-inline">Thumbnail Groups</h1>
|
|
114
|
+
<a href="<?php echo esc_url( admin_url( 'admin.php?page=thumbnail-group-form&add=1' ) ); ?>" class="page-title-action">Add New Group</a>
|
|
115
|
+
<hr class="wp-header-end">
|
|
116
|
+
|
|
117
|
+
<?php if ( empty( $groups ) ) : ?>
|
|
118
|
+
<p>No groups yet.</p>
|
|
119
|
+
<?php else : ?>
|
|
120
|
+
<table class="wp-list-table widefat fixed striped">
|
|
121
|
+
<thead>
|
|
122
|
+
<tr>
|
|
123
|
+
<th>Name</th>
|
|
124
|
+
<th>Created</th>
|
|
125
|
+
<th>Actions</th>
|
|
126
|
+
</tr>
|
|
127
|
+
</thead>
|
|
128
|
+
<tbody>
|
|
129
|
+
<?php foreach ( $groups as $g ) : ?>
|
|
130
|
+
<tr>
|
|
131
|
+
<td><strong><?php echo esc_html( $g['name'] ); ?></strong></td>
|
|
132
|
+
<td><?php echo esc_html( $g['created_at'] ); ?></td>
|
|
133
|
+
<td>
|
|
134
|
+
<a href="<?php echo esc_url( admin_url( 'admin.php?page=thumbnail-group-form&id=' . (int) $g['id'] ) ); ?>">Edit</a>
|
|
135
|
+
|
|
|
136
|
+
<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=thumbnails-manager&clone=' . (int) $g['id'] ), 'clone_group_' . $g['id'] ) ); ?>">Clone</a>
|
|
137
|
+
|
|
|
138
|
+
<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=thumbnails-manager&delete=' . (int) $g['id'] ), 'delete_group_' . $g['id'] ) ); ?>"
|
|
139
|
+
onclick="return confirm('Delete this group and all its thumbnails?')">Delete</a>
|
|
140
|
+
</td>
|
|
141
|
+
</tr>
|
|
142
|
+
<?php endforeach; ?>
|
|
143
|
+
</tbody>
|
|
144
|
+
</table>
|
|
145
|
+
<?php endif; ?>
|
|
146
|
+
</div>
|
|
147
|
+
<?php
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public static function handle_save_group(): void {
|
|
151
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
152
|
+
wp_die( 'Insufficient permissions.' );
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
$id = (int) ( $_POST['group_id'] ?? 0 );
|
|
156
|
+
$name = sanitize_text_field( wp_unslash( $_POST['name'] ?? '' ) );
|
|
157
|
+
|
|
158
|
+
if ( $name === '' ) {
|
|
159
|
+
wp_die( 'Name is required.' );
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if ( $id ) {
|
|
163
|
+
Storage::update_group( $id, $name );
|
|
164
|
+
} else {
|
|
165
|
+
$id = Storage::insert_group( $name );
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
wp_safe_redirect( admin_url( 'admin.php?page=thumbnail-group-form&id=' . $id . '&saved=1' ) );
|
|
169
|
+
exit;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
public static function handle_clone_group(): void {
|
|
173
|
+
if ( empty( $_GET['page'] ) || $_GET['page'] !== 'thumbnails-manager' || empty( $_GET['clone'] ) ) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
$id = (int) $_GET['clone'];
|
|
178
|
+
if ( ! wp_verify_nonce( $_GET['_wpnonce'] ?? '', 'clone_group_' . $id ) ) {
|
|
179
|
+
wp_die( 'Security check failed.' );
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
183
|
+
wp_die( 'Insufficient permissions.' );
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
$new_id = Storage::clone_group( $id );
|
|
187
|
+
if ( $new_id ) {
|
|
188
|
+
wp_safe_redirect( admin_url( 'admin.php?page=thumbnail-group-form&id=' . $new_id ) );
|
|
189
|
+
} else {
|
|
190
|
+
wp_safe_redirect( admin_url( 'admin.php?page=thumbnails-manager' ) );
|
|
191
|
+
}
|
|
192
|
+
exit;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public static function handle_delete_group(): void {
|
|
196
|
+
if ( empty( $_GET['page'] ) || $_GET['page'] !== 'thumbnails-manager' || empty( $_GET['delete'] ) ) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
$id = (int) $_GET['delete'];
|
|
201
|
+
if ( ! wp_verify_nonce( $_GET['_wpnonce'] ?? '', 'delete_group_' . $id ) ) {
|
|
202
|
+
wp_die( 'Security check failed.' );
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if ( ! current_user_can( 'manage_options' ) ) {
|
|
206
|
+
wp_die( 'Insufficient permissions.' );
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
Storage::delete_group( $id );
|
|
210
|
+
wp_safe_redirect( admin_url( 'admin.php?page=thumbnails-manager' ) );
|
|
211
|
+
exit;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
public static function render_form(): void {
|
|
215
|
+
$id = (int) ( $_GET['id'] ?? 0 );
|
|
216
|
+
$add = ! empty( $_GET['add'] );
|
|
217
|
+
|
|
218
|
+
$group = $id ? Storage::get_group( $id ) : null;
|
|
219
|
+
|
|
220
|
+
if ( $id && ! $group ) {
|
|
221
|
+
echo '<div class="wrap"><h1>Group not found.</h1></div>';
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
$title = $add ? 'Add New Group' : 'Edit Group';
|
|
226
|
+
|
|
227
|
+
$post_types = self::get_service_post_types();
|
|
228
|
+
|
|
229
|
+
?>
|
|
230
|
+
<div class="wrap">
|
|
231
|
+
<h1><?php echo esc_html( $title ); ?></h1>
|
|
232
|
+
|
|
233
|
+
<?php if ( ! empty( $_GET['saved'] ) ) : ?>
|
|
234
|
+
<div class="notice notice-success is-dismissible"><p>Group saved.</p></div>
|
|
235
|
+
<?php endif; ?>
|
|
236
|
+
|
|
237
|
+
<?php if ( $add ) : ?>
|
|
238
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
|
239
|
+
<?php wp_nonce_field( 'thumbnails_save_group', '_thnonce' ); ?>
|
|
240
|
+
<input type="hidden" name="action" value="thumbnails_save_group">
|
|
241
|
+
|
|
242
|
+
<table class="form-table">
|
|
243
|
+
<tr>
|
|
244
|
+
<th><label for="group_name">Group Name</label></th>
|
|
245
|
+
<td><input type="text" id="group_name" name="name" value="" class="regular-text" required></td>
|
|
246
|
+
</tr>
|
|
247
|
+
</table>
|
|
248
|
+
|
|
249
|
+
<?php submit_button( 'Create Group' ); ?>
|
|
250
|
+
</form>
|
|
251
|
+
|
|
252
|
+
<?php else :
|
|
253
|
+
$group_id = (int) $group['id'];
|
|
254
|
+
?>
|
|
255
|
+
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
|
|
256
|
+
<?php wp_nonce_field( 'thumbnails_save_group', '_thnonce' ); ?>
|
|
257
|
+
<input type="hidden" name="action" value="thumbnails_save_group">
|
|
258
|
+
<input type="hidden" name="group_id" value="<?php echo $group_id; ?>">
|
|
259
|
+
|
|
260
|
+
<table class="form-table">
|
|
261
|
+
<tr>
|
|
262
|
+
<th><label for="group_name">Group Name</label></th>
|
|
263
|
+
<td><input type="text" id="group_name" name="name" value="<?php echo esc_attr( $group['name'] ); ?>" class="regular-text" required></td>
|
|
264
|
+
</tr>
|
|
265
|
+
</table>
|
|
266
|
+
|
|
267
|
+
<?php submit_button( 'Update Group' ); ?>
|
|
268
|
+
</form>
|
|
269
|
+
|
|
270
|
+
<hr>
|
|
271
|
+
|
|
272
|
+
<div id="thumbnails-group-app" data-group-id="<?php echo $group_id; ?>">
|
|
273
|
+
<nav class="thumbnails-tabs">
|
|
274
|
+
<?php foreach ( $post_types as $slug => $type_obj ) : ?>
|
|
275
|
+
<a href="#" class="thumbnails-tab" data-post-type="<?php echo esc_attr( $slug ); ?>">
|
|
276
|
+
<?php echo esc_html( $type_obj->labels->name ); ?>
|
|
277
|
+
</a>
|
|
278
|
+
<?php endforeach; ?>
|
|
279
|
+
</nav>
|
|
280
|
+
|
|
281
|
+
<div class="thumbnails-search-bar">
|
|
282
|
+
<input type="text" id="thumbnails-search" placeholder="Search posts..." class="regular-text">
|
|
283
|
+
<div id="thumbnails-tax-filters" class="thumbnails-tax-filters"></div>
|
|
284
|
+
</div>
|
|
285
|
+
|
|
286
|
+
<div id="thumbnails-posts-container">
|
|
287
|
+
<p class="thumbnails-loading">Select a tab to load posts...</p>
|
|
288
|
+
</div>
|
|
289
|
+
</div>
|
|
290
|
+
|
|
291
|
+
<?php endif; ?>
|
|
292
|
+
</div>
|
|
293
|
+
<?php
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public static function get_service_post_types(): array {
|
|
297
|
+
$types = get_post_types( [], 'objects' );
|
|
298
|
+
$result = [];
|
|
299
|
+
foreach ( $types as $slug => $obj ) {
|
|
300
|
+
if ( ! empty( $obj->service_content ) ) {
|
|
301
|
+
$result[ $slug ] = $obj;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return $result;
|
|
305
|
+
}
|
|
306
|
+
}
|