diff --git a/assets/src/admin/multisite-plugin/index.js b/assets/src/admin/multisite-plugin/index.js index 1d1ac8d..d7491b5 100644 --- a/assets/src/admin/multisite-plugin/index.js +++ b/assets/src/admin/multisite-plugin/index.js @@ -70,7 +70,6 @@ const OneDesignMultisiteGoverningSiteSelection = () => { setGoverningSite( data.governing_site ); currentGoverningSiteID.current = data.governing_site; } - return; } catch { setNotice( { type: 'error', @@ -116,7 +115,6 @@ const OneDesignMultisiteGoverningSiteSelection = () => { setIsSaving( false ); window.location.reload(); }, 1000 ); - return; } catch { setNotice( { type: 'error', diff --git a/assets/src/css/admin.scss b/assets/src/css/admin.scss index 6c4b12e..f1ba72a 100644 --- a/assets/src/css/admin.scss +++ b/assets/src/css/admin.scss @@ -113,6 +113,6 @@ body.onedesign-site-selection-modal { [id="toplevel_page_onedesign"] { .dashicons-admin-generic::before { - content: url("../images/logo.svg") !important; + content: url(../images/logo.svg) !important; } } diff --git a/inc/classes/class-assets.php b/inc/classes/class-assets.php index 9d5a7e2..def055e 100644 --- a/inc/classes/class-assets.php +++ b/inc/classes/class-assets.php @@ -26,7 +26,7 @@ class Assets { * * @var array */ - private static array $localized_data = array(); + private static array $localized_data = []; /** * Protected class constructor @@ -40,8 +40,8 @@ protected function __construct() { * Setup WordPress hooks */ public function setup_hooks(): void { - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts' ), 20, 1 ); - add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_scripts' ), 20, 1 ); + add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_scripts' ], 20, 1 ); + add_action( 'admin_enqueue_scripts', [ $this, 'add_admin_scripts' ], 20, 1 ); } /** @@ -50,12 +50,12 @@ public function setup_hooks(): void { * @return void */ private static function build_localized_data(): void { - self::$localized_data = array( + self::$localized_data = [ 'restUrl' => esc_url( home_url( '/wp-json' ) ), 'restNonce' => wp_create_nonce( 'wp_rest' ), 'apiKey' => get_option( Constants::ONEDESIGN_API_KEY, 'default_api_key' ), 'settingsLink' => esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ), - ); + ]; } /** @@ -84,12 +84,12 @@ public function add_admin_scripts( $hook_suffix ): void { 'OneDesignSettings', array_merge( self::$localized_data, - array( + [ 'multisites' => Utils::get_all_multisites_info(), 'isMultisite' => Utils::is_multisite(), 'isGoverningSiteSelected' => Utils::is_governing_site_selected(), 'currentSiteId' => Utils::is_multisite() ? get_current_blog_id() : null, - ) + ] ) ); @@ -122,7 +122,6 @@ public function add_admin_scripts( $hook_suffix ): void { ); wp_enqueue_script( 'onedesign-setup-script' ); - } if ( Utils::is_multisite() && 'plugins-network' === $current_screen->id && ! Utils::is_governing_site_selected() ) { @@ -140,14 +139,13 @@ public function add_admin_scripts( $hook_suffix ): void { 'OneDesignMultiSiteSettings', array_merge( self::$localized_data, - array( + [ 'multisites' => Utils::get_all_multisites_info(), - ) + ] ) ); wp_enqueue_script( 'onedesign-multisite-setup-script' ); - } $this->register_style( 'onedesign-admin-style', 'admin.css' ); @@ -164,7 +162,6 @@ public function enqueue_scripts(): void { $current_screen = Utils::get_current_screen(); if ( $current_screen && Pattern::SLUG === $current_screen->id ) { - $this->register_script( 'onedesign-patterns-library-script', 'patterns-library.js' @@ -182,24 +179,25 @@ public function enqueue_scripts(): void { wp_enqueue_style( 'onedesign-editor-style' ); } - if ( Template::SLUG === $current_screen->id ) { + if ( Template::SLUG !== $current_screen->id ) { + return; + } - $this->register_script( - 'onedesign-templates-library-script', - 'templates-library.js' - ); + $this->register_script( + 'onedesign-templates-library-script', + 'templates-library.js' + ); - wp_localize_script( - 'onedesign-templates-library-script', - 'TemplateLibraryData', - self::$localized_data - ); + wp_localize_script( + 'onedesign-templates-library-script', + 'TemplateLibraryData', + self::$localized_data + ); - wp_enqueue_script( 'onedesign-templates-library-script' ); + wp_enqueue_script( 'onedesign-templates-library-script' ); - $this->register_style( 'onedesign-template-style', 'template.css' ); - wp_enqueue_style( 'onedesign-template-style' ); - } + $this->register_style( 'onedesign-template-style', 'template.css' ); + wp_enqueue_style( 'onedesign-template-style' ); } /** @@ -211,14 +209,14 @@ public function enqueue_scripts(): void { * * @return array */ - public function get_asset_meta( $file, $deps = array(), $ver = false ): array { + public function get_asset_meta( $file, $deps = [], $ver = false ): array { $asset_meta_file = sprintf( '%s/%s.asset.php', untrailingslashit( ONEDESIGN_BUILD_PATH ), basename( $file, '.' . pathinfo( $file )['extension'] ) ); $asset_meta = is_readable( $asset_meta_file ) ? require_once $asset_meta_file - : array( - 'dependencies' => array(), + : [ + 'dependencies' => [], 'version' => $this->get_file_version( $file, $ver ), - ); + ]; $asset_meta['dependencies'] = array_merge( $deps, $asset_meta['dependencies'] ); @@ -236,7 +234,7 @@ public function get_asset_meta( $file, $deps = array(), $ver = false ): array { * Default 'false'. * @return bool Whether the script has been registered. True on success, false on failure. */ - public function register_script( $handle, $file, $deps = array(), $ver = false, $in_footer = true ): bool { + public function register_script( $handle, $file, $deps = [], $ver = false, $in_footer = true ): bool { $file_path = sprintf( '%s/%s', ONEDESIGN_BUILD_PATH, $file ); @@ -270,7 +268,7 @@ public function register_script( $handle, $file, $deps = array(), $ver = false, * * @return bool Whether the style has been registered. True on success, false on failure. */ - public function register_style( $handle, $file, $deps = array(), $ver = false, $media = 'all' ): bool { + public function register_style( $handle, $file, $deps = [], $ver = false, $media = 'all' ): bool { $file_path = sprintf( '%s/%s', ONEDESIGN_BUILD_PATH, $file ); @@ -287,8 +285,8 @@ public function register_style( $handle, $file, $deps = array(), $ver = false, $ /** * Get file version. * - * @param string $file File path. - * @param int|string|boolean $ver File version. + * @param string $file File path. + * @param int|string|bool $ver File version. * * @return bool|int|string */ diff --git a/inc/classes/class-cpt-restriction.php b/inc/classes/class-cpt-restriction.php index 21ab97a..5dc3bf3 100644 --- a/inc/classes/class-cpt-restriction.php +++ b/inc/classes/class-cpt-restriction.php @@ -38,14 +38,14 @@ protected function __construct() { * Function to setup hooks. */ public function setup_hooks(): void { - add_filter( 'register_post_type_args', array( $this, 'restrict_cpt' ), 5, 2 ); - add_action( 'init', array( $this, 'unregister_cpt' ), 20 ); - add_action( 'current_screen', array( $this, 'limit_pattern_library_posts' ) ); - add_action( 'current_screen', array( $this, 'limit_template_posts' ) ); - add_filter( 'register_post_type_args', array( $this, 'modify_pattern_library_labels' ), 10, 2 ); - add_action( 'admin_menu', array( $this, 'modify_pattern_library_admin_menu' ), 999 ); - add_filter( 'default_content', array( $this, 'add_default_content_to_editor' ), 10, 2 ); - add_filter( 'default_title', array( $this, 'add_default_title_to_editor' ), 10, 2 ); + add_filter( 'register_post_type_args', [ $this, 'restrict_cpt' ], 5, 2 ); + add_action( 'init', [ $this, 'unregister_cpt' ], 20 ); + add_action( 'current_screen', [ $this, 'limit_pattern_library_posts' ] ); + add_action( 'current_screen', [ $this, 'limit_template_posts' ] ); + add_filter( 'register_post_type_args', [ $this, 'modify_pattern_library_labels' ], 10, 2 ); + add_action( 'admin_menu', [ $this, 'modify_pattern_library_admin_menu' ], 999 ); + add_filter( 'default_content', [ $this, 'add_default_content_to_editor' ], 10, 2 ); + add_filter( 'default_title', [ $this, 'add_default_title_to_editor' ], 10, 2 ); } /** @@ -72,7 +72,7 @@ public function add_default_title_to_editor( string $title ): string { * @return array Modified arguments. */ public function restrict_cpt( array $args, string $post_type ): array { - if ( ! in_array( $post_type, array( Pattern::SLUG, Template::SLUG ), true ) ) { + if ( ! in_array( $post_type, [ Pattern::SLUG, Template::SLUG ], true ) ) { return $args; } @@ -88,7 +88,7 @@ public function restrict_cpt( array $args, string $post_type ): array { return $args; } - if ( in_array( $post_type, array( Pattern::SLUG, Template::SLUG ), true ) ) { + if ( in_array( $post_type, [ Pattern::SLUG, Template::SLUG ], true ) ) { $args['public'] = false; $args['show_ui'] = false; $args['show_in_menu'] = false; @@ -99,7 +99,7 @@ public function restrict_cpt( array $args, string $post_type ): array { $args['exclude_from_search'] = true; $args['publicly_queryable'] = false; $args['show_in_rest'] = false; - $args['capabilities'] = array( + $args['capabilities'] = [ 'edit_post' => 'do_not_allow', 'read_post' => 'do_not_allow', 'delete_post' => 'do_not_allow', @@ -107,7 +107,7 @@ public function restrict_cpt( array $args, string $post_type ): array { 'edit_others_posts' => 'do_not_allow', 'publish_posts' => 'do_not_allow', 'read_private_posts' => 'do_not_allow', - ); + ]; } return $args; @@ -142,22 +142,24 @@ public function limit_pattern_library_posts(): void { $post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private; // If a post already exists, redirect to edit screen. - if ( $post_count > 0 ) { - // Get the existing post. - $existing_post = get_posts( - array( - 'post_type' => Pattern::SLUG, - 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), - 'numberposts' => 1, - 'suppress_filters' => false, - ) - ); + if ( $post_count <= 0 ) { + return; + } - if ( ! empty( $existing_post ) ) { - // Redirect to edit screen of the existing post. - wp_safe_redirect( admin_url( 'post.php?post=' . $existing_post[0]->ID . '&action=edit' ) ); - exit; - } + // Get the existing post. + $existing_post = get_posts( + [ + 'post_type' => Pattern::SLUG, + 'post_status' => [ 'publish', 'draft', 'pending', 'private' ], + 'numberposts' => 1, + 'suppress_filters' => false, + ] + ); + + if ( ! empty( $existing_post ) ) { + // Redirect to edit screen of the existing post. + wp_safe_redirect( admin_url( 'post.php?post=' . $existing_post[0]->ID . '&action=edit' ) ); + exit; } } @@ -183,22 +185,24 @@ public function limit_template_posts(): void { $post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private; // If a post already exists, redirect to edit screen. - if ( $post_count > 0 ) { - // Get the existing post. - $existing_post = get_posts( - array( - 'post_type' => Template::SLUG, - 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), - 'numberposts' => 1, - 'suppress_filters' => false, - ) - ); + if ( $post_count <= 0 ) { + return; + } - if ( ! empty( $existing_post ) ) { - // Redirect to edit screen of the existing post. - wp_safe_redirect( admin_url( 'post.php?post=' . $existing_post[0]->ID . '&action=edit' ) ); - exit; - } + // Get the existing post. + $existing_post = get_posts( + [ + 'post_type' => Template::SLUG, + 'post_status' => [ 'publish', 'draft', 'pending', 'private' ], + 'numberposts' => 1, + 'suppress_filters' => false, + ] + ); + + if ( ! empty( $existing_post ) ) { + // Redirect to edit screen of the existing post. + wp_safe_redirect( admin_url( 'post.php?post=' . $existing_post[0]->ID . '&action=edit' ) ); + exit; } } @@ -257,28 +261,32 @@ public function modify_pattern_library_admin_menu(): void { $post_count = $existing_posts->publish + $existing_posts->draft + $existing_posts->pending + $existing_posts->private; - if ( $post_count > 0 ) { - // Find the "Add New" menu item. - foreach ( $submenu[ 'edit.php?post_type=' . Pattern::SLUG ] as $key => $item ) { - if ( 'post-new.php?post_type=' . Pattern::SLUG === $item[2] ) { - // Get the existing post. - $existing_post = get_posts( - array( - 'post_type' => Pattern::SLUG, - 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), - 'numberposts' => 1, - 'suppress_filters' => false, - ) - ); - - if ( ! empty( $existing_post ) ) { - // Change the "Add New" link to edit the existing post. - $submenu['edit.php?post_type=onedesign-pattern'][ $key ][2] = 'post.php?post=' . $existing_post[0]->ID . '&action=edit'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type. - $submenu['edit.php?post_type=onedesign-pattern'][ $key ][0] = esc_html__( 'Edit Pattern Library', 'onedesign' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type. - } - break; - } + if ( $post_count <= 0 ) { + return; + } + + // Find the "Add New" menu item. + foreach ( $submenu[ 'edit.php?post_type=' . Pattern::SLUG ] as $key => $item ) { + if ( 'post-new.php?post_type=' . Pattern::SLUG !== $item[2] ) { + continue; + } + + // Get the existing post. + $existing_post = get_posts( + [ + 'post_type' => Pattern::SLUG, + 'post_status' => [ 'publish', 'draft', 'pending', 'private' ], + 'numberposts' => 1, + 'suppress_filters' => false, + ] + ); + + if ( ! empty( $existing_post ) ) { + // Change the "Add New" link to edit the existing post. + $submenu['edit.php?post_type=onedesign-pattern'][ $key ][2] = 'post.php?post=' . $existing_post[0]->ID . '&action=edit'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type. + $submenu['edit.php?post_type=onedesign-pattern'][ $key ][0] = esc_html__( 'Edit Pattern Library', 'onedesign' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type. } + break; } } diff --git a/inc/classes/class-hooks.php b/inc/classes/class-hooks.php index 783cd31..3d1bca4 100644 --- a/inc/classes/class-hooks.php +++ b/inc/classes/class-hooks.php @@ -36,27 +36,27 @@ protected function __construct() { * @return void */ public function setup_hooks(): void { - add_action( 'admin_footer', array( $this, 'print_pattern_library_button_in_editor_js_template' ) ); - add_action( 'admin_footer', array( $this, 'add_templates_button_to_editor' ) ); - add_action( 'wp_ajax_register_block_patterns', array( $this, 'ajax_register_block_patterns' ) ); - add_action( 'wp_ajax_nopriv_register_block_patterns', array( $this, 'ajax_register_block_patterns' ) ); - add_action( 'init', array( $this, 'register_block_patterns_if_not_exist' ) ); + add_action( 'admin_footer', [ $this, 'print_pattern_library_button_in_editor_js_template' ] ); + add_action( 'admin_footer', [ $this, 'add_templates_button_to_editor' ] ); + add_action( 'wp_ajax_register_block_patterns', [ $this, 'ajax_register_block_patterns' ] ); + add_action( 'wp_ajax_nopriv_register_block_patterns', [ $this, 'ajax_register_block_patterns' ] ); + add_action( 'init', [ $this, 'register_block_patterns_if_not_exist' ] ); add_filter( 'should_load_remote_block_patterns', '__return_false' ); - add_action( 'after_setup_theme', array( $this, 'remove_core_block_patterns' ) ); - add_filter( 'allowed_block_types_all', array( $this, 'allowed_block_types' ), 10, 2 ); + add_action( 'after_setup_theme', [ $this, 'remove_core_block_patterns' ] ); + add_filter( 'allowed_block_types_all', [ $this, 'allowed_block_types' ], 10, 2 ); // Create templates, patterns and template parts from saved options. - add_action( 'after_setup_theme', array( $this, 'create_template' ), 99 ); + add_action( 'after_setup_theme', [ $this, 'create_template' ], 99 ); // add container for modal for site selection on activation. - add_action( 'admin_footer', array( $this, 'add_site_selection_modal' ) ); + add_action( 'admin_footer', [ $this, 'add_site_selection_modal' ] ); // add body class for site selection modal. - add_filter( 'admin_body_class', array( $this, 'add_body_class_for_modal' ) ); - add_filter( 'admin_body_class', array( $this, 'add_body_class_for_missing_sites' ) ); + add_filter( 'admin_body_class', [ $this, 'add_body_class_for_modal' ] ); + add_filter( 'admin_body_class', [ $this, 'add_body_class_for_missing_sites' ] ); // add setup page link to plugins page. - add_filter( 'plugin_action_links_' . ONEDESIGN_PLUGIN_LOADER_PLUGIN_BASENAME, array( $this, 'add_setup_page_link' ) ); + add_filter( 'plugin_action_links_' . ONEDESIGN_PLUGIN_LOADER_PLUGIN_BASENAME, [ $this, 'add_setup_page_link' ] ); } /** @@ -140,7 +140,7 @@ public function add_body_class_for_missing_sites( $classes ): string { } // get onedesign_shared_sites option. - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); // if shared_sites is empty or not an array, return the classes. if ( empty( $shared_sites ) || ! is_array( $shared_sites ) ) { @@ -167,28 +167,28 @@ public function create_template(): void { return; } - $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, array() ); + $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, [] ); - $shared_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, array() ); + $shared_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, [] ); - $logs = array(); + $logs = []; - $all_post_types = get_post_types( array( 'public' => true ), 'names' ); + $all_post_types = get_post_types( [ 'public' => true ], 'names' ); // Remove post types that shouldn't have templates. - $excluded_types = array( 'attachment', 'wp_block', 'wp_template', 'wp_template_part', 'wp_navigation' ); + $excluded_types = [ 'attachment', 'wp_block', 'wp_template', 'wp_template_part', 'wp_navigation' ]; $all_post_types = array_values( array_diff( $all_post_types, $excluded_types ) ); foreach ( $shared_templates as $template ) { $res = register_block_template( sanitize_text_field( $template['id'] ), - array( + [ 'slug' => isset( $template['slug'] ) ? sanitize_text_field( $template['slug'] ) : '', 'title' => isset( $template['title'] ) ? sanitize_text_field( $template['title'] ) : '', 'description' => isset( $template['description'] ) ? sanitize_textarea_field( $template['description'] ) : '', 'content' => $template['content'] ?? '', 'post_types' => isset( $template['post_types'] ) ? array_map( 'sanitize_textarea_field', $template['post_types'] ) : $all_post_types, - ) + ] ); $logs[] = sprintf( @@ -199,19 +199,19 @@ public function create_template(): void { ); } - $shared_patterns = get_option( Constants::ONEDESIGN_SHARED_PATTERNS, array() ); + $shared_patterns = get_option( Constants::ONEDESIGN_SHARED_PATTERNS, [] ); foreach ( $shared_patterns as $pattern ) { if ( ! class_exists( '\WP_Block_Patterns_Registry' ) ) { require_once ABSPATH . 'wp-includes/class-wp-block-patterns-registry.php'; } $res = register_block_pattern( sanitize_text_field( $pattern['slug'] ), - array( + [ 'title' => isset( $pattern['title'] ) ? sanitize_text_field( $pattern['title'] ) : '', 'content' => $pattern['content'] ?? '', 'description' => isset( $pattern['description'] ) ? sanitize_textarea_field( $pattern['description'] ) : '', - 'postTypes' => isset( $pattern['post_types'] ) ? array_map( 'sanitize_textarea_field', $pattern['post_types'] ) : array(), - ) + 'postTypes' => isset( $pattern['post_types'] ) ? array_map( 'sanitize_textarea_field', $pattern['post_types'] ) : [], + ] ); $logs[] = sprintf( /* translators: 1: Pattern slug. 2: Result. */ @@ -221,17 +221,17 @@ public function create_template(): void { ); } - $shared_template_parts = get_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, array() ); + $shared_template_parts = get_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, [] ); foreach ( $shared_template_parts as $template_part ) { // Check if template part already exists. $existing = get_posts( - array( + [ 'post_type' => 'wp_template_part', 'name' => sanitize_text_field( $template_part['slug'] ), 'post_status' => 'any', 'numberposts' => 1, 'fields' => 'ids', - ) + ] ); if ( ! empty( $existing ) ) { @@ -244,13 +244,13 @@ public function create_template(): void { } // Create the template part post. - $post_data = array( + $post_data = [ 'post_type' => 'wp_template_part', 'post_title' => isset( $template_part['title'] ) ? sanitize_text_field( $template_part['title'] ) : '', 'post_name' => isset( $template_part['slug'] ) ? sanitize_text_field( $template_part['slug'] ) : '', 'post_status' => 'publish', 'post_content' => $template_part['content'] ?? '', - ); + ]; $post_id = wp_insert_post( $post_data ); @@ -262,16 +262,16 @@ public function create_template(): void { $post_id->get_error_message() ); continue; - } else { - $logs[] = sprintf( - /* translators: 1: Template part slug. 2: Post ID. */ - __( 'Template part created successfully: %1$s (ID: %2$d)', 'onedesign' ), - sanitize_text_field( $template_part['slug'] ), - $post_id - ); - $brand_site_post_ids[] = $post_id; } + $logs[] = sprintf( + /* translators: 1: Template part slug. 2: Post ID. */ + __( 'Template part created successfully: %1$s (ID: %2$d)', 'onedesign' ), + sanitize_text_field( $template_part['slug'] ), + $post_id + ); + $brand_site_post_ids[] = $post_id; + $current_theme = get_option( 'stylesheet' ); $theme_slug = get_option( 'template' ); @@ -308,7 +308,7 @@ public function create_template(): void { public function allowed_block_types( bool|array $allowed_block_types, \WP_Block_Editor_Context $editor_context ): array|bool { // Allow all block types in the Pattern Library post type. if ( isset( $editor_context->post->post_type ) && ( Template::SLUG === $editor_context->post->post_type ) ) { - return array(); + return []; } return $allowed_block_types; } @@ -374,7 +374,7 @@ public function add_templates_button_to_editor(): void { public function ajax_register_block_patterns(): void { // Verify nonce for security. if ( ! check_ajax_referer( 'onedesign_nonce', 'security', false ) ) { - wp_send_json_error( array( 'message' => 'Invalid security token.' ), 403 ); + wp_send_json_error( [ 'message' => 'Invalid security token.' ], 403 ); return; } @@ -382,7 +382,7 @@ public function ajax_register_block_patterns(): void { $this->register_block_patterns_if_not_exist(); // Return success. - wp_send_json_success( array( 'message' => 'Patterns registered successfully' ) ); + wp_send_json_success( [ 'message' => 'Patterns registered successfully' ] ); } /** @@ -407,20 +407,22 @@ public function register_block_patterns_if_not_exist(): void { } // Prepare pattern args. - $pattern_args = array( + $pattern_args = [ 'title' => $pattern_data['title'] ?? '', 'content' => $pattern_data['content'] ?? '', - 'categories' => $pattern_data['categories'] ?? array(), - 'keywords' => $pattern_data['keywords'] ?? array(), + 'categories' => $pattern_data['categories'] ?? [], + 'keywords' => $pattern_data['keywords'] ?? [], 'description' => $pattern_data['description'] ?? '', - ); + ]; // Register categories if they are not registered. if ( ! empty( $pattern_args['categories'] ) ) { foreach ( $pattern_args['categories'] as $category ) { - if ( ! term_exists( $category, 'wp_pattern_category' ) ) { - wp_insert_term( $category, 'wp_pattern_category' ); + if ( term_exists( $category, 'wp_pattern_category' ) ) { + continue; } + + wp_insert_term( $category, 'wp_pattern_category' ); } } @@ -442,9 +444,11 @@ public function register_block_patterns_if_not_exist(): void { } // Only register if we have required content. - if ( ! empty( $pattern_args['title'] ) && ! empty( $pattern_args['content'] ) ) { - register_block_pattern( $pattern_name, $pattern_args ); + if ( empty( $pattern_args['title'] ) || empty( $pattern_args['content'] ) ) { + continue; } + + register_block_pattern( $pattern_name, $pattern_args ); } } } diff --git a/inc/classes/class-multisite.php b/inc/classes/class-multisite.php index 172c51b..a64c5b1 100644 --- a/inc/classes/class-multisite.php +++ b/inc/classes/class-multisite.php @@ -40,19 +40,19 @@ protected function setup_hooks(): void { } // add governing site selection modal on network admin plugins page. - add_action( 'admin_footer', array( $this, 'render_governing_site_modal' ) ); + add_action( 'admin_footer', [ $this, 'render_governing_site_modal' ] ); // add admin_body_class class of onedesign-multisite-selection-modal on network admin plugins page. - add_filter( 'admin_body_class', array( $this, 'add_admin_body_class' ) ); + add_filter( 'admin_body_class', [ $this, 'add_admin_body_class' ] ); // add onedesign_multisite_api_key_generated action to change same key in governing site. - add_action( 'onedesign_multisite_api_key_generated', array( $this, 'sync_api_key_to_governing_site' ), 10, 2 ); + add_action( 'onedesign_multisite_api_key_generated', [ $this, 'sync_api_key_to_governing_site' ], 10, 2 ); // auto assign brand-site on new site creation if governing site is set. - add_action( 'wp_initialize_site', array( $this, 'assign_brand_site_on_new_site_creation' ), 10, 2 ); + add_action( 'wp_initialize_site', [ $this, 'assign_brand_site_on_new_site_creation' ], 10, 2 ); // listen to option changes for blogname, siteurl and home to update into governing site table. - add_action( 'updated_option', array( $this, 'update_site_details_in_governing_site_table' ), 10, 3 ); + add_action( 'updated_option', [ $this, 'update_site_details_in_governing_site_table' ], 10, 3 ); } /** @@ -115,22 +115,24 @@ public function sync_api_key_to_governing_site( string $secret_key, int $blog_id $governing_site_id = get_site_option( Constants::ONEDESIGN_MULTISITE_GOVERNING_SITE, 0 ); // go to governing site and update shared_sites option secret_key of blog_id site. - if ( $governing_site_id && $secret_key ) { - if ( ! switch_to_blog( (int) $governing_site_id ) ) { - return; - } - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); - foreach ( $shared_sites as &$site ) { - if ( (int) $site['id'] === (int) $blog_id ) { - $site['api_key'] = $secret_key; - break; - } + if ( ! $governing_site_id || ! $secret_key ) { + return; + } + + if ( ! switch_to_blog( (int) $governing_site_id ) ) { + return; + } + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); + foreach ( $shared_sites as &$site ) { + if ( (int) $site['id'] === (int) $blog_id ) { + $site['api_key'] = $secret_key; + break; } + } - update_option( Constants::ONEDESIGN_SHARED_SITES, $shared_sites, false ); + update_option( Constants::ONEDESIGN_SHARED_SITES, $shared_sites, false ); - restore_current_blog(); - } + restore_current_blog(); } /** @@ -144,15 +146,17 @@ public function assign_brand_site_on_new_site_creation( \WP_Site $new_site ): vo $governing_site_id = get_site_option( Constants::ONEDESIGN_MULTISITE_GOVERNING_SITE, 0 ); - if ( $governing_site_id && $new_site->blog_id !== $governing_site_id ) { - if ( ! switch_to_blog( (int) $new_site->blog_id ) ) { - return; - } - - update_option( Constants::ONEDESIGN_SITE_TYPE, 'brand-site', false ); + if ( ! $governing_site_id || $new_site->blog_id === $governing_site_id ) { + return; + } - restore_current_blog(); + if ( ! switch_to_blog( (int) $new_site->blog_id ) ) { + return; } + + update_option( Constants::ONEDESIGN_SITE_TYPE, 'brand-site', false ); + + restore_current_blog(); } /** @@ -173,51 +177,55 @@ public function update_site_details_in_governing_site_table( string $option_name return; } - $relevant_options = array( 'blogname', 'siteurl', 'home', 'site_icon' ); + $relevant_options = [ 'blogname', 'siteurl', 'home', 'site_icon' ]; $current_site_id = get_current_blog_id(); // For site_icon: Fetch details in CHILD site context BEFORE switching. $logo_url = ''; $logo_id = 0; - if ( in_array( $option_name, $relevant_options, true ) ) { - if ( 'site_icon' === $option_name ) { - if ( ! empty( $new_value ) && is_numeric( $new_value ) && (int) $new_value > 0 ) { - $attachment = get_post( (int) $new_value ); - if ( $attachment && 'attachment' === $attachment->post_type ) { - $logo_url = wp_get_attachment_url( (int) $new_value ); - $logo_id = (int) $new_value; - } + if ( ! in_array( $option_name, $relevant_options, true ) ) { + return; + } + + if ( 'site_icon' === $option_name ) { + if ( ! empty( $new_value ) && is_numeric( $new_value ) && (int) $new_value > 0 ) { + $attachment = get_post( (int) $new_value ); + if ( $attachment && 'attachment' === $attachment->post_type ) { + $logo_url = wp_get_attachment_url( (int) $new_value ); + $logo_id = (int) $new_value; } } + } - // Now switch to governing site. - if ( ! switch_to_blog( (int) $governing_site_id ) ) { - return; + // Now switch to governing site. + if ( ! switch_to_blog( (int) $governing_site_id ) ) { + return; + } + + // Get shared sites from governing site. + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); + + foreach ( $shared_sites as &$site ) { + if ( (int) $site['id'] !== $current_site_id ) { + continue; } - // Get shared sites from governing site. - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); - - foreach ( $shared_sites as &$site ) { - if ( (int) $site['id'] === $current_site_id ) { - if ( 'blogname' === $option_name ) { - $site['name'] = sanitize_text_field( $new_value ); - } elseif ( in_array( $option_name, array( 'siteurl', 'home' ), true ) ) { - $site['url'] = esc_url_raw( $new_value ); - } elseif ( 'site_icon' === $option_name ) { - $site['logo'] = $logo_url; - $site['logo_id'] = $logo_id; - } - break; - } + if ( 'blogname' === $option_name ) { + $site['name'] = sanitize_text_field( $new_value ); + } elseif ( in_array( $option_name, [ 'siteurl', 'home' ], true ) ) { + $site['url'] = esc_url_raw( $new_value ); + } elseif ( 'site_icon' === $option_name ) { + $site['logo'] = $logo_url; + $site['logo_id'] = $logo_id; } + break; + } - // Save the updated shared_sites option. - update_option( Constants::ONEDESIGN_SHARED_SITES, $shared_sites, false ); + // Save the updated shared_sites option. + update_option( Constants::ONEDESIGN_SHARED_SITES, $shared_sites, false ); - // Restore blog. - restore_current_blog(); - } + // Restore blog. + restore_current_blog(); } } diff --git a/inc/classes/class-plugin.php b/inc/classes/class-plugin.php index 543cbda..8cf0874 100644 --- a/inc/classes/class-plugin.php +++ b/inc/classes/class-plugin.php @@ -40,9 +40,11 @@ private function setup(): void { $this->load_configs(); // Load multisite functionality. - if ( Utils::is_multisite() ) { - $this->load_multisite_classes(); + if ( ! Utils::is_multisite() ) { + return; } + + $this->load_multisite_classes(); } /** diff --git a/inc/classes/class-rest.php b/inc/classes/class-rest.php index c326149..e86b8d5 100644 --- a/inc/classes/class-rest.php +++ b/inc/classes/class-rest.php @@ -44,7 +44,7 @@ protected function setup_hooks() { } // allow cors header for all REST API requests. - add_filter( 'rest_pre_serve_request', array( $this, 'add_cors_headers' ), PHP_INT_MAX - 30, 4 ); + add_filter( 'rest_pre_serve_request', [ $this, 'add_cors_headers' ], PHP_INT_MAX - 30, 4 ); } /** diff --git a/inc/classes/class-settings.php b/inc/classes/class-settings.php index 992c900..27ec8e9 100644 --- a/inc/classes/class-settings.php +++ b/inc/classes/class-settings.php @@ -39,9 +39,9 @@ protected function __construct() { * Function to setup hooks. */ public function setup_hooks(): void { - add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); - add_action( 'admin_init', array( $this, 'handle_pattern_library_redirect' ) ); - add_action( 'admin_init', array( $this, 'templates_page_redirection' ) ); + add_action( 'admin_menu', [ $this, 'add_settings_page' ] ); + add_action( 'admin_init', [ $this, 'handle_pattern_library_redirect' ] ); + add_action( 'admin_init', [ $this, 'templates_page_redirection' ] ); } /** @@ -86,7 +86,7 @@ public function add_settings_page(): void { __( 'Settings', 'onedesign' ), 'manage_options', 'onedesign-settings', - array( $this, 'settings_page_content' ) + [ $this, 'settings_page_content' ] ); remove_submenu_page( 'onedesign', 'onedesign' ); @@ -101,7 +101,7 @@ public function add_settings_page(): void { * @return void */ public function templates_page_redirection(): void { - $pages = array( 'onedesign-template-library' ); + $pages = [ 'onedesign-template-library' ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['page'] ) || ! in_array( $_GET['page'], $pages, true ) ) { @@ -115,12 +115,12 @@ public function templates_page_redirection(): void { // Check if a Pattern Library post already exists. $existing_posts = get_posts( - array( + [ 'post_type' => Template::SLUG, - 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), + 'post_status' => [ 'publish', 'draft', 'pending', 'private' ], 'numberposts' => 1, 'suppress_filters' => false, - ) + ] ); if ( ! empty( $existing_posts ) ) { @@ -131,12 +131,12 @@ public function templates_page_redirection(): void { // If no post exists, create a new one. $new_post_id = wp_insert_post( - array( + [ 'post_type' => Template::SLUG, 'post_title' => esc_html__( 'Templates', 'onedesign' ), 'post_content' => '', 'post_status' => 'draft', - ) + ] ); if ( is_wp_error( $new_post_id ) ) { @@ -157,7 +157,7 @@ public function templates_page_redirection(): void { * @return void */ public function handle_pattern_library_redirect(): void { - $pages = array( 'onedesign-pattern-library', 'onedesign' ); + $pages = [ 'onedesign-pattern-library', 'onedesign' ]; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['page'] ) || ! in_array( $_GET['page'], $pages, true ) ) { @@ -198,12 +198,12 @@ public function settings_page_content(): void { public function create_and_open_pattern_library_post(): void { // Check if a Pattern Library post already exists. $existing_posts = get_posts( - array( + [ 'post_type' => Pattern::SLUG, - 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), + 'post_status' => [ 'publish', 'draft', 'pending', 'private' ], 'numberposts' => 1, 'suppress_filters' => false, - ) + ] ); if ( ! empty( $existing_posts ) ) { @@ -214,12 +214,12 @@ public function create_and_open_pattern_library_post(): void { // If no post exists, create a new one. $new_post_id = wp_insert_post( - array( + [ 'post_type' => Pattern::SLUG, 'post_title' => esc_html__( 'Pattern Library', 'onedesign' ), 'post_content' => '

Click on the "Patterns Selection" to push patterns to brand site.

', 'post_status' => 'draft', - ) + ] ); if ( is_wp_error( $new_post_id ) ) { diff --git a/inc/classes/class-utils.php b/inc/classes/class-utils.php index 695b6a3..1fbcf6b 100644 --- a/inc/classes/class-utils.php +++ b/inc/classes/class-utils.php @@ -7,8 +7,8 @@ namespace OneDesign; -use OneDesign\Traits\Singleton; use OneDesign\Plugin_Configs\Constants; +use OneDesign\Traits\Singleton; /** * Class Utils @@ -62,8 +62,8 @@ public static function is_governing_site_selected(): bool { * @return array Array of shared sites information. */ public static function get_shared_sites_info(): array { - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); - return is_array( $shared_sites ) ? $shared_sites : array(); + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); + return is_array( $shared_sites ) ? $shared_sites : []; } /** @@ -96,21 +96,23 @@ public static function get_multisite_governing_site(): int { */ public static function get_all_multisites_info(): array { if ( ! self::is_multisite() ) { - return array(); + return []; } - $sites = get_sites( array( 'number' => 0 ) ); - $sites_info = array(); + $sites = get_sites( [ 'number' => 0 ] ); + $sites_info = []; foreach ( $sites as $site ) { $site_details = get_blog_details( $site->blog_id ); - if ( $site_details ) { - $sites_info[] = array( - 'id' => (string) $site_details->blog_id, - 'name' => $site_details->blogname, - 'url' => $site_details->siteurl, - ); + if ( ! $site_details ) { + continue; } + + $sites_info[] = [ + 'id' => (string) $site_details->blog_id, + 'name' => $site_details->blogname, + 'url' => $site_details->siteurl, + ]; } return $sites_info; @@ -123,12 +125,14 @@ public static function get_all_multisites_info(): array { */ public static function get_all_multisite_urls(): array { $sites_info = self::get_all_multisites_info(); - $urls = array(); + $urls = []; foreach ( $sites_info as $site ) { - if ( isset( $site['url'] ) ) { - $urls[] = $site['url']; + if ( ! isset( $site['url'] ) ) { + continue; } + + $urls[] = $site['url']; } return $urls; @@ -140,8 +144,7 @@ public static function get_all_multisite_urls(): array { * @return string */ public static function get_current_site_type(): string { - $onedesign_site_type = get_option( Constants::ONEDESIGN_SITE_TYPE, '' ); - return $onedesign_site_type; + return get_option( Constants::ONEDESIGN_SITE_TYPE, '' ); } /** @@ -204,7 +207,7 @@ public static function get_site_by_id( string $site_id ): array|null { $sites = self::get_sites_info(); $filtered = array_filter( $sites, - function ( $site ) use ( $site_id ): bool { + static function ( $site ) use ( $site_id ): bool { return (string) $site['id'] === (string) $site_id; } ); @@ -218,8 +221,8 @@ function ( $site ) use ( $site_id ): bool { * @return array Array of sites info. */ public static function get_sites_info(): array { - $sites_info = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); - return is_array( $sites_info ) ? $sites_info : array(); + $sites_info = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); + return is_array( $sites_info ) ? $sites_info : []; } /** @@ -287,11 +290,11 @@ public static function modify_content_references( string|array|\WP_Block_Templat } } elseif ( is_array( $content ) ) { // Handle array format. - if ( isset( $content['content'] ) ) { - $content_string = $content['content']; - } else { + if ( ! isset( $content['content'] ) ) { return ''; } + + $content_string = $content['content']; } else { // Unsupported content type. return ''; @@ -302,7 +305,7 @@ public static function modify_content_references( string|array|\WP_Block_Templat return preg_replace_callback( $pattern, - function ( $matches ) use ( $shared_site_name ): string|null { + static function ( $matches ) use ( $shared_site_name ): string|null { $block_type = $matches[1]; $attributes_json = $matches[2]; @@ -390,7 +393,7 @@ public static function modify_template_template_part_pattern_slug( array $templa $content = preg_replace_callback( $pattern, - function ( $matches ): string|null { + static function ( $matches ): string|null { $block_type = $matches[1]; $attributes_json = $matches[2]; @@ -427,17 +430,19 @@ function ( $matches ): string|null { $current_content = $templates[ $index ]['content']; } - if ( ! empty( $current_content ) ) { - $modified_content = self::modify_content_references( - $current_content, - $shared_site_name - ); + if ( empty( $current_content ) ) { + continue; + } - if ( $template instanceof \WP_Block_Template ) { - $templates[ $index ]->content = $modified_content; - } else { - $templates[ $index ]['content'] = $modified_content; - } + $modified_content = self::modify_content_references( + $current_content, + $shared_site_name + ); + + if ( $template instanceof \WP_Block_Template ) { + $templates[ $index ]->content = $modified_content; + } else { + $templates[ $index ]['content'] = $modified_content; } } @@ -452,22 +457,24 @@ function ( $matches ): string|null { * @param string $content_key Key for the content field (default: 'content'). * @return array Modified items with updated block refs. */ - public static function replace_block_refs( array $items, array $id_map = array(), string $content_key = 'content' ): array { + public static function replace_block_refs( array $items, array $id_map = [], string $content_key = 'content' ): array { if ( empty( $id_map ) || ! is_array( $items ) ) { return $items; } foreach ( $items as $key => $item ) { - if ( isset( $item[ $content_key ] ) && ! empty( $item[ $content_key ] ) ) { - $content = $item[ $content_key ]; + if ( ! isset( $item[ $content_key ] ) || empty( $item[ $content_key ] ) ) { + continue; + } - foreach ( $id_map as $old_id => $new_id ) { - $pattern1 = '/()/'; - $content = preg_replace( $pattern1, '${1}' . $new_id . '${2}', $content ); - } + $content = $item[ $content_key ]; - $items[ $key ][ $content_key ] = $content; + foreach ( $id_map as $old_id => $new_id ) { + $pattern1 = '/()/'; + $content = preg_replace( $pattern1, '${1}' . $new_id . '${2}', $content ); } + + $items[ $key ][ $content_key ] = $content; } return $items; @@ -476,7 +483,7 @@ public static function replace_block_refs( array $items, array $id_map = array() /** * Get current screen object. * - * @return WP_Screen|null Current screen object or null if not available. + * @return \OneDesign\WP_Screen|null Current screen object or null if not available. */ public static function get_current_screen(): ?\WP_Screen { if ( ! function_exists( 'get_current_screen' ) ) { diff --git a/inc/classes/plugin-configs/class-secret-key.php b/inc/classes/plugin-configs/class-secret-key.php index 4ada494..2bbf806 100644 --- a/inc/classes/plugin-configs/class-secret-key.php +++ b/inc/classes/plugin-configs/class-secret-key.php @@ -30,7 +30,7 @@ protected function __construct() { * Setup WordPress hooks */ public function setup_hooks(): void { - add_action( 'admin_init', array( self::class, 'generate_secret_key' ) ); + add_action( 'admin_init', [ self::class, 'generate_secret_key' ] ); } /** @@ -87,10 +87,10 @@ public static function get_secret_key(): \WP_REST_Response|\WP_Error { $secret_key = self::generate_secret_key(); } return new \WP_REST_Response( - array( + [ 'success' => true, 'secret_key' => $secret_key, - ) + ] ); } @@ -104,11 +104,11 @@ public static function regenerate_secret_key(): \WP_REST_Response|\WP_Error { $regenerated_key = self::generate_secret_key( true ); return new \WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Secret key regenerated successfully.', 'onedesign' ), 'secret_key' => $regenerated_key, - ) + ] ); } } diff --git a/inc/classes/post-type/class-base.php b/inc/classes/post-type/class-base.php index 38db7ff..471cc82 100644 --- a/inc/classes/post-type/class-base.php +++ b/inc/classes/post-type/class-base.php @@ -36,7 +36,7 @@ protected function setup_hooks(): void { /** * Actions */ - add_action( 'init', array( $this, 'register_post_type' ) ); + add_action( 'init', [ $this, 'register_post_type' ] ); } /** @@ -45,22 +45,22 @@ protected function setup_hooks(): void { * @return void */ final public function register_post_type(): void { - + // phpcs:ignore SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants.DisallowedLateStaticBindingForConstant -- @todo we don't need this. if ( empty( static::SLUG ) ) { return; } $args = $this->get_args(); - $args = ( ! empty( $args ) && is_array( $args ) ) ? $args : array(); + $args = ! empty( $args ) && is_array( $args ) ? $args : []; $labels = $this->get_labels(); - $labels = ( ! empty( $labels ) && is_array( $labels ) ) ? $labels : array(); + $labels = ! empty( $labels ) && is_array( $labels ) ? $labels : []; if ( ! empty( $labels ) && is_array( $labels ) ) { $args['labels'] = $labels; } - // phpcs:ignore WordPress.NamingConventions.ValidPostTypeSlug.NotStringLiteral + // phpcs:ignore WordPress.NamingConventions.ValidPostTypeSlug.NotStringLiteral, SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants.DisallowedLateStaticBindingForConstant -- @todo we don't need this. register_post_type( static::SLUG, $args ); } @@ -72,13 +72,13 @@ final public function register_post_type(): void { * @return array */ public function get_args(): array { - return array( + return [ 'show_in_rest' => true, 'public' => true, 'has_archive' => true, 'menu_position' => 6, - 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), - ); + 'supports' => [ 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ], + ]; } /** @@ -87,7 +87,8 @@ public function get_args(): array { * @return string Slug of post type. */ public function get_slug(): string { - return ( ! empty( static::SLUG ) ) ? static::SLUG : ''; + // phpcs:ignore SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants.DisallowedLateStaticBindingForConstant -- @todo we don't need this. + return ! empty( static::SLUG ) ? static::SLUG : ''; } /** diff --git a/inc/classes/post-type/class-meta.php b/inc/classes/post-type/class-meta.php index bbd28da..637440f 100644 --- a/inc/classes/post-type/class-meta.php +++ b/inc/classes/post-type/class-meta.php @@ -30,7 +30,7 @@ protected function __construct() { */ public function setup_hooks(): void { // Register all the meta based on post type here. - add_action( 'init', array( $this, 'register_custom_meta' ), 10 ); + add_action( 'init', [ $this, 'register_custom_meta' ], 10 ); } /** @@ -44,14 +44,14 @@ public function register_custom_meta(): void { $post_meta_array = $this->get_post_meta_array(); foreach ( $post_meta_array as $meta_info ) { - $args = array( + $args = [ 'show_in_rest' => $meta_info['show_in_rest'] ?? true, 'type' => $meta_info['type'] ?? '', 'single' => $meta_info['single'] ?? true, - 'auth_callback' => function () { + 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, - ); + ]; if ( array_key_exists( 'default', $meta_info ) ) { $args['default'] = $meta_info['default']; @@ -59,10 +59,12 @@ public function register_custom_meta(): void { $post_types = $meta_info['post_type']; - if ( is_array( $post_types ) && ! empty( $post_types ) ) { - foreach ( $post_types as $post_type ) { - register_post_meta( $post_type, $meta_info['meta'], $args ); - } + if ( ! is_array( $post_types ) || empty( $post_types ) ) { + continue; + } + + foreach ( $post_types as $post_type ) { + register_post_meta( $post_type, $meta_info['meta'], $args ); } } } @@ -74,23 +76,21 @@ public function register_custom_meta(): void { * @return array */ private function get_post_meta_array(): array { - $post_meta_array = array( - array( - 'post_type' => array( Pattern::SLUG ), + return [ + [ + 'post_type' => [ Pattern::SLUG ], 'meta' => 'brand_site', 'type' => 'array', - 'show_in_rest' => array( - 'schema' => array( + 'show_in_rest' => [ + 'schema' => [ 'type' => 'array', - 'items' => array( + 'items' => [ 'type' => 'integer', - ), - ), - ), + ], + ], + ], 'single' => true, - ), - ); - - return $post_meta_array; + ], + ]; } } diff --git a/inc/classes/post-type/class-pattern.php b/inc/classes/post-type/class-pattern.php index d47a853..4e4b424 100644 --- a/inc/classes/post-type/class-pattern.php +++ b/inc/classes/post-type/class-pattern.php @@ -32,7 +32,7 @@ class Pattern extends Base { * @return array */ public function get_labels(): array { - return array( + return [ 'name' => _x( 'Pattern Library', 'post type general name', 'onedesign' ), 'singular_name' => _x( 'Pattern Library', 'post type singular name', 'onedesign' ), 'menu_name' => _x( 'Pattern Library', 'admin menu', 'onedesign' ), @@ -47,7 +47,7 @@ public function get_labels(): array { 'parent_item_colon' => __( 'Parent Pattern Library:', 'onedesign' ), 'not_found' => __( 'No Pattern Library found.', 'onedesign' ), 'not_found_in_trash' => __( 'No Pattern Library found in trash.', 'onedesign' ), - ); + ]; } /** @@ -56,14 +56,14 @@ public function get_labels(): array { * @return array */ public function get_args(): array { - return array( + return [ 'public' => false, 'show_ui' => true, 'has_archive' => false, 'show_in_rest' => true, 'menu_position' => 6, - 'supports' => array( 'title', 'editor', 'revisions', 'custom-fields' ), + 'supports' => [ 'title', 'editor', 'revisions', 'custom-fields' ], 'menu_icon' => 'dashicons-networking', - ); + ]; } } diff --git a/inc/classes/post-type/class-template.php b/inc/classes/post-type/class-template.php index 468a8f4..aece1cf 100644 --- a/inc/classes/post-type/class-template.php +++ b/inc/classes/post-type/class-template.php @@ -32,7 +32,7 @@ class Template extends Base { * @return array */ public function get_labels(): array { - return array( + return [ 'name' => _x( 'Template Library', 'post type general name', 'onedesign' ), 'singular_name' => _x( 'Template Library', 'post type singular name', 'onedesign' ), 'menu_name' => _x( 'Template Library', 'admin menu', 'onedesign' ), @@ -47,7 +47,7 @@ public function get_labels(): array { 'parent_item_colon' => __( 'Parent Template Library:', 'onedesign' ), 'not_found' => __( 'No Template Library found.', 'onedesign' ), 'not_found_in_trash' => __( 'No Template Library found in trash.', 'onedesign' ), - ); + ]; } /** @@ -56,14 +56,14 @@ public function get_labels(): array { * @return array */ public function get_args(): array { - return array( + return [ 'public' => false, 'show_ui' => true, 'has_archive' => false, 'show_in_rest' => true, 'menu_position' => 6, - 'supports' => array( 'title', 'editor', 'custom-fields' ), + 'supports' => [ 'title', 'editor', 'custom-fields' ], 'menu_icon' => 'dashicons-media-text', - ); + ]; } } diff --git a/inc/classes/rest/class-basic-options.php b/inc/classes/rest/class-basic-options.php index 2f0c8c2..d7f85fa 100644 --- a/inc/classes/rest/class-basic-options.php +++ b/inc/classes/rest/class-basic-options.php @@ -12,7 +12,6 @@ use WP_REST_Server; use WP_REST_Response; use WP_REST_Request; -use WP_Error; /** * Class Basic_Options @@ -42,7 +41,7 @@ protected function __construct() { * Function to setup hooks. */ public function setup_hooks(): void { - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + add_action( 'rest_api_init', [ $this, 'register_routes' ] ); } /** @@ -55,25 +54,25 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/site-type', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_site_type' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - array( + 'callback' => [ $this, 'get_site_type' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'set_site_type' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - 'args' => array( - 'site_type' => array( + 'callback' => [ $this, 'set_site_type' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + 'args' => [ + 'site_type' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', - ), - ), - ), - ) + ], + ], + ], + ] ); /** @@ -82,27 +81,27 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/shared-sites', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_shared_sites' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - array( + 'callback' => [ $this, 'get_shared_sites' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'set_shared_sites' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - 'args' => array( - 'sites_data' => array( + 'callback' => [ $this, 'set_shared_sites' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + 'args' => [ + 'sites_data' => [ 'required' => true, 'type' => 'array', - 'validate_callback' => function ( $value ): bool { + 'validate_callback' => static function ( $value ): bool { return is_array( $value ); }, - ), - ), - ), - ) + ], + ], + ], + ] ); /** @@ -111,11 +110,11 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/health-check', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'health_check' ), + 'callback' => [ $this, 'health_check' ], 'permission_callback' => 'onedesign_validate_api_key_health_check', - ) + ] ); /** @@ -124,18 +123,18 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/secret-key', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( Secret_Key::class, 'get_secret_key' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - array( + 'callback' => [ Secret_Key::class, 'get_secret_key' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + [ 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( Secret_Key::class, 'regenerate_secret_key' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - ) + 'callback' => [ Secret_Key::class, 'regenerate_secret_key' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + ] ); /** @@ -144,18 +143,18 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/governing-site', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_governing_site' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - array( + 'callback' => [ $this, 'get_governing_site' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + [ 'methods' => WP_REST_Server::DELETABLE, - 'callback' => array( $this, 'remove_governing_site' ), - 'permission_callback' => array( self::class, 'permission_callback' ), - ), - ), + 'callback' => [ $this, 'remove_governing_site' ], + 'permission_callback' => [ self::class, 'permission_callback' ], + ], + ], ); } @@ -171,123 +170,125 @@ public static function permission_callback(): bool { /** * Get the site type. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function get_site_type(): WP_REST_Response|WP_Error { + public function get_site_type(): WP_REST_Response|\WP_Error { $site_type = get_option( Constants::ONEDESIGN_SITE_TYPE, '' ); return rest_ensure_response( - array( + [ 'success' => true, 'site_type' => $site_type, - ) + ] ); } /** * Set the site type. * - * @param WP_REST_Request $request The request object. + * @param \WP_REST_Request $request The request object. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function set_site_type( WP_REST_Request $request ): WP_REST_Response|WP_Error { + public function set_site_type( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $site_type = sanitize_text_field( $request->get_param( 'site_type' ) ); update_option( Constants::ONEDESIGN_SITE_TYPE, $site_type, false ); return rest_ensure_response( - array( + [ 'success' => true, 'site_type' => $site_type, - ) + ] ); } /** * Get shared sites data. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function get_shared_sites(): WP_REST_Response|WP_Error { - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); + public function get_shared_sites(): WP_REST_Response|\WP_Error { + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); return rest_ensure_response( - array( + [ 'success' => true, 'shared_sites' => $shared_sites, - ) + ] ); } /** * Set shared sites data. * - * @param WP_REST_Request $request The request object. + * @param \WP_REST_Request $request The request object. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function set_shared_sites( WP_REST_Request $request ): WP_REST_Response|WP_Error { + public function set_shared_sites( WP_REST_Request $request ): WP_REST_Response|\WP_Error { $body = $request->get_body(); $decoded_body = json_decode( $body, true ); - $sites_data = $decoded_body['sites_data'] ?? array(); + $sites_data = $decoded_body['sites_data'] ?? []; // check if same url exists more than once or not. - $urls = array(); + $urls = []; foreach ( $sites_data as $site ) { if ( isset( $site['siteUrl'] ) && in_array( $site['siteUrl'], $urls, true ) ) { - return new WP_Error( 'duplicate_site_url', __( 'Brand Site already exists.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'duplicate_site_url', __( 'Brand Site already exists.', 'onedesign' ), [ 'status' => 400 ] ); } $urls[] = $site['siteUrl'] ?? ''; } // add unique id to each site if not exists. foreach ( $sites_data as &$site ) { - if ( ! isset( $site['id'] ) || empty( $site['id'] ) ) { - $site['id'] = wp_generate_uuid4(); + if ( isset( $site['id'] ) && ! empty( $site['id'] ) ) { + continue; } + + $site['id'] = wp_generate_uuid4(); } update_option( Constants::ONEDESIGN_SHARED_SITES, $sites_data, false ); return rest_ensure_response( - array( + [ 'success' => true, 'sites_data' => $sites_data, - ) + ] ); } /** * Health check endpoint. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function health_check(): WP_REST_Response|WP_Error { + public function health_check(): WP_REST_Response|\WP_Error { return rest_ensure_response( - array( + [ 'success' => true, 'message' => __( 'Health check passed successfully.', 'onedesign' ), - ) + ] ); } /** * Get governing site url. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function get_governing_site(): WP_REST_Response|WP_Error { + public function get_governing_site(): WP_REST_Response|\WP_Error { $governing_site_url = get_option( Constants::ONEDESIGN_GOVERNING_SITE_URL, '' ); return rest_ensure_response( - array( + [ 'success' => true, 'governing_site_url' => $governing_site_url, - ) + ] ); } @@ -296,14 +297,14 @@ public function get_governing_site(): WP_REST_Response|WP_Error { * * @return \WP_REST_Response|\WP_Error */ - public function remove_governing_site(): WP_REST_Response|WP_Error { + public function remove_governing_site(): WP_REST_Response|\WP_Error { update_option( Constants::ONEDESIGN_GOVERNING_SITE_URL, '', false ); return rest_ensure_response( - array( + [ 'success' => true, 'message' => __( 'Governing site removed successfully.', 'onedesign' ), - ) + ] ); } } diff --git a/inc/classes/rest/class-multisite.php b/inc/classes/rest/class-multisite.php index 0791dc5..56ba541 100644 --- a/inc/classes/rest/class-multisite.php +++ b/inc/classes/rest/class-multisite.php @@ -10,7 +10,6 @@ use OneDesign\Plugin_Configs\{Constants, Secret_Key }; use OneDesign\Traits\Singleton; use OneDesign\Utils; -use WP_Error; use WP_REST_Response; use WP_REST_Server; @@ -44,7 +43,7 @@ protected function __construct() { * @return void */ protected function setup_hooks(): void { - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + add_action( 'rest_api_init', [ $this, 'register_routes' ] ); } /** @@ -59,25 +58,25 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/governing-site', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_multisite_governing_site' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ), - array( + 'callback' => [ $this, 'get_multisite_governing_site' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ], + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'set_multisite_governing_site' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'governing_site_id' => array( + 'callback' => [ $this, 'set_multisite_governing_site' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'governing_site_id' => [ 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', - ), - ), - ), - ) + ], + ], + ], + ] ); /** @@ -86,19 +85,19 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/add-sites', - array( - array( + [ + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'add_multisite_sites' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'site_ids' => array( + 'callback' => [ $this, 'add_multisite_sites' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'site_ids' => [ 'required' => true, 'type' => 'array', - ), - ), - ), - ) + ], + ], + ], + ] ); /** @@ -107,20 +106,20 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/sites', - array( - array( + [ + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_all_multisite_sites' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ), - ) + 'callback' => [ $this, 'get_all_multisite_sites' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ], + ] ); } /** * Get the governing site for multisite setup. * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_multisite_governing_site(): WP_REST_Response { @@ -128,10 +127,10 @@ public function get_multisite_governing_site(): WP_REST_Response { $governing_site = get_site_option( Constants::ONEDESIGN_MULTISITE_GOVERNING_SITE, '' ); return new WP_REST_Response( - array( + [ 'success' => true, 'governing_site' => $governing_site, - ) + ] ); } @@ -140,17 +139,17 @@ public function get_multisite_governing_site(): WP_REST_Response { * * @param \WP_REST_Request $request The REST request. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function set_multisite_governing_site( \WP_REST_Request $request ): WP_REST_Response|WP_Error { + public function set_multisite_governing_site( \WP_REST_Request $request ): WP_REST_Response|\WP_Error { $governing_site_id = filter_var( $request->get_param( 'governing_site_id' ), FILTER_SANITIZE_NUMBER_INT ); if ( empty( $governing_site_id ) || ! is_numeric( $governing_site_id ) ) { - return new WP_Error( + return new \WP_Error( 'invalid_governing_site_id', __( 'Invalid site info provided.', 'onedesign' ), - array( 'status' => 400 ) + [ 'status' => 400 ] ); } @@ -158,10 +157,10 @@ public function set_multisite_governing_site( \WP_REST_Request $request ): WP_RE $is_updated = update_site_option( Constants::ONEDESIGN_MULTISITE_GOVERNING_SITE, $governing_site_id ); if ( ! $is_updated ) { - return new WP_Error( + return new \WP_Error( 'update_failed', __( 'Failed to update governing site.', 'onedesign' ), - array( 'status' => 500 ) + [ 'status' => 500 ] ); } @@ -169,7 +168,6 @@ public function set_multisite_governing_site( \WP_REST_Request $request ): WP_RE $multisite_info = Utils::get_all_multisites_info(); foreach ( $multisite_info as $site ) { - if ( ! switch_to_blog( (int) $site['id'] ) ) { continue; } @@ -189,10 +187,10 @@ public function set_multisite_governing_site( \WP_REST_Request $request ): WP_RE } return new WP_REST_Response( - array( + [ 'success' => true, 'governing_site' => $governing_site_id, - ) + ] ); } @@ -201,17 +199,17 @@ public function set_multisite_governing_site( \WP_REST_Request $request ): WP_RE * * @param \WP_REST_Request $request The REST request. * - * @return WP_REST_Response|WP_Error + * @return \WP_REST_Response|\WP_Error */ - public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Response|WP_Error { + public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Response|\WP_Error { $site_ids = array_map( 'absint', (array) $request->get_param( 'site_ids' ) ); if ( empty( $site_ids ) || ! is_array( $site_ids ) ) { - return new WP_Error( + return new \WP_Error( 'invalid_site_ids', __( 'Invalid site info provided.', 'onedesign' ), - array( 'status' => 400 ) + [ 'status' => 400 ] ); } @@ -219,25 +217,25 @@ public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Respon $governing_site_id = get_site_option( Constants::ONEDESIGN_MULTISITE_GOVERNING_SITE, 0 ); if ( ! $governing_site_id ) { - return new WP_Error( + return new \WP_Error( 'no_governing_site', __( 'No governing site set. Please set a governing site first.', 'onedesign' ), - array( 'status' => 400 ) + [ 'status' => 400 ] ); } // get governing site details. $governing_site_details = get_blog_details( $governing_site_id ); if ( ! $governing_site_details || empty( $governing_site_details->siteurl ) ) { - return new WP_Error( + return new \WP_Error( 'invalid_governing_site', __( 'The governing site could not be found.', 'onedesign' ), - array( 'status' => 400 ) + [ 'status' => 400 ] ); } $governing_site_url = $governing_site_details->siteurl; - $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); + $shared_sites = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); foreach ( $site_ids as $site_id ) { @@ -246,13 +244,13 @@ public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Respon continue; } - $shared_sites[] = array( + $shared_sites[] = [ 'id' => $site_id, 'name' => get_bloginfo( 'name' ), 'url' => get_bloginfo( 'url' ), 'api_key' => get_option( Constants::ONEDESIGN_API_KEY, '' ), 'is_editable' => false, - ); + ]; update_option( Constants::ONEDESIGN_SITE_TYPE, 'brand-site', false ); update_option( Constants::ONEDESIGN_GOVERNING_SITE_URL, $governing_site_url, false ); @@ -265,10 +263,10 @@ public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Respon // update shared sites in governing site. if ( ! switch_to_blog( (int) $governing_site_id ) ) { - return new WP_Error( + return new \WP_Error( sprintf( 'failed_to_switch_blog_%d', $governing_site_id ), __( 'Failed to switch to governing site blog.', 'onedesign' ), - array( 'status' => 500 ) + [ 'status' => 500 ] ); } @@ -277,27 +275,27 @@ public function add_multisite_sites( \WP_REST_Request $request ): WP_REST_Respon restore_current_blog(); return new WP_REST_Response( - array( + [ 'success' => true, 'added_sites' => $site_ids, - ) + ] ); } /** * Get all sites from current multisite setup. * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_all_multisite_sites(): WP_REST_Response { $all_multisites = Utils::get_all_multisites_info(); return new WP_REST_Response( - array( + [ 'success' => true, 'sites' => $all_multisites, - ) + ] ); } } diff --git a/inc/classes/rest/class-patterns.php b/inc/classes/rest/class-patterns.php index 23738d9..857dcc6 100644 --- a/inc/classes/rest/class-patterns.php +++ b/inc/classes/rest/class-patterns.php @@ -12,7 +12,6 @@ use OneDesign\Utils; use WP_REST_Request; use WP_REST_Response; -use WP_Error; use WP_REST_Server; /** @@ -41,7 +40,7 @@ protected function __construct() { * Setup hooks for the class. */ public function setup_hooks(): void { - add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); + add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); } /** @@ -55,11 +54,11 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/local-patterns', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_local_patterns' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_local_patterns' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -68,11 +67,11 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/brand-site-patterns', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_brand_site_patterns' ), + 'callback' => [ $this, 'get_brand_site_patterns' ], 'permission_callback' => 'onedesign_validate_api_key', - ) + ] ); /** @@ -81,11 +80,11 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/get-all-brand-site-patterns', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_all_brand_site_patterns' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_all_brand_site_patterns' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -94,27 +93,27 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/request-remove-brand-site-patterns', - array( + [ 'methods' => WP_REST_Server::DELETABLE, - 'callback' => array( $this, 'request_remove_brand_site_patterns' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'pattern_names' => array( + 'callback' => [ $this, 'request_remove_brand_site_patterns' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'pattern_names' => [ 'required' => true, 'type' => 'array', - 'validate_callback' => function ( $param ): bool { + 'validate_callback' => static function ( $param ): bool { return is_array( $param ) && ! empty( $param ); }, - ), - 'site_id' => array( + ], + 'site_id' => [ 'required' => true, 'type' => 'string', - 'validate_callback' => function ( $param ): bool { + 'validate_callback' => static function ( $param ): bool { return is_string( $param ) && ! empty( $param ); }, - ), - ), - ) + ], + ], + ] ); /** @@ -123,20 +122,20 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/remove-brand-site-patterns', - array( + [ 'methods' => WP_REST_Server::DELETABLE, - 'callback' => array( $this, 'remove_brand_site_patterns' ), + 'callback' => [ $this, 'remove_brand_site_patterns' ], 'permission_callback' => 'onedesign_validate_api_key', - 'args' => array( - 'pattern_names' => array( + 'args' => [ + 'pattern_names' => [ 'required' => true, 'type' => 'array', - 'validate_callback' => function ( $param ): bool { + 'validate_callback' => static function ( $param ): bool { return is_array( $param ) && ! empty( $param ); }, - ), - ), - ) + ], + ], + ] ); /** @@ -145,11 +144,11 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/pattern-categories', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_pattern_categories' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_pattern_categories' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -158,11 +157,11 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/configured-sites', - array( + [ 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_configured_child_sites' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_configured_child_sites' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -171,34 +170,34 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/push-patterns', - array( + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'push_patterns_to_targets' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'pattern_names' => array( + 'callback' => [ $this, 'push_patterns_to_targets' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'pattern_names' => [ 'required' => true, 'type' => 'array', - 'items' => array( 'type' => 'string' ), - 'validate_callback' => function ( $param ): bool { + 'items' => [ 'type' => 'string' ], + 'validate_callback' => static function ( $param ): bool { return is_array( $param ) && ! empty( $param ); }, - ), - 'target_site_ids' => array( + ], + 'target_site_ids' => [ 'required' => true, 'type' => 'array', - 'items' => array( - 'oneOf' => array( - array( 'type' => 'string' ), - array( 'type' => 'integer' ), - ), - ), - 'validate_callback' => function ( $param ): bool { + 'items' => [ + 'oneOf' => [ + [ 'type' => 'string' ], + [ 'type' => 'integer' ], + ], + ], + 'validate_callback' => static function ( $param ): bool { return is_array( $param ) && ! empty( $param ); }, - ), - ), - ) + ], + ], + ] ); /** @@ -207,45 +206,45 @@ public function register_rest_routes(): void { register_rest_route( self::NAMESPACE, '/receive-patterns', - array( + [ 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'receive_patterns' ), + 'callback' => [ $this, 'receive_patterns' ], 'permission_callback' => 'onedesign_validate_api_key', - 'args' => array( - 'patterns_data' => array( + 'args' => [ + 'patterns_data' => [ 'required' => true, 'type' => 'array', - 'validate_callback' => function ( $param ): bool { + 'validate_callback' => static function ( $param ): bool { return is_array( $param ); }, - ), - 'source_site_name' => array( + ], + 'source_site_name' => [ 'required' => false, 'type' => 'string', - ), - ), - ) + ], + ], + ] ); } /** * Request to remove patterns from brand sites. * - * @param WP_REST_Request $request The REST request object. + * @param \WP_REST_Request $request The REST request object. * - * @return WP_Error|WP_REST_Response + * @return \WP_Error|\WP_REST_Response */ - public function request_remove_brand_site_patterns( WP_REST_Request $request ): WP_Error|WP_REST_Response { + public function request_remove_brand_site_patterns( WP_REST_Request $request ): \WP_Error|WP_REST_Response { if ( ! Utils::is_governing_site() ) { - return new WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), array( 'status' => 403 ) ); + return new \WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), [ 'status' => 403 ] ); } $pattern_name = $request->get_param( 'pattern_names' ); $site_id = $request->get_param( 'site_id' ); if ( empty( $pattern_name ) || empty( $site_id ) ) { - return new WP_Error( 'invalid_params', __( 'Pattern name and site ID are required.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'invalid_params', __( 'Pattern name and site ID are required.', 'onedesign' ), [ 'status' => 400 ] ); } // Use the option name from your settings class. @@ -254,37 +253,37 @@ public function request_remove_brand_site_patterns( WP_REST_Request $request ): if ( isset( $site['id'] ) && (string) $site['id'] === (string) $site_id ) { $remote_api_key = $site['api_key'] ?? ''; if ( empty( $remote_api_key ) ) { - return new WP_Error( 'no_api_key', __( 'API key for the target site is missing in configuration.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'no_api_key', __( 'API key for the target site is missing in configuration.', 'onedesign' ), [ 'status' => 400 ] ); } break; } } if ( ! isset( $remote_api_key ) ) { - return new WP_Error( 'site_not_found', __( 'Target site not found in configuration.', 'onedesign' ), array( 'status' => 404 ) ); + return new \WP_Error( 'site_not_found', __( 'Target site not found in configuration.', 'onedesign' ), [ 'status' => 404 ] ); } $remote_url = Utils::build_api_endpoint( $site['url'], 'remove-brand-site-patterns' ); $response = wp_safe_remote_request( $remote_url, - array( + [ 'method' => 'DELETE', - 'headers' => array( + 'headers' => [ 'X-OneDesign-Token' => $remote_api_key, 'Content-Type' => 'application/json', - ), + ], 'body' => wp_json_encode( - array( + [ 'pattern_names' => $pattern_name, - ) + ] ), 'timeout' => 45, - ) + ] ); if ( is_wp_error( $response ) ) { - return new WP_Error( 'request_failed', __( 'Failed to communicate with the target site.', 'onedesign' ), array( 'status' => 500 ) ); + return new \WP_Error( 'request_failed', __( 'Failed to communicate with the target site.', 'onedesign' ), [ 'status' => 500 ] ); } $status_code = wp_remote_retrieve_response_code( $response ); @@ -293,62 +292,64 @@ public function request_remove_brand_site_patterns( WP_REST_Request $request ): if ( $status_code >= 200 && $status_code < 300 ) { return new WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Pattern removal request sent successfully.', 'onedesign' ), 'response' => $decoded_body, - ), + ], 200 ); - } else { - $error_message = isset( $decoded_body['message'] ) ? $decoded_body['message'] : __( 'Unknown error from remote site.', 'onedesign' ); - return new WP_Error( - 'remote_error', - // translators: %1$s is the error message, %2$d is the HTTP status code. - sprintf( __( 'Error removing pattern: %1$s (Status: %2$d)', 'onedesign' ), $error_message, $status_code ), - array( 'status' => $status_code ) - ); } + + $error_message = isset( $decoded_body['message'] ) ? $decoded_body['message'] : __( 'Unknown error from remote site.', 'onedesign' ); + return new \WP_Error( + 'remote_error', + // translators: %1$s is the error message, %2$d is the HTTP status code. + sprintf( __( 'Error removing pattern: %1$s (Status: %2$d)', 'onedesign' ), $error_message, $status_code ), + [ 'status' => $status_code ] + ); } /** * Remove patterns from brand site patterns. * - * @param WP_REST_Request $request The REST request object. + * @param \WP_REST_Request $request The REST request object. * - * @return WP_Error|WP_REST_Response + * @return \WP_Error|\WP_REST_Response */ - public function remove_brand_site_patterns( WP_REST_Request $request ): WP_Error|WP_REST_Response { + public function remove_brand_site_patterns( WP_REST_Request $request ): \WP_Error|WP_REST_Response { $pattern_names = $request->get_param( 'pattern_names' ); if ( empty( $pattern_names ) || ! is_array( $pattern_names ) ) { - return new WP_Error( 'invalid_params', __( 'Pattern names must be provided as an array.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'invalid_params', __( 'Pattern names must be provided as an array.', 'onedesign' ), [ 'status' => 400 ] ); } // Remove the patterns from the brand site patterns option. - $brand_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, array() ); + $brand_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, [] ); if ( empty( $brand_patterns ) ) { - return new WP_Error( 'no_patterns_found', __( 'No patterns found for this brand site.', 'onedesign' ), array( 'status' => 404 ) ); + return new \WP_Error( 'no_patterns_found', __( 'No patterns found for this brand site.', 'onedesign' ), [ 'status' => 404 ] ); } - $updated_patterns = array(); + $updated_patterns = []; foreach ( $brand_patterns as $pattern ) { - if ( ! in_array( $pattern['name'], $pattern_names, true ) ) { - $updated_patterns[ $pattern['name'] ] = $pattern; // Keep patterns not in the removal list. + if ( in_array( $pattern['name'], $pattern_names, true ) ) { + continue; } + + $updated_patterns[ $pattern['name'] ] = $pattern; // Keep patterns not in the removal list. } if ( count( $updated_patterns ) === count( $brand_patterns ) ) { - return new WP_Error( 'no_patterns_removed', __( 'No patterns were removed. Please check the pattern names provided.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'no_patterns_removed', __( 'No patterns were removed. Please check the pattern names provided.', 'onedesign' ), [ 'status' => 400 ] ); } update_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, $updated_patterns, false ); return new WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Brand site patterns removed successfully.', 'onedesign' ), - ), + ], 200 ); } @@ -356,24 +357,24 @@ public function remove_brand_site_patterns( WP_REST_Request $request ): WP_Error /** * Get all brand site patterns from child sites. * - * @return WP_Error|WP_REST_Response + * @return \WP_Error|\WP_REST_Response */ - public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response { + public function get_all_brand_site_patterns(): \WP_Error|WP_REST_Response { if ( ! Utils::is_governing_site() ) { - return new WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), array( 'status' => 403 ) ); + return new \WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), [ 'status' => 403 ] ); } // Call every child site to get their patterns. $child_sites = $this->get_compatible_sites_object(); if ( empty( $child_sites ) ) { - return new WP_Error( 'no_child_sites', __( 'No child sites configured to receive patterns.', 'onedesign' ), array( 'status' => 404 ) ); + return new \WP_Error( 'no_child_sites', __( 'No child sites configured to receive patterns.', 'onedesign' ), [ 'status' => 404 ] ); } - $all_patterns = array(); - $error_logs = array(); + $all_patterns = []; + $error_logs = []; foreach ( $child_sites as $site ) { - $site_patterns = array(); + $site_patterns = []; $remote_api_key = $site['api_key'] ?? ''; $remote_url = Utils::build_api_endpoint( $site['url'], 'brand-site-patterns' ) . '?timestamp=' . time(); // Add timestamp to avoid caching issues. @@ -383,13 +384,13 @@ public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response { $response = wp_safe_remote_get( $remote_url, - array( - 'headers' => array( + [ + 'headers' => [ 'X-OneDesign-Token' => $remote_api_key, 'Content-Type' => 'application/json', - ), + ], 'timeout' => 45, - ) + ] ); if ( is_wp_error( $response ) ) { $error_logs[] = $response; @@ -405,43 +406,45 @@ public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response { foreach ( $decoded_body['patterns'] as $pattern ) { // Ensure the pattern has a name and title. - if ( ! empty( $pattern['name'] ) && ! empty( $pattern['title'] ) ) { - // Use name as a key to avoid duplicates. - $site_patterns[] = array( - 'name' => $pattern['name'], - 'title' => $pattern['title'], - 'content' => $pattern['content'] ?? '', - 'description' => $pattern['description'] ?? '', - 'categories' => $pattern['categories'] ?? array(), - 'keywords' => $pattern['keywords'] ?? array(), - 'viewportWidth' => $pattern['viewportWidth'] ?? null, - 'blockTypes' => $pattern['blockTypes'] ?? null, - 'postTypes' => $pattern['postTypes'] ?? null, - 'templateTypes' => $pattern['templateTypes'] ?? null, - 'inserter' => $pattern['inserter'] ?? true, - 'source_site' => $site['name'] ?? 'Unknown Site', // Add the source site name for context. - ); + if ( empty( $pattern['name'] ) || empty( $pattern['title'] ) ) { + continue; } + + // Use name as a key to avoid duplicates. + $site_patterns[] = [ + 'name' => $pattern['name'], + 'title' => $pattern['title'], + 'content' => $pattern['content'] ?? '', + 'description' => $pattern['description'] ?? '', + 'categories' => $pattern['categories'] ?? [], + 'keywords' => $pattern['keywords'] ?? [], + 'viewportWidth' => $pattern['viewportWidth'] ?? null, + 'blockTypes' => $pattern['blockTypes'] ?? null, + 'postTypes' => $pattern['postTypes'] ?? null, + 'templateTypes' => $pattern['templateTypes'] ?? null, + 'inserter' => $pattern['inserter'] ?? true, + 'source_site' => $site['name'] ?? 'Unknown Site', // Add the source site name for context. + ]; } } } else { // Log or handle error response from a child site. $error_message = $decoded_body['message'] ?? __( 'Unknown error from remote site.', 'onedesign' ); - $error_logs[] = array( + $error_logs[] = [ 'site' => $site['name'] ?? __( 'Unknown Site', 'onedesign' ), 'status_code' => $status_code, 'message' => $error_message, - ); + ]; } $all_patterns[ $site['id'] ] = $site_patterns; // Store patterns by site ID. } return new WP_REST_Response( - array( + [ 'success' => true, 'patterns' => $all_patterns, 'error_logs' => $error_logs, - ), + ], 200 ); } @@ -449,27 +452,27 @@ public function get_all_brand_site_patterns(): WP_Error|WP_REST_Response { /** * Get brand site patterns. * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_brand_site_patterns(): WP_REST_Response { // Use the option name from your settings class. - $brand_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, array() ); + $brand_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, [] ); if ( empty( $brand_patterns ) ) { return new WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'No patterns found for this brand site.', 'onedesign' ), - ), + ], 404 ); } return new WP_REST_Response( - array( + [ 'success' => true, 'patterns' => $brand_patterns, - ), + ], 200 ); } @@ -477,7 +480,7 @@ public function get_brand_site_patterns(): WP_REST_Response { /** * Get all local patterns (both registered and user-created). * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_local_patterns(): WP_REST_Response { $patterns = $this->get_all_local_patterns_map(); @@ -491,69 +494,71 @@ public function get_local_patterns(): WP_REST_Response { /** * Get pattern categories. * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_pattern_categories(): WP_REST_Response { if ( ! class_exists( 'WP_Block_Pattern_Categories_Registry' ) ) { return rest_ensure_response( - array( + [ 'success' => false, 'message' => __( 'Pattern categories registry not found.', 'onedesign' ), - ) + ] ); } $categories = \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); $user_created_categories = get_terms( - array( + [ 'taxonomy' => 'wp_pattern_category', 'hide_empty' => false, - ) + ] ); if ( ! is_wp_error( $user_created_categories ) && ! empty( $user_created_categories ) && is_array( $user_created_categories ) ) { foreach ( $user_created_categories as $category ) { // Ensure the category has a name and label. - if ( ! empty( $category->name ) && ! empty( $category->slug ) ) { - // Check if category already exists to avoid duplicates. - $exists = false; - foreach ( $categories as $existing_category ) { - if ( $existing_category['name'] === $category->slug ) { - $exists = true; - break; - } - } - if ( $exists ) { - continue; // Skip if the category already exists. + if ( empty( $category->name ) || empty( $category->slug ) ) { + continue; + } + + // Check if category already exists to avoid duplicates. + $exists = false; + foreach ( $categories as $existing_category ) { + if ( $existing_category['name'] === $category->slug ) { + $exists = true; + break; } - // Add user-created category to the list. - $categories[] = array( - 'name' => $category->slug, - 'label' => $category->name, - ); } + if ( $exists ) { + continue; // Skip if the category already exists. + } + // Add user-created category to the list. + $categories[] = [ + 'name' => $category->slug, + 'label' => $category->name, + ]; } } return new WP_REST_Response( - array( + [ 'success' => true, 'categories' => $categories, - ) + ] ); } /** * Get configured child sites (for a parent site type). * - * @return WP_REST_Response + * @return \WP_REST_Response */ public function get_configured_child_sites(): WP_REST_Response { if ( ! Utils::is_governing_site() ) { - return new WP_REST_Response( array(), 200 ); // Return empty if not a parent site. + return new WP_REST_Response( [], 200 ); // Return empty if not a parent site. } // Use the option name from your settings class. @@ -565,14 +570,14 @@ public function get_configured_child_sites(): WP_REST_Response { /** * Push patterns to target sites. * - * @param WP_REST_Request $request The REST request object. + * @param \WP_REST_Request $request The REST request object. * - * @return WP_Error|WP_REST_Response + * @return \WP_Error|\WP_REST_Response */ public function push_patterns_to_targets( WP_REST_Request $request ) { if ( ! Utils::is_governing_site() ) { - return new WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), array( 'status' => 403 ) ); + return new \WP_Error( 'not_parent_site', __( 'This site is not configured as a parent site.', 'onedesign' ), [ 'status' => 403 ] ); } $pattern_names = $request->get_param( 'pattern_names' ); @@ -585,39 +590,41 @@ public function push_patterns_to_targets( WP_REST_Request $request ) { // Get all patterns (both registered and user-created). $local_patterns_map = $this->get_all_local_patterns_map(); - $patterns_to_push = array(); + $patterns_to_push = []; foreach ( $pattern_names as $name ) { - if ( isset( $local_patterns_map[ $name ] ) ) { - $pattern_data = $local_patterns_map[ $name ]; - $patterns_to_push[] = array( - 'name' => $pattern_data['name'] ?? '', - 'title' => $pattern_data['title'] ?? '', - 'content' => $pattern_data['content'] ?? '', - 'description' => $pattern_data['description'] ?? '', - 'categories' => $pattern_data['categories'] ?? array(), - 'keywords' => $pattern_data['keywords'] ?? array(), - 'viewportWidth' => $pattern_data['viewportWidth'] ?? null, - 'blockTypes' => $pattern_data['blockTypes'] ?? null, - 'postTypes' => $pattern_data['postTypes'] ?? null, - 'templateTypes' => $pattern_data['templateTypes'] ?? null, - 'inserter' => $pattern_data['inserter'] ?? true, - 'source' => $pattern_data['source'] ?? 'registered', - 'id' => $pattern_data['id'] ?? null, // For user patterns. - ); + if ( ! isset( $local_patterns_map[ $name ] ) ) { + continue; } + + $pattern_data = $local_patterns_map[ $name ]; + $patterns_to_push[] = [ + 'name' => $pattern_data['name'] ?? '', + 'title' => $pattern_data['title'] ?? '', + 'content' => $pattern_data['content'] ?? '', + 'description' => $pattern_data['description'] ?? '', + 'categories' => $pattern_data['categories'] ?? [], + 'keywords' => $pattern_data['keywords'] ?? [], + 'viewportWidth' => $pattern_data['viewportWidth'] ?? null, + 'blockTypes' => $pattern_data['blockTypes'] ?? null, + 'postTypes' => $pattern_data['postTypes'] ?? null, + 'templateTypes' => $pattern_data['templateTypes'] ?? null, + 'inserter' => $pattern_data['inserter'] ?? true, + 'source' => $pattern_data['source'] ?? 'registered', + 'id' => $pattern_data['id'] ?? null, // For user patterns. + ]; } if ( empty( $patterns_to_push ) ) { - return new WP_Error( 'no_patterns_selected', __( 'No valid patterns selected to push.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'no_patterns_selected', __( 'No valid patterns selected to push.', 'onedesign' ), [ 'status' => 400 ] ); } - $results = array(); + $results = []; $current_site_name = get_bloginfo( 'name' ); foreach ( $target_site_ids as $site_id ) { $target_site = array_filter( $configured_child_sites, - function ( $site ) use ( $site_id ) { + static function ( $site ) use ( $site_id ) { return isset( $site['id'] ) && $site['id'] === $site_id; } ); @@ -625,10 +632,10 @@ function ( $site ) use ( $site_id ) { $target_site = reset( $target_site ); // Get the first match, should be unique by ID. if ( empty( $target_site ) ) { - $results[ $site_id ] = array( + $results[ $site_id ] = [ 'success' => false, 'message' => __( 'Target site not found in configuration.', 'onedesign' ), - ); + ]; continue; } @@ -637,55 +644,55 @@ function ( $site ) use ( $site_id ) { $remote_url = Utils::build_api_endpoint( $target_site['url'], 'receive-patterns' ); if ( empty( $remote_api_key ) ) { - $results[ $site_id ] = array( + $results[ $site_id ] = [ 'success' => false, 'message' => __( 'API key for the target site is missing in configuration.', 'onedesign' ), - ); + ]; continue; } $response = wp_safe_remote_post( $remote_url, - array( + [ 'method' => 'POST', - 'headers' => array( + 'headers' => [ 'X-OneDesign-Token' => $remote_api_key, 'Content-Type' => 'application/json', - ), + ], 'body' => wp_json_encode( - array( + [ 'patterns_data' => $patterns_to_push, 'source_site_name' => $current_site_name, - ) + ] ), 'timeout' => 45, - ) + ] ); if ( is_wp_error( $response ) ) { - $results[ $site_id ] = array( + $results[ $site_id ] = [ 'success' => false, 'message' => $response->get_error_message(), - ); + ]; } else { $status_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $decoded_body = json_decode( $body, true ); if ( $status_code >= 200 && $status_code < 300 ) { - $results[ $site_id ] = array( + $results[ $site_id ] = [ 'success' => true, 'message' => __( 'Patterns pushed successfully.', 'onedesign' ), 'response' => $decoded_body, - ); + ]; } else { $error_message = $decoded_body['message'] ?? __( 'Unknown error from remote site.', 'onedesign' ); - $results[ $site_id ] = array( + $results[ $site_id ] = [ 'success' => false, // translators: %1$s is the error message, %2$d is the HTTP status code. 'message' => sprintf( __( 'Error pushing patterns: %1$s (Status: %2$d)', 'onedesign' ), $error_message, $status_code ), 'response_body' => $body, - ); + ]; } } } @@ -699,30 +706,32 @@ function ( $site ) use ( $site_id ) { * @return array */ private function get_all_local_patterns_map(): array { - $patterns_map = array(); + $patterns_map = []; $pattern_categories = \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); $user_created_categories = get_terms( - array( + [ 'taxonomy' => 'wp_pattern_category', 'hide_empty' => false, - ) + ] ); if ( ! is_wp_error( $user_created_categories ) && ! empty( $user_created_categories ) && is_array( $user_created_categories ) ) { foreach ( $user_created_categories as $category ) { // Ensure the category has a name and label. - if ( ! empty( $category->name ) && ! empty( $category->slug ) ) { - $pattern_categories[] = array( - 'name' => $category->slug, - 'label' => $category->name, - ); + if ( empty( $category->name ) || empty( $category->slug ) ) { + continue; } + + $pattern_categories[] = [ + 'name' => $category->slug, + 'label' => $category->name, + ]; } } - $pattern_category_name_to_label = array(); + $pattern_category_name_to_label = []; if ( ! empty( $pattern_categories ) && is_array( $pattern_categories ) ) { foreach ( $pattern_categories as $category ) { $pattern_category_name_to_label[ $category['name'] ] = $category['label']; @@ -738,70 +747,70 @@ private function get_all_local_patterns_map(): array { } // Map pattern category name to labels. - $pattern['category_labels'] = array(); + $pattern['category_labels'] = []; if ( ! empty( $pattern['categories'] ) && is_array( $pattern['categories'] ) ) { foreach ( $pattern['categories'] as $category_name ) { $pattern['category_labels'][ $category_name ] = $pattern_category_name_to_label[ $category_name ] ?? $category_name; } } else { - $pattern['category_labels'] = array(); // Ensure it's an empty array if no categories. + $pattern['category_labels'] = []; // Ensure it's an empty array if no categories. } - $patterns_map[ $pattern['name'] ] = array( + $patterns_map[ $pattern['name'] ] = [ 'name' => $pattern['name'], 'title' => $pattern['title'], 'description' => $pattern['description'] ?? '', 'content' => $pattern['content'], - 'categories' => $pattern['categories'] ?? array(), + 'categories' => $pattern['categories'] ?? [], 'category_labels' => $pattern['category_labels'], - 'keywords' => $pattern['keywords'] ?? array(), + 'keywords' => $pattern['keywords'] ?? [], 'viewportWidth' => $pattern['viewportWidth'] ?? null, 'blockTypes' => $pattern['blockTypes'] ?? null, 'postTypes' => $pattern['postTypes'] ?? null, 'templateTypes' => $pattern['templateTypes'] ?? null, 'inserter' => $pattern['inserter'] ?? true, 'source' => 'registered', - ); + ]; } // Get user-created patterns from wp_block posts. $user_patterns = get_posts( - array( + [ 'post_type' => 'wp_block', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'suppress_filters' => false, - ) + ] ); foreach ( $user_patterns as $pattern_post ) { // Get pattern categories (stored as wp_pattern_category taxonomy). - $categories = wp_get_object_terms( $pattern_post->ID, 'wp_pattern_category', array( 'fields' => 'slugs' ) ); + $categories = wp_get_object_terms( $pattern_post->ID, 'wp_pattern_category', [ 'fields' => 'slugs' ] ); if ( is_wp_error( $categories ) ) { - $categories = array(); + $categories = []; } // Map pattern category name to labels. - $pattern['category_labels'] = array(); + $pattern['category_labels'] = []; if ( ! empty( $categories ) && is_array( $categories ) ) { foreach ( $categories as $category_name ) { $pattern['category_labels'][ $category_name ] = $pattern_category_name_to_label[ $category_name ] ?? $category_name; } } else { - $pattern['category_labels'] = array(); // Ensure it's an empty array if no categories. + $pattern['category_labels'] = []; // Ensure it's an empty array if no categories. } // Get pattern keywords (stored as meta). $keywords = get_post_meta( $pattern_post->ID, 'wp_pattern_keywords', true ); if ( ! is_array( $keywords ) ) { - $keywords = array(); + $keywords = []; } $pattern_name = 'user-pattern-' . $pattern_post->ID; - $patterns_map[ $pattern_name ] = array( + $patterns_map[ $pattern_name ] = [ 'name' => $pattern_name, 'title' => $pattern_post->post_title, 'description' => $pattern_post->post_excerpt, @@ -816,7 +825,7 @@ private function get_all_local_patterns_map(): array { 'inserter' => true, 'source' => 'user', 'id' => $pattern_post->ID, - ); + ]; } return $patterns_map; @@ -827,25 +836,25 @@ private function get_all_local_patterns_map(): array { * * This endpoint is primarily for child sites to receive patterns pushed from the parent site. * - * @param WP_REST_Request $request The REST request object. + * @param \WP_REST_Request $request The REST request object. * - * @return WP_Error|WP_REST_Response + * @return \WP_Error|\WP_REST_Response */ - public function receive_patterns( WP_REST_Request $request ): WP_Error|WP_REST_Response { + public function receive_patterns( WP_REST_Request $request ): \WP_Error|WP_REST_Response { // This endpoint is primarily for child sites, but a site could technically receive even if set as parent if token matches. if ( ! Utils::is_brand_site() ) { - return new WP_Error( 'not_child_site', __( 'This site is not configured as a child site to receive patterns.', 'onedesign' ), array( 'status' => 403 ) ); + return new \WP_Error( 'not_child_site', __( 'This site is not configured as a child site to receive patterns.', 'onedesign' ), [ 'status' => 403 ] ); } $patterns_data = $request->get_param( 'patterns_data' ); if ( empty( $patterns_data ) || ! is_array( $patterns_data ) ) { - return new WP_Error( 'no_patterns_data', __( 'No patterns data received.', 'onedesign' ), array( 'status' => 400 ) ); + return new \WP_Error( 'no_patterns_data', __( 'No patterns data received.', 'onedesign' ), [ 'status' => 400 ] ); } // Get existing patterns from option. - $existing_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, array() ); + $existing_patterns = get_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, [] ); foreach ( $patterns_data as $pattern ) { if ( empty( $pattern['name'] ) || empty( $pattern['title'] ) || ! isset( $pattern['content'] ) ) { @@ -854,22 +863,22 @@ public function receive_patterns( WP_REST_Request $request ): WP_Error|WP_REST_R $name = sanitize_key( $pattern['name'] ); // Prepare args, same as before. - $pattern_args = array( + $pattern_args = [ 'name' => $name, 'title' => sanitize_text_field( $pattern['title'] ), 'content' => $pattern['content'], 'description' => isset( $pattern['description'] ) ? sanitize_text_field( $pattern['description'] ) : '', - 'categories' => isset( $pattern['categories'] ) && is_array( $pattern['categories'] ) ? array_map( 'sanitize_text_field', $pattern['categories'] ) : array(), - 'keywords' => isset( $pattern['keywords'] ) && is_array( $pattern['keywords'] ) ? array_map( 'sanitize_text_field', $pattern['keywords'] ) : array(), + 'categories' => isset( $pattern['categories'] ) && is_array( $pattern['categories'] ) ? array_map( 'sanitize_text_field', $pattern['categories'] ) : [], + 'keywords' => isset( $pattern['keywords'] ) && is_array( $pattern['keywords'] ) ? array_map( 'sanitize_text_field', $pattern['keywords'] ) : [], 'viewportWidth' => isset( $pattern['viewportWidth'] ) ? intval( $pattern['viewportWidth'] ) : null, 'blockTypes' => isset( $pattern['blockTypes'] ) && is_array( $pattern['blockTypes'] ) ? array_map( 'sanitize_text_field', $pattern['blockTypes'] ) : null, 'postTypes' => isset( $pattern['postTypes'] ) && is_array( $pattern['postTypes'] ) ? array_map( 'sanitize_text_field', $pattern['postTypes'] ) : null, 'templateTypes' => isset( $pattern['templateTypes'] ) && is_array( $pattern['templateTypes'] ) ? array_map( 'sanitize_text_field', $pattern['templateTypes'] ) : null, 'inserter' => isset( $pattern['inserter'] ) ? (bool) $pattern['inserter'] : true, - ); + ]; $pattern_args = array_filter( $pattern_args, - function ( $value ) { + static function ( $value ) { return ! is_null( $value ); } ); @@ -882,12 +891,12 @@ function ( $value ) { update_option( Constants::ONEDESIGN_BRAND_SITE_PATTERNS, $existing_patterns, false ); return new WP_REST_Response( - array( + [ 'success' => true, 'message' => sprintf( __( 'Patterns processed.', 'onedesign' ), ), - ), + ], 200 ); } @@ -898,9 +907,9 @@ function ( $value ) { * @return array List of configured child sites. */ public function get_compatible_sites_object(): array { - $children = get_option( Constants::ONEDESIGN_SHARED_SITES, array() ); + $children = get_option( Constants::ONEDESIGN_SHARED_SITES, [] ); if ( empty( $children ) || ! is_array( $children ) ) { - return array(); // Return an empty array if no children configured. + return []; // Return an empty array if no children configured. } return $children; diff --git a/inc/classes/rest/class-templates.php b/inc/classes/rest/class-templates.php index 913a82c..20127ea 100644 --- a/inc/classes/rest/class-templates.php +++ b/inc/classes/rest/class-templates.php @@ -39,7 +39,7 @@ protected function __construct() { * @return void */ protected function setup_hooks(): void { - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + add_action( 'rest_api_init', [ $this, 'register_routes' ] ); } /** @@ -57,11 +57,11 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/all', - array( + [ 'methods' => \WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_all_templates' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_all_templates' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -70,11 +70,11 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/connected-sites', - array( + [ 'methods' => \WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_templates_from_connected_sites' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - ) + 'callback' => [ $this, 'get_templates_from_connected_sites' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + ] ); /** @@ -83,32 +83,32 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/shared', - array( - array( + [ + [ 'methods' => \WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_shared_templates' ), + 'callback' => [ $this, 'get_shared_templates' ], 'permission_callback' => 'onedesign_validate_api_key', - ), - array( + ], + [ 'methods' => \WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'create_templates' ), + 'callback' => [ $this, 'create_templates' ], 'permission_callback' => 'onedesign_validate_api_key', - 'args' => array( - 'templates' => array( + 'args' => [ + 'templates' => [ 'required' => true, 'type' => 'array', - ), - 'patterns' => array( + ], + 'patterns' => [ 'required' => false, 'type' => 'array', - ), - 'template_parts' => array( + ], + 'template_parts' => [ 'required' => false, 'type' => 'array', - ), - ), - ), - ) + ], + ], + ], + ] ); /** @@ -117,21 +117,21 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/apply', - array( + [ 'methods' => \WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'apply_templates_to_sites' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'sites' => array( + 'callback' => [ $this, 'apply_templates_to_sites' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'sites' => [ 'required' => true, 'type' => 'array', - ), - 'templates' => array( + ], + 'templates' => [ 'required' => true, 'type' => 'array', - ), - ), - ) + ], + ], + ] ); /** @@ -140,21 +140,21 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/remove', - array( + [ 'methods' => \WP_REST_Server::DELETABLE, - 'callback' => array( $this, 'remove_template' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'template_ids' => array( + 'callback' => [ $this, 'remove_template' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'template_ids' => [ 'required' => true, 'type' => 'array', - ), - 'site' => array( + ], + 'site' => [ 'required' => true, 'type' => 'string', - ), - ), - ) + ], + ], + ] ); /** @@ -163,21 +163,21 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/remove-site-templates', - array( + [ 'methods' => \WP_REST_Server::DELETABLE, - 'callback' => array( $this, 'remove_template_from_brand_site' ), + 'callback' => [ $this, 'remove_template_from_brand_site' ], 'permission_callback' => 'onedesign_validate_api_key', - 'args' => array( - 'template_ids' => array( + 'args' => [ + 'template_ids' => [ 'required' => true, 'type' => 'array', - ), - 'is_remove_all' => array( + ], + 'is_remove_all' => [ 'required' => false, 'type' => 'boolean', - ), - ), - ) + ], + ], + ] ); /** @@ -186,21 +186,21 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/resync', - array( + [ 'methods' => \WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'resync_applied_templates' ), - 'permission_callback' => array( Basic_Options::class, 'permission_callback' ), - 'args' => array( - 'sites' => array( + 'callback' => [ $this, 'resync_applied_templates' ], + 'permission_callback' => [ Basic_Options::class, 'permission_callback' ], + 'args' => [ + 'sites' => [ 'required' => true, 'type' => 'array', - ), - 'templates' => array( + ], + 'templates' => [ 'required' => true, 'type' => 'array', - ), - ), - ) + ], + ], + ] ); /** @@ -209,17 +209,17 @@ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/create-synced-patterns', - array( + [ 'methods' => \WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'create_synced_patterns' ), + 'callback' => [ $this, 'create_synced_patterns' ], 'permission_callback' => 'onedesign_validate_api_key', - 'args' => array( - 'synced_patterns' => array( + 'args' => [ + 'synced_patterns' => [ 'required' => false, 'type' => 'array', - ), - ), - ) + ], + ], + ] ); } @@ -233,32 +233,34 @@ public function register_routes(): void { public function create_synced_patterns( \WP_REST_Request $request ): \WP_REST_Response { $synced_patterns = $request->get_param( 'synced_patterns' ); - $existing_synced_patterns = get_option( Constants::ONEDESIGN_SHARED_SYNCED_PATTERNS, array() ); + $existing_synced_patterns = get_option( Constants::ONEDESIGN_SHARED_SYNCED_PATTERNS, [] ); if ( ! is_array( $existing_synced_patterns ) ) { - $existing_synced_patterns = array(); + $existing_synced_patterns = []; } // Merge new synced patterns with existing ones, avoiding duplicates based on 'id'. foreach ( $synced_patterns as $pattern ) { - if ( isset( $pattern['id'] ) && ! array_filter( $existing_synced_patterns, fn( $t ) => $t['id'] === $pattern['id'] ) ) { - $existing_synced_patterns[] = $pattern; + if ( ! isset( $pattern['id'] ) || array_filter( $existing_synced_patterns, static fn( $t ) => $t['id'] === $pattern['id'] ) ) { + continue; } + + $existing_synced_patterns[] = $pattern; } update_option( Constants::ONEDESIGN_SHARED_SYNCED_PATTERNS, $existing_synced_patterns, false ); // need to actual create posts so that in governing site I can map existing synced pattern. - $created_posts = array(); - $error_logs = array(); + $created_posts = []; + $error_logs = []; foreach ( $existing_synced_patterns as $sync_pattern ) { // check if same post_name don't exists. $existing_post = get_posts( - array( + [ 'post_type' => 'wp_block', 'post_name' => sanitize_text_field( $sync_pattern['slug'] ), 'post_status' => 'publish', 'numberposts' => 1, - ), + ], ); if ( ! empty( $existing_post ) ) { @@ -266,13 +268,13 @@ public function create_synced_patterns( \WP_REST_Request $request ): \WP_REST_Re continue; } - $post_data = array( + $post_data = [ 'post_type' => 'wp_block', 'post_title' => isset( $sync_pattern['title'] ) ? sanitize_text_field( $sync_pattern['title'] ) : '', 'post_name' => isset( $sync_pattern['slug'] ) ? sanitize_text_field( $sync_pattern['slug'] ) : '', 'post_status' => 'publish', 'post_content' => $sync_pattern['content'], - ); + ]; $post_id = wp_insert_post( $post_data ); @@ -288,22 +290,22 @@ public function create_synced_patterns( \WP_REST_Request $request ): \WP_REST_Re } // update brand site post ids option. - $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, array() ); + $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, [] ); if ( ! is_array( $brand_site_post_ids ) ) { - $brand_site_post_ids = array(); + $brand_site_post_ids = []; } $brand_site_post_ids = array_merge( $brand_site_post_ids, array_values( $created_posts ) ); update_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, $brand_site_post_ids, false ); return new \WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Synced patterns created successfully.', 'onedesign' ), 'synced_patterns' => $existing_synced_patterns, 'created_posts' => $created_posts, 'error_logs' => $error_logs, - ), + ], 200 ); } @@ -321,10 +323,10 @@ public function resync_applied_templates( \WP_REST_Request $request ): \WP_REST_ if ( empty( $sites ) || empty( $templates ) ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Sites and templates parameters are required.', 'onedesign' ), - ), + ], 400 ); } @@ -343,12 +345,12 @@ public function resync_applied_templates( \WP_REST_Request $request ): \WP_REST_ $apply_template_response = $this->apply_templates_to_sites( $apply_request ); return new \WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Templates re-synced successfully to the selected sites.', 'onedesign' ), 'remove_response' => $remove_template_response->get_data(), 'apply_response' => $apply_template_response->get_data(), - ), + ], 200 ); } @@ -366,68 +368,67 @@ public function remove_template_from_brand_site( \WP_REST_Request $request ): \W if ( empty( $template_ids ) ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Template IDs parameter is required.', 'onedesign' ), - ), + ], 400 ); } - $existing_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, array() ); + $existing_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, [] ); if ( ! is_array( $existing_templates ) ) { - $existing_templates = array(); + $existing_templates = []; } // Remove templates based on 'id'. - $delete_logs = array(); + $delete_logs = []; if ( $is_remove_all ) { - $updated_templates = array(); + $updated_templates = []; // get brand site post ids and remove them. - $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, array() ); + $brand_site_post_ids = get_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, [] ); if ( is_array( $brand_site_post_ids ) ) { foreach ( $brand_site_post_ids as $post_id ) { $deleted = wp_delete_post( $post_id, true ); if ( is_wp_error( $deleted ) ) { - $delete_logs[] = array( + $delete_logs[] = [ 'post_id' => $post_id, 'message' => sprintf( /* translators: %s: error message */ __( 'Failed to delete post ID %s.', 'onedesign' ), $post_id, ), - ); + ]; } else { - $delete_logs[] = array( + $delete_logs[] = [ 'post_id' => $post_id, 'message' => sprintf( /* translators: %s: post ID */ __( 'Deleted post ID %s.', 'onedesign' ), $post_id, ), - ); + ]; } } } - update_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, array(), false ); - update_option( Constants::ONEDESIGN_SHARED_PATTERNS, array(), false ); - update_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, array(), false ); - update_option( Constants::ONEDESIGN_SHARED_SYNCED_PATTERNS, array(), false ); - + update_option( Constants::ONEDESIGN_BRAND_SITE_POST_IDS, [], false ); + update_option( Constants::ONEDESIGN_SHARED_PATTERNS, [], false ); + update_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, [], false ); + update_option( Constants::ONEDESIGN_SHARED_SYNCED_PATTERNS, [], false ); } else { - $updated_templates = array_filter( $existing_templates, fn( $t ) => ! in_array( $t['id'], $template_ids, true ) ); + $updated_templates = array_filter( $existing_templates, static fn( $t ) => ! in_array( $t['id'], $template_ids, true ) ); } update_option( Constants::ONEDESIGN_SHARED_TEMPLATES, array_values( $updated_templates ), false ); return new \WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Templates removed successfully.', 'onedesign' ), 'templates' => array_values( $updated_templates ), 'delete_logs' => $delete_logs, - ), + ], 200 ); } @@ -446,25 +447,25 @@ public function remove_template( \WP_REST_Request $request ): \WP_REST_Response if ( empty( $template_ids ) || empty( $site ) ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Template IDs and site parameters are required.', 'onedesign' ), - ), + ], 400 ); } - $error_log = array(); - $response_data = array(); + $error_log = []; + $response_data = []; // get site info from child sites option. $site_info = Utils::get_site_by_id( $site ); if ( ! $site_info ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Site not found.', 'onedesign' ), - ), + ], 404 ); } @@ -474,19 +475,19 @@ public function remove_template( \WP_REST_Request $request ): \WP_REST_Response $response = wp_safe_remote_request( $request_url, - array( - 'headers' => array( + [ + 'headers' => [ 'X-OneDesign-Token' => $api_key, 'Content-Type' => 'application/json', - ), + ], 'method' => 'DELETE', 'body' => wp_json_encode( - array( + [ 'template_ids' => $template_ids, 'is_remove_all' => $is_remove_all, - ) + ] ), - ) + ] ); $handle_response = $this->handle_remote_response( $response ); @@ -497,14 +498,14 @@ public function remove_template( \WP_REST_Request $request ): \WP_REST_Response } return new \WP_REST_Response( - array( + [ 'success' => count( $error_log ) === 0, 'message' => __( 'Templates removed successfully from the site.', 'onedesign' ), 'template_ids' => $template_ids, 'site' => $site, 'response' => $response_data, 'errors' => $error_log, - ), + ], 200 ); } @@ -523,70 +524,76 @@ public function create_templates( \WP_REST_Request $request ): \WP_REST_Response if ( empty( $templates ) || ! is_array( $templates ) ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Templates parameter is required and should be an array.', 'onedesign' ), - ), + ], 400 ); } - $existing_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, array() ); + $existing_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, [] ); if ( ! is_array( $existing_templates ) ) { - $existing_templates = array(); + $existing_templates = []; } // Merge new templates with existing ones, avoiding duplicates based on 'id'. foreach ( $templates as $template ) { - if ( isset( $template['id'] ) && ! array_filter( $existing_templates, fn( $t ) => $t['id'] === $template['id'] ) ) { - $existing_templates[] = $template; + if ( ! isset( $template['id'] ) || array_filter( $existing_templates, static fn( $t ) => $t['id'] === $template['id'] ) ) { + continue; } + + $existing_templates[] = $template; } update_option( Constants::ONEDESIGN_SHARED_TEMPLATES, $existing_templates, false ); // get existing patterns. - $existing_patterns = get_option( Constants::ONEDESIGN_SHARED_PATTERNS, array() ); + $existing_patterns = get_option( Constants::ONEDESIGN_SHARED_PATTERNS, [] ); if ( ! is_array( $existing_patterns ) ) { - $existing_patterns = array(); + $existing_patterns = []; } // Merge new patterns with existing ones, avoiding duplicates based on 'id'. if ( is_array( $patterns ) ) { foreach ( $patterns as $pattern ) { - if ( isset( $pattern['name'] ) && ! array_filter( $existing_patterns, fn( $t ) => $t['name'] === $pattern['name'] ) ) { - $existing_patterns[] = $pattern; + if ( ! isset( $pattern['name'] ) || array_filter( $existing_patterns, static fn( $t ) => $t['name'] === $pattern['name'] ) ) { + continue; } + + $existing_patterns[] = $pattern; } } update_option( Constants::ONEDESIGN_SHARED_PATTERNS, $existing_patterns, false ); // get existing template parts. - $existing_template_parts = get_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, array() ); + $existing_template_parts = get_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, [] ); if ( ! is_array( $existing_template_parts ) ) { - $existing_template_parts = array(); + $existing_template_parts = []; } // Merge new template parts with existing ones, avoiding duplicates based on 'id'. if ( is_array( $template_parts ) ) { foreach ( $template_parts as $template_part ) { - if ( isset( $template_part['id'] ) && ! array_filter( $existing_template_parts, fn( $t ) => $t['id'] === $template_part['id'] ) ) { - $existing_template_parts[] = $template_part; + if ( ! isset( $template_part['id'] ) || array_filter( $existing_template_parts, static fn( $t ) => $t['id'] === $template_part['id'] ) ) { + continue; } + + $existing_template_parts[] = $template_part; } } update_option( Constants::ONEDESIGN_SHARED_TEMPLATE_PARTS, $existing_template_parts, false ); return new \WP_REST_Response( - array( + [ 'success' => true, 'message' => __( 'Templates saved successfully.', 'onedesign' ), 'templates' => $existing_templates, 'patterns' => $existing_patterns, 'template_parts' => $existing_template_parts, - ), + ], 200 ); } @@ -604,111 +611,115 @@ public function apply_templates_to_sites( \WP_REST_Request $request ): \WP_REST_ if ( empty( $sites ) || empty( $templates ) ) { return new \WP_REST_Response( - array( + [ 'success' => false, 'message' => __( 'Sites and templates parameters are required.', 'onedesign' ), - ), + ], 400 ); } $all_templates = get_block_templates(); - $shared_templates = array(); + $shared_templates = []; foreach ( $all_templates as $template ) { $template = (array) $template; - if ( in_array( $template['id'], $templates, true ) ) { - $shared_templates[] = $template; + if ( ! in_array( $template['id'], $templates, true ) ) { + continue; } + + $shared_templates[] = $template; } // process templates to have all info about its template parts/patterns. - $parsed_templates = array(); - $already_tracked = array(); + $parsed_templates = []; + $already_tracked = []; foreach ( $shared_templates as $template ) { $parsed_templates = array_merge( $parsed_templates, onedesign_parse_block_template( $template['content'], $already_tracked ) ); } - $template_parts = array_filter( $parsed_templates, fn( $t ) => 'template-part' === $t['type'] ); - $patterns = array_filter( $parsed_templates, fn( $t ) => 'pattern' === $t['type'] ); - $synced_patterns = array_filter( $parsed_templates, fn( $t ) => 'block' === $t['type'] ); + $template_parts = array_filter( $parsed_templates, static fn( $t ) => 'template-part' === $t['type'] ); + $patterns = array_filter( $parsed_templates, static fn( $t ) => 'pattern' === $t['type'] ); + $synced_patterns = array_filter( $parsed_templates, static fn( $t ) => 'block' === $t['type'] ); // get site info from child sites option. $brand_sites = Utils::get_sites_info(); - $error_log = array(); - $response_data = array(); + $error_log = []; + $response_data = []; foreach ( $brand_sites as $site ) { $site_url = esc_url_raw( trailingslashit( $site['url'] ) ); $site_api_key = $site['api_key']; $site_id = $site['id']; - if ( in_array( $site_id, $sites, true ) ) { - $request_url = Utils::build_api_endpoint( $site['url'], 'shared', self::NAMESPACE ); - $new_templates = Utils::modify_template_template_part_pattern_slug( $shared_templates, $site['name'] ); - $new_patterns = Utils::modify_template_template_part_pattern_slug( $patterns, $site['name'] ); - $new_template_parts = Utils::modify_template_template_part_pattern_slug( $template_parts, $site['name'] ); - $new_synced_patterns = Utils::modify_template_template_part_pattern_slug( $synced_patterns, $site['name'] ); - - // first make a request to create synced patterns to brand site. - $synced_patterns_request_url = Utils::build_api_endpoint( $site['url'], 'create-synced-patterns', self::NAMESPACE ); - $synced_patterns_response = wp_safe_remote_post( - $synced_patterns_request_url, - array( - 'headers' => array( - 'X-OneDesign-Token' => $site_api_key, - 'Content-Type' => 'application/json', - ), - 'body' => wp_json_encode( - array( - 'synced_patterns' => $new_synced_patterns, - ) - ), - ) - ); + if ( ! in_array( $site_id, $sites, true ) ) { + continue; + } - $handled_response = $this->handle_remote_response( $synced_patterns_response ); - if ( ! $handled_response['success'] ) { - $error_log[ $site_url ] = $handled_response['error']; - continue; - } - $synced_patterns_response = $handled_response['data']; + $request_url = Utils::build_api_endpoint( $site['url'], 'shared', self::NAMESPACE ); + $new_templates = Utils::modify_template_template_part_pattern_slug( $shared_templates, $site['name'] ); + $new_patterns = Utils::modify_template_template_part_pattern_slug( $patterns, $site['name'] ); + $new_template_parts = Utils::modify_template_template_part_pattern_slug( $template_parts, $site['name'] ); + $new_synced_patterns = Utils::modify_template_template_part_pattern_slug( $synced_patterns, $site['name'] ); + + // first make a request to create synced patterns to brand site. + $synced_patterns_request_url = Utils::build_api_endpoint( $site['url'], 'create-synced-patterns', self::NAMESPACE ); + $synced_patterns_response = wp_safe_remote_post( + $synced_patterns_request_url, + [ + 'headers' => [ + 'X-OneDesign-Token' => $site_api_key, + 'Content-Type' => 'application/json', + ], + 'body' => wp_json_encode( + [ + 'synced_patterns' => $new_synced_patterns, + ] + ), + ] + ); - // replace current site synced pattern ref with created post id. - if ( isset( $synced_patterns_response['created_posts'] ) && is_array( $synced_patterns_response['created_posts'] ) ) { - $created_posts = $synced_patterns_response['created_posts']; + $handled_response = $this->handle_remote_response( $synced_patterns_response ); + if ( ! $handled_response['success'] ) { + $error_log[ $site_url ] = $handled_response['error']; + continue; + } + $synced_patterns_response = $handled_response['data']; - $new_templates = Utils::replace_block_refs( $new_templates, $created_posts ); - $new_template_parts = Utils::replace_block_refs( $new_template_parts, $created_posts ); - $new_patterns = Utils::replace_block_refs( $new_patterns, $created_posts ); - } - $response = wp_safe_remote_post( - $request_url, - array( - 'headers' => array( - 'X-OneDesign-Token' => $site_api_key, - 'Content-Type' => 'application/json', - ), - 'body' => wp_json_encode( - array( - 'templates' => $new_templates, - 'patterns' => $new_patterns, - 'template_parts' => $new_template_parts, - ) - ), - ) - ); + // replace current site synced pattern ref with created post id. + if ( isset( $synced_patterns_response['created_posts'] ) && is_array( $synced_patterns_response['created_posts'] ) ) { + $created_posts = $synced_patterns_response['created_posts']; - $handled_response = $this->handle_remote_response( $response ); - if ( ! $handled_response['success'] ) { - $error_log[ $site_url ] = $handled_response['error']; - } else { - $response_data[ $site_url ] = $handled_response['data']; - } + $new_templates = Utils::replace_block_refs( $new_templates, $created_posts ); + $new_template_parts = Utils::replace_block_refs( $new_template_parts, $created_posts ); + $new_patterns = Utils::replace_block_refs( $new_patterns, $created_posts ); + } + $response = wp_safe_remote_post( + $request_url, + [ + 'headers' => [ + 'X-OneDesign-Token' => $site_api_key, + 'Content-Type' => 'application/json', + ], + 'body' => wp_json_encode( + [ + 'templates' => $new_templates, + 'patterns' => $new_patterns, + 'template_parts' => $new_template_parts, + ] + ), + ] + ); + + $handled_response = $this->handle_remote_response( $response ); + if ( ! $handled_response['success'] ) { + $error_log[ $site_url ] = $handled_response['error']; + } else { + $response_data[ $site_url ] = $handled_response['data']; } } return new \WP_REST_Response( - array( + [ 'success' => count( $error_log ) === 0, 'message' => __( 'Templates applied successfully to the selected sites.', 'onedesign' ), 'sites' => $sites, @@ -719,7 +730,7 @@ public function apply_templates_to_sites( \WP_REST_Request $request ): \WP_REST_ 'template_parts' => $new_template_parts, 'patterns' => $new_patterns, 'synced_patterns' => $synced_patterns, - ), + ], 200 ); } @@ -732,10 +743,10 @@ public function apply_templates_to_sites( \WP_REST_Request $request ): \WP_REST_ public function get_all_templates(): \WP_REST_Response { $templates = get_block_templates(); return new \WP_REST_Response( - array( + [ 'success' => true, 'templates' => $templates, - ), + ], 200 ); } @@ -747,20 +758,20 @@ public function get_all_templates(): \WP_REST_Response { */ public function get_templates_from_connected_sites(): \WP_REST_Response { $connected_sites = Utils::get_sites_info(); - $sites_response = array(); - $error_log = array(); + $sites_response = []; + $error_log = []; foreach ( $connected_sites as $site ) { $request_url = Utils::build_api_endpoint( $site['url'], 'shared', self::NAMESPACE ) . '?timestamp=' . time(); // Add timestamp to avoid caching issues. $api_key = $site['api_key']; $response = wp_safe_remote_get( $request_url, - array( - 'headers' => array( + [ + 'headers' => [ 'X-OneDesign-Token' => $api_key, 'Content-Type' => 'application/json', - ), + ], 'timeout' => 15, - ) + ] ); $handled_response = $this->handle_remote_response( $response ); if ( $handled_response['success'] ) { @@ -778,11 +789,11 @@ public function get_templates_from_connected_sites(): \WP_REST_Response { } } return new \WP_REST_Response( - array( + [ 'success' => true, 'templates' => $sites_response, 'errors' => $error_log, - ), + ], 200 ); } @@ -793,12 +804,12 @@ public function get_templates_from_connected_sites(): \WP_REST_Response { * @return \WP_REST_Response */ public function get_shared_templates(): \WP_REST_Response { - $shared_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, array() ); + $shared_templates = get_option( Constants::ONEDESIGN_SHARED_TEMPLATES, [] ); return new \WP_REST_Response( - array( + [ 'success' => true, 'templates' => $shared_templates, - ), + ], 200 ); } @@ -812,41 +823,41 @@ public function get_shared_templates(): \WP_REST_Response { */ private function handle_remote_response( array|\WP_Error $response ): array { if ( is_wp_error( $response ) ) { - return array( + return [ 'success' => false, 'error' => $response->get_error_message(), - ); + ]; } $response_code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $response_code ) { - return array( + return [ 'success' => false, 'error' => sprintf( /* translators: %s: response code */ __( 'Unexpected response code: %s', 'onedesign' ), $response_code ), - ); + ]; } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( json_last_error() !== JSON_ERROR_NONE ) { - return array( + return [ 'success' => false, 'error' => sprintf( /* translators: %s: error message */ __( 'JSON decode error: %s', 'onedesign' ), json_last_error_msg() ), - ); + ]; } - return array( + return [ 'success' => true, 'data' => $data, - ); + ]; } } diff --git a/inc/helpers/custom-functions.php b/inc/helpers/custom-functions.php index ae5b203..906402e 100644 --- a/inc/helpers/custom-functions.php +++ b/inc/helpers/custom-functions.php @@ -5,8 +5,8 @@ * @package OneDesign */ -use OneDesign\Utils; use OneDesign\Plugin_Configs\Constants; +use OneDesign\Utils; /** * Parse the block template content to extract blocks, template parts, and patterns. @@ -21,7 +21,7 @@ * @return array An array of parsed elements, each containing type, attributes, and content. */ function onedesign_parse_block_template( string $content, array &$already_tracked ): array { - $results = array(); + $results = []; // to process template parts and patterns. $pattern = '//'; @@ -34,11 +34,11 @@ function onedesign_parse_block_template( string $content, array &$already_tracke // Decode JSON attributes. $attributes = json_decode( $attributes_json, true ); - $result = array( + $result = [ 'type' => $block_type, 'full_match' => $match[0], - 'attributes' => $attributes ? $attributes : array(), - ); + 'attributes' => $attributes ? $attributes : [], + ]; // Create unique tracking key based on content identity. $tracking_key = ''; @@ -80,29 +80,33 @@ function onedesign_parse_block_template( string $content, array &$already_tracke } // Check if this specific content has already been processed. - if ( ! in_array( $tracking_key, $already_tracked, true ) ) { - // Add to tracking to prevent processing again. - $already_tracked[] = $tracking_key; + if ( in_array( $tracking_key, $already_tracked, true ) ) { + continue; + } - // Add to results only if not already processed. - $results[] = $result; + // Add to tracking to prevent processing again. + $already_tracked[] = $tracking_key; - // Recursively parse nested blocks and merge them at the same level. - if ( ! empty( $result['content'] ) ) { - $nested_blocks = array(); + // Add to results only if not already processed. + $results[] = $result; - if ( isset( $result['content']->content ) ) { - $nested_blocks = onedesign_parse_block_template( $result['content']->content, $already_tracked ); - } + // Recursively parse nested blocks and merge them at the same level. + if ( empty( $result['content'] ) ) { + continue; + } - if ( isset( $result['content']->post_content ) ) { - $nested_blocks = onedesign_parse_block_template( $result['content']->post_content, $already_tracked ); - } + $nested_blocks = []; - // Flatten the nested results into the main array. - $results = array_merge( $results, $nested_blocks ); - } + if ( isset( $result['content']->content ) ) { + $nested_blocks = onedesign_parse_block_template( $result['content']->content, $already_tracked ); } + + if ( isset( $result['content']->post_content ) ) { + $nested_blocks = onedesign_parse_block_template( $result['content']->post_content, $already_tracked ); + } + + // Flatten the nested results into the main array. + $results = array_merge( $results, $nested_blocks ); } } diff --git a/inc/traits/trait-singleton.php b/inc/traits/trait-singleton.php index 3669893..528e889 100644 --- a/inc/traits/trait-singleton.php +++ b/inc/traits/trait-singleton.php @@ -39,7 +39,7 @@ final public static function get_instance() { * * @var array */ - static $instance = array(); + static $instance = []; /** * If this trait is implemented in a class which has multiple @@ -49,17 +49,15 @@ final public static function get_instance() { * a key=>value pair for each `classname => instance` in self::$_instance * for each sub-class. */ - $called_class = get_called_class(); + $called_class = static::class; if ( ! isset( $instance[ $called_class ] ) ) { - $instance[ $called_class ] = new $called_class(); /** * Dependent items can use the onedesign_features_singleton_init_{$called_class} hook to execute code */ do_action( sprintf( 'onedesign_features_singleton_init_%s', $called_class ) ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - } return $instance[ $called_class ]; diff --git a/uninstall.php b/uninstall.php index f75c846..93715a1 100644 --- a/uninstall.php +++ b/uninstall.php @@ -22,7 +22,7 @@ */ function onedesign_delete_options_post_data(): void { // get brand site post ids & delete posts. - $brand_site_post_ids = get_option( 'onedesign_brand_site_post_ids', array() ); + $brand_site_post_ids = get_option( 'onedesign_brand_site_post_ids', [] ); if ( is_array( $brand_site_post_ids ) && ! empty( $brand_site_post_ids ) ) { foreach ( $brand_site_post_ids as $post_id ) { @@ -38,7 +38,7 @@ function onedesign_delete_options_post_data(): void { } } - $options_to_delete = array( + $options_to_delete = [ 'onedesign_site_type', 'onedesign_brand_site_patterns', 'onedesign_child_site_public_key', @@ -53,7 +53,7 @@ function onedesign_delete_options_post_data(): void { 'onedesign_shared_template_parts', 'onedesign_shared_synced_patterns', 'onedesign_multisite_governing_site', - ); + ]; foreach ( $options_to_delete as $option ) { delete_option( $option ); @@ -73,26 +73,28 @@ function onedesign_plugin_deletion(): void { onedesign_delete_options_post_data(); // if it's multisite, delete site options as well. - if ( is_multisite() ) { - $site_options_to_delete = array( - 'onedesign_multisite_governing_site', - ); - - foreach ( $site_options_to_delete as $site_option ) { - delete_site_option( $site_option ); - } + if ( ! is_multisite() ) { + return; + } - // for each site delete options. - $all_sites = get_sites( array( 'fields' => 'ids' ) ); - foreach ( $all_sites as $site_id ) { - if ( ! switch_to_blog( (int) $site_id ) ) { - continue; - } + $site_options_to_delete = [ + 'onedesign_multisite_governing_site', + ]; - onedesign_delete_options_post_data(); + foreach ( $site_options_to_delete as $site_option ) { + delete_site_option( $site_option ); + } - restore_current_blog(); + // for each site delete options. + $all_sites = get_sites( [ 'fields' => 'ids' ] ); + foreach ( $all_sites as $site_id ) { + if ( ! switch_to_blog( (int) $site_id ) ) { + continue; } + + onedesign_delete_options_post_data(); + + restore_current_blog(); } } }