Skip to content

Commit 613841c

Browse files
chore: addredd PR feedback
Co-authored-by: Utsav Patel <[email protected]> Co-authored-by: Vishal Kakadiya <[email protected]>
1 parent 831a020 commit 613841c

File tree

15 files changed

+246
-158
lines changed

15 files changed

+246
-158
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
![Banner V3](https://rtcamp.com/wp-content/uploads/sites/2/2024/09/OneDesign-Banner.png)
1+
![Banner V3](./assets/images/banner.webp)
22

33
# OneDesign
4-
Contributors: [rtcamp](https://profiles.wordpress.org/rtcamp), [parthnvaswani](https://github.com/parthnvaswani), [up1512001](https://github.com/up1512001), [singhakanshu00](https://github.com/singhakanshu00), [danish17](https://github.com/danish17), [aviral-mittal](https://github.com/aviral-mittal), [vaishaliagola27](https://github.com/vaishaliagola27), [rishavjeet](https://github.com/rishavjeet), [vishal4669](https://github.com/vishal4669), [iamimmanuelraj](https://github.com/iamimmanuelraj)
4+
Contributors: [rtcamp](https://profiles.wordpress.org/rtcamp), [parthnvaswani](https://github.com/parthnvaswani), [up1512001](https://github.com/up1512001), [singhakanshu00](https://github.com/singhakanshu00), [danish17](https://github.com/danish17), [aviral-mittal](https://github.com/aviral-mittal), [vaishaliagola27](https://github.com/vaishaliagola27), [rishavjeet](https://github.com/rishavjeet), [vishal4669](https://github.com/vishal4669), [iamimmanuelraj](https://github.com/iamimmanuelraj) [vishalkakadiya](https://github.com/vishalkakadiya)
55

66
Tags: OnePress, Pattern distribution, Pattern sync, OneDesign, WordPress multisite, WordPress network, Gutenberg, WordPress Site Editor, Block Patterns, Pattern management, WordPress plugin, Design consistency, Pattern library
77

assets/images/banner.webp

184 KB
Loading

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"inc/"
88
],
99
"files": [
10-
"./inc/helpers/custom-functions.php"
10+
"./inc/helpers/custom-functions.php",
11+
"./inc/helpers/template-functions.php"
1112
]
1213
},
1314
"authors": [

inc/classes/class-assets.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ class Assets {
2121
*/
2222
use Singleton;
2323

24+
/**
25+
* Localized data for scripts.
26+
*
27+
* @var array
28+
*/
29+
private static array $localized_data = array();
30+
2431
/**
2532
* Protected class constructor
2633
*/
2734
protected function __construct() {
2835
$this->setup_hooks();
36+
self::build_localized_data();
2937
}
3038

3139
/**
@@ -36,6 +44,20 @@ public function setup_hooks(): void {
3644
add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ), 20, 1 );
3745
}
3846

47+
/**
48+
* Prepare localized data.
49+
*
50+
* @return void
51+
*/
52+
private static function build_localized_data(): void {
53+
self::$localized_data = array(
54+
'restUrl' => esc_url( home_url( '/wp-json' ) ),
55+
'restNonce' => wp_create_nonce( 'wp_rest' ),
56+
'apiKey' => get_option( Constants::ONEDESIGN_API_KEY, 'default_api_key' ),
57+
'settingsLink' => esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ),
58+
);
59+
}
60+
3961
/**
4062
* Add admin scripts.
4163
*
@@ -58,10 +80,7 @@ public function add_admin_scripts( $hook_suffix ): void {
5880
wp_localize_script(
5981
'onedesign-settings-script',
6082
'OneDesignSettings',
61-
array(
62-
'restUrl' => esc_url( home_url( '/wp-json' ) ),
63-
'restNonce' => wp_create_nonce( 'wp_rest' ),
64-
)
83+
self::$localized_data
6584
);
6685

6786
wp_enqueue_script( 'onedesign-settings-script' );
@@ -89,12 +108,7 @@ public function add_admin_scripts( $hook_suffix ): void {
89108
wp_localize_script(
90109
'onedesign-setup-script',
91110
'OneDesignSettings',
92-
array(
93-
'restUrl' => esc_url( home_url( '/wp-json' ) ),
94-
'apiKey' => get_option( Constants::ONEDESIGN_API_KEY, 'default_api_key' ),
95-
'restNonce' => wp_create_nonce( 'wp_rest' ),
96-
'settingsLink' => esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ),
97-
)
111+
self::$localized_data
98112
);
99113

100114
wp_enqueue_script( 'onedesign-setup-script' );
@@ -124,15 +138,7 @@ public function enqueue_scripts(): void {
124138
wp_localize_script(
125139
'onedesign-patterns-library-script',
126140
'patternSyncData',
127-
array(
128-
'ajaxurl' => admin_url( 'admin-ajax.php' ),
129-
'nonce' => wp_create_nonce( 'onedesign_nonce' ),
130-
'siteUrl' => home_url(),
131-
'adminUrl' => admin_url(),
132-
'settingsLink' => esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ),
133-
'restUrl' => esc_url( home_url( '/wp-json' ) ),
134-
'restNonce' => wp_create_nonce( 'wp_rest' ),
135-
)
141+
self::$localized_data
136142
);
137143

138144
wp_enqueue_script( 'onedesign-patterns-library-script' );
@@ -151,14 +157,7 @@ public function enqueue_scripts(): void {
151157
wp_localize_script(
152158
'onedesign-templates-library-script',
153159
'TemplateLibraryData',
154-
array(
155-
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
156-
'restNonce' => wp_create_nonce( 'wp_rest' ),
157-
'siteUrl' => home_url(),
158-
'adminUrl' => admin_url(),
159-
'restUrl' => esc_url( home_url( '/wp-json' ) ),
160-
'settingsLink' => esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ),
161-
)
160+
self::$localized_data
162161
);
163162

164163
wp_enqueue_script( 'onedesign-templates-library-script' );
@@ -256,7 +255,7 @@ public function register_style( $handle, $file, $deps = array(), $ver = false, $
256255
* @param string $file File path.
257256
* @param int|string|boolean $ver File version.
258257
*
259-
* @return bool|false|int
258+
* @return bool|int|string
260259
*/
261260
public function get_file_version( $file, $ver = false ): bool|int|string {
262261
if ( ! empty( $ver ) ) {

inc/classes/class-cpt-restriction.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,21 @@ public function setup_hooks(): void {
4545
add_filter( 'register_post_type_args', array( $this, 'modify_design_library_labels' ), 10, 2 );
4646
add_action( 'admin_menu', array( $this, 'modify_design_library_admin_menu' ), 999 );
4747
add_filter( 'default_content', array( $this, 'add_default_content_to_editor' ), 10, 2 );
48-
add_filter(
49-
'default_title',
50-
function ( $title ) {
51-
if ( Design_Library::SLUG === get_current_screen()->post_type ) {
52-
return esc_html__( 'Design Library', 'onedesign' );
53-
}
54-
return $title;
55-
}
56-
);
48+
add_filter( 'default_title', array( $this, 'add_default_title_to_editor' ), 10, 2 );
49+
}
50+
51+
/**
52+
* Callback function to add default title to the editor.
53+
*
54+
* @param string $title The default title.
55+
*
56+
* @return string Modified title.
57+
*/
58+
public function add_default_title_to_editor( string $title ): string {
59+
if ( Design_Library::SLUG === get_current_screen()->post_type ) {
60+
return esc_html__( 'Design Library', 'onedesign' );
61+
}
62+
return $title;
5763
}
5864

5965
/**
@@ -167,7 +173,13 @@ public function limit_template_posts(): void {
167173

168174
// Count existing design library posts.
169175
$existing_posts = wp_count_posts( Template::SLUG );
170-
$post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private;
176+
177+
// check if $existing_posts is not null to avoid errors.
178+
if ( ! $existing_posts ) {
179+
return;
180+
}
181+
182+
$post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private;
171183

172184
// If a post already exists, redirect to edit screen.
173185
if ( $post_count > 0 ) {
@@ -236,7 +248,13 @@ public function modify_design_library_admin_menu(): void {
236248

237249
// Count existing design library posts.
238250
$existing_posts = wp_count_posts( Design_Library::SLUG );
239-
$post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private;
251+
252+
// check if $existing_posts is not null to avoid errors.
253+
if ( ! $existing_posts ) {
254+
return;
255+
}
256+
257+
$post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private;
240258

241259
if ( $post_count > 0 ) {
242260
// Find the "Add New" menu item.

inc/classes/class-hooks.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,6 @@ public function create_template(): void {
295295
);
296296
}
297297
update_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, array_unique( $brand_site_post_ids ), false );
298-
299-
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
300-
foreach ( $logs as $log_entry ) {
301-
error_log( $log_entry ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- only adding logs in debug mode.
302-
}
303-
}
304298
}
305299

306300
/**

inc/classes/class-utils.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public function setup_hooks(): void {
3636
// Add any hooks if needed in the future.
3737
}
3838

39+
/**
40+
* Build OneDesign REST namespace.
41+
*
42+
* @var string
43+
*/
44+
const NAMESPACE = Constants::ONEDESIGN_REST_NAMESPACE . '/' . Constants::ONEDESIGN_REST_VERSION;
45+
3946
/**
4047
* Get the current site type.
4148
*
@@ -64,6 +71,19 @@ public static function is_governing_site(): bool {
6471
return hash_equals( 'governing-site', self::get_current_site_type() );
6572
}
6673

74+
/**
75+
* Build API endpoint URL.
76+
*
77+
* @param string $site_url The base URL of the site.
78+
* @param string $endpoint The specific endpoint path.
79+
* @param string $rest_namespace The REST namespace (default: self::NAMESPACE).
80+
*
81+
* @return string Full API endpoint URL.
82+
*/
83+
public static function build_api_endpoint( string $site_url, string $endpoint, string $rest_namespace = self::NAMESPACE ): string {
84+
return esc_url_raw( trailingslashit( $site_url ) ) . '/wp-json/' . $rest_namespace . '/' . ltrim( $endpoint, '/' );
85+
}
86+
6787
/**
6888
* Check if two URLs belong to the same domain.
6989
*
@@ -155,7 +175,7 @@ public static function generate_unique_slug_for_template_patterns_template_parts
155175
* @param string|array|\WP_Block_Template $content The block content containing WordPress block markup.
156176
* @param string $shared_site_name The name of the site to which template is going to be shared.
157177
*
158-
* @return string Modified content with updated slugs and themes.
178+
* @return array|string|null Modified content with updated slugs and themes.
159179
*/
160180
public static function modify_content_references( string|array|\WP_Block_Template $content, string $shared_site_name ): array|string|null {
161181

inc/classes/plugin-configs/class-constants.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ class Constants {
108108
*/
109109
public const ONEDESIGN_SHARED_SYNCED_PATTERNS = 'onedesign_shared_synced_patterns';
110110

111+
/**
112+
* OneDesign rest namespace.
113+
*
114+
* @var string
115+
*/
116+
public const ONEDESIGN_REST_NAMESPACE = 'onedesign';
117+
118+
/**
119+
* OneDesign rest version.
120+
*
121+
* @var string
122+
*/
123+
public const ONEDESIGN_REST_VERSION = 'v1';
124+
111125
/**
112126
* Use Singleton trait.
113127
*/

inc/classes/rest/class-patterns.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Patterns {
2828
/**
2929
* REST namespace.
3030
*/
31-
const NAMESPACE = 'onedesign/v1';
31+
const NAMESPACE = Utils::NAMESPACE;
3232

3333
/**
3434
* Constructor.
@@ -259,7 +259,7 @@ public function request_remove_brand_site_patterns( WP_REST_Request $request ):
259259
return new WP_Error( 'site_not_found', __( 'Target site not found in configuration.', 'onedesign' ), array( 'status' => 404 ) );
260260
}
261261

262-
$remote_url = trailingslashit( $site['url'] ) . 'wp-json/onedesign/v1/remove-brand-site-patterns';
262+
$remote_url = Utils::build_api_endpoint( $site['url'], 'remove-brand-site-patterns' );
263263

264264
$response = wp_safe_remote_request(
265265
$remote_url,
@@ -366,10 +366,11 @@ public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response {
366366
}
367367

368368
$all_patterns = array();
369+
$error_logs = array();
369370
foreach ( $child_sites as $site ) {
370371
$site_patterns = array();
371372
$remote_api_key = $site['api_key'] ?? '';
372-
$remote_url = trailingslashit( $site['url'] ) . 'wp-json/onedesign/v1/brand-site-patterns?timestamp=' . time(); // Add timestamp to avoid caching issues.
373+
$remote_url = Utils::build_api_endpoint( $site['url'], 'brand-site-patterns?timestamp=' . time() ); // Add timestamp to avoid caching issues.
373374

374375
if ( empty( $remote_api_key ) ) {
375376
continue; // Skip sites without API key.
@@ -420,25 +421,20 @@ public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response {
420421
} else {
421422
// Log or handle error response from a child site.
422423
$error_message = $decoded_body['message'] ?? __( 'Unknown error from remote site.', 'onedesign' );
423-
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
424-
error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- showing in dev mode only.
425-
sprintf(
426-
// translators: %1$s is the site name, %2$s is the error message, %3$d is the HTTP status code.
427-
__( 'Error fetching patterns from %1$s: %2$s (Status: %3$d)', 'onedesign' ),
428-
$site['name'] ?? __( 'Unknown Site', 'onedesign' ),
429-
$error_message,
430-
$status_code
431-
)
432-
);
433-
}
424+
$error_logs[] = array(
425+
'site' => $site['name'] ?? __( 'Unknown Site', 'onedesign' ),
426+
'status_code' => $status_code,
427+
'message' => $error_message,
428+
);
434429
}
435430
$all_patterns[ $site['id'] ] = $site_patterns; // Store patterns by site ID.
436431
}
437432

438433
return new WP_REST_Response(
439434
array(
440-
'success' => true,
441-
'patterns' => $all_patterns,
435+
'success' => true,
436+
'patterns' => $all_patterns,
437+
'error_logs' => $error_logs,
442438
),
443439
200
444440
);
@@ -632,7 +628,7 @@ function ( $site ) use ( $site_id ) {
632628

633629
// The 'api_key' in $target_site is the token for the remote child site.
634630
$remote_api_key = $target_site['api_key'] ?? '';
635-
$remote_url = trailingslashit( $target_site['url'] ) . 'wp-json/onedesign/v1/receive-patterns';
631+
$remote_url = Utils::build_api_endpoint( $target_site['url'], 'receive-patterns' );
636632

637633
if ( empty( $remote_api_key ) ) {
638634
$results[ $site_id ] = array(

inc/classes/rest/class-templates.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Templates {
1919
/**
2020
* REST namespace.
2121
*/
22-
const NAMESPACE = 'onedesign/v1/templates';
22+
const NAMESPACE = Utils::NAMESPACE . '/templates';
2323

2424
/**
2525
* Use singleton trait.
@@ -469,7 +469,7 @@ public function remove_template( \WP_REST_Request $request ): \WP_REST_Response
469469
);
470470
}
471471

472-
$request_url = esc_url_raw( trailingslashit( $site_info['url'] ) ) . '/wp-json/' . self::NAMESPACE . '/remove-site-templates';
472+
$request_url = Utils::build_api_endpoint( $site_info['url'], 'remove-site-templates', self::NAMESPACE );
473473
$api_key = $site_info['api_key'] ?? '';
474474

475475
$response = wp_safe_remote_request(
@@ -643,14 +643,14 @@ public function apply_templates_to_sites( \WP_REST_Request $request ): \WP_REST_
643643
$site_api_key = $site['api_key'];
644644
$site_id = $site['id'];
645645
if ( in_array( $site_id, $sites, true ) ) {
646-
$request_url = $site_url . 'wp-json/' . self::NAMESPACE . '/shared';
646+
$request_url = Utils::build_api_endpoint( $site['url'], 'shared', self::NAMESPACE );
647647
$new_templates = Utils::modify_template_template_part_pattern_slug( $shared_templates, $site['name'] );
648648
$new_patterns = Utils::modify_template_template_part_pattern_slug( $patterns, $site['name'] );
649649
$new_template_parts = Utils::modify_template_template_part_pattern_slug( $template_parts, $site['name'] );
650650
$new_synced_patterns = Utils::modify_template_template_part_pattern_slug( $synced_patterns, $site['name'] );
651651

652652
// first make a request to create synced patterns to brand site.
653-
$synced_patterns_request_url = $site_url . 'wp-json/' . self::NAMESPACE . '/create-synced-patterns';
653+
$synced_patterns_request_url = Utils::build_api_endpoint( $site['url'], 'create-synced-patterns', self::NAMESPACE );
654654
$synced_patterns_response = wp_safe_remote_post(
655655
$synced_patterns_request_url,
656656
array(
@@ -750,7 +750,7 @@ public function get_templates_from_connected_sites(): \WP_REST_Response {
750750
$sites_response = array();
751751
$error_log = array();
752752
foreach ( $connected_sites as $site ) {
753-
$request_url = esc_url_raw( trailingslashit( $site['url'] ) ) . '/wp-json/' . self::NAMESPACE . '/shared?timestamp=' . time();
753+
$request_url = Utils::build_api_endpoint( $site['url'], 'shared', self::NAMESPACE ) . '?timestamp=' . time();
754754
$api_key = $site['api_key'];
755755
$response = wp_safe_remote_get(
756756
$request_url,

0 commit comments

Comments
 (0)