Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@
"psr-4": {
"OneDesign\\": "inc/"
},
"classmap": [
"inc/"
],
"files": [
"./inc/helpers/custom-functions.php"
"custom-functions.php"
]
},
"require": {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion inc/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package OneDesign
*/

declare(strict_types = 1);
declare( strict_types = 1 );

namespace OneDesign;

Expand Down
22 changes: 5 additions & 17 deletions inc/classes/class-cpt-restriction.php → inc/CPT_Restriction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,25 @@

namespace OneDesign;

use OneDesign\Traits\Singleton;
use OneDesign\Post_Types\{ Pattern, Template };
use OneDesign\Contracts\Interfaces\Registrable;
use OneDesign\Modules\Post_Types\{ Pattern, Template };

Check failure on line 11 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

Group use declaration is disallowed, use single use for every import.

/**
* Class CPT_Restriction
*/
class CPT_Restriction {

/**
* Use Singleton trait.
*/
use Singleton;
class CPT_Restriction implements Registrable {

/**
* Slug for the Design Sync menu.
*
* @var string
*/
const MENU_SLUG = 'onedesign-design-sync';

Check failure on line 23 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

Constant \OneDesign\CPT_Restriction::MENU_SLUG visibility missing.

/**
* Protected class constructor
*/
protected function __construct() {
$this->setup_hooks();
}

/**
* Function to setup hooks.
* {@inheritDoc}
*/
public function setup_hooks(): void {
public function register_hooks(): void {
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' ] );
Expand All @@ -45,7 +33,7 @@
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 );

Check failure on line 36 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Callback expects 1 parameter, $accepted_args is set to 2.
}

/**
Expand All @@ -71,7 +59,7 @@
*
* @return array Modified arguments.
*/
public function restrict_cpt( array $args, string $post_type ): array {

Check failure on line 62 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method OneDesign\CPT_Restriction::restrict_cpt() return type has no value type specified in iterable type array.

Check failure on line 62 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method OneDesign\CPT_Restriction::restrict_cpt() has parameter $args with no value type specified in iterable type array.
if ( ! in_array( $post_type, [ Pattern::SLUG, Template::SLUG ], true ) ) {
return $args;
}
Expand Down Expand Up @@ -147,7 +135,7 @@
}

// Get the existing post.
$existing_post = get_posts(

Check warning on line 138 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

get_posts() is uncached unless the "suppress_filters" parameter is set to false. If the suppress_filter parameter is set to false this can be safely ignored. More Info: https://docs.wpvip.com/technical-references/caching/uncached-functions/.
[
'post_type' => Pattern::SLUG,
'post_status' => [ 'publish', 'draft', 'pending', 'private' ],
Expand Down Expand Up @@ -190,7 +178,7 @@
}

// Get the existing post.
$existing_post = get_posts(

Check warning on line 181 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

get_posts() is uncached unless the "suppress_filters" parameter is set to false. If the suppress_filter parameter is set to false this can be safely ignored. More Info: https://docs.wpvip.com/technical-references/caching/uncached-functions/.
[
'post_type' => Template::SLUG,
'post_status' => [ 'publish', 'draft', 'pending', 'private' ],
Expand All @@ -214,7 +202,7 @@
*
* @return array Modified arguments.
*/
public function modify_pattern_library_labels( array $args, string $post_type ): array {

Check failure on line 205 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method OneDesign\CPT_Restriction::modify_pattern_library_labels() return type has no value type specified in iterable type array.

Check failure on line 205 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method OneDesign\CPT_Restriction::modify_pattern_library_labels() has parameter $args with no value type specified in iterable type array.
// Only modify if it's our post type.
if ( Pattern::SLUG !== $post_type ) {
return $args;
Expand Down Expand Up @@ -272,7 +260,7 @@
}

// Get the existing post.
$existing_post = get_posts(

Check warning on line 263 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

get_posts() is uncached unless the "suppress_filters" parameter is set to false. If the suppress_filter parameter is set to false this can be safely ignored. More Info: https://docs.wpvip.com/technical-references/caching/uncached-functions/.
[
'post_type' => Pattern::SLUG,
'post_status' => [ 'publish', 'draft', 'pending', 'private' ],
Expand All @@ -299,7 +287,7 @@
* @return string Modified content.
*/
public function add_default_content_to_editor( string $content, object $post ): string {
if ( Pattern::SLUG === $post->post_type && empty( $content ) ) {

Check failure on line 290 in inc/CPT_Restriction.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Access to an undefined property object::$post_type.
$content = '<!-- wp:heading {"level":2} -->
<h2>Click on the "Patterns Selection" to push patterns to brand site.</h2>
<!-- /wp:heading -->';
Expand Down
25 changes: 25 additions & 0 deletions inc/Contracts/Interfaces/Registrable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Interface for Registrable classes.
*
* Registrable classes are those that register hooks (actions/filters) with WordPress.
*
* @package OneDesign\Contracts\Interfaces
*/

declare( strict_types = 1 );

namespace OneDesign\Contracts\Interfaces;

/**
* Interface - Registrable
*/
interface Registrable {

/**
* Registers class methods to WordPress.
*
* WordPress actions/filters should be included here.
*/
public function register_hooks(): void;
}
72 changes: 72 additions & 0 deletions inc/Contracts/Traits/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Singleton trait.
*
* @package OneDesign\Contracts\Traits
*/

declare( strict_types = 1 );

namespace OneDesign\Contracts\Traits;

/**
* Singleton trait.
*/
trait Singleton {
/**
* Instance of the class.
*
* @var ?static
*/
protected static $instance;

/**
* Prevent the class from being instantiated directly.
*/
protected function __construct() {
// To be implemented by the class using the trait.
}

/**
* Get the instance of the class.
*
* @return static
*/
final public static function instance() {
if ( ! isset( static::$instance ) ) {
static::$instance = new static();
}

return static::$instance;
}

/**
* Prevent the class from being cloned.
*/
final public function __clone() {
_doing_it_wrong(
__FUNCTION__,
sprintf(
// translators: %s: Class name.
esc_html__( 'The %s class should not be cloned.', 'onedesign' ),
esc_html( static::class ),
),
'0.0.1'
);
}

/**
* Prevent the class from being deserialized.
*/
final public function __wakeup() {
_doing_it_wrong(
__FUNCTION__,
sprintf(
// translators: %s: Class name.
esc_html__( 'De-serializing instances of %s is not allowed.', 'onedesign' ),
esc_html( static::class ),
),
'0.0.1'
);
}
}
26 changes: 5 additions & 21 deletions inc/classes/class-hooks.php → inc/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,19 @@

namespace OneDesign;

use OneDesign\Contracts\Interfaces\Registrable;
use OneDesign\Plugin_Configs\Constants;
use OneDesign\Traits\Singleton;
use OneDesign\Post_Types\{ Pattern, Template };
use OneDesign\Modules\Post_Types\{ Pattern, Template };

/**
* Class Hooks
*/
class Hooks {
class Hooks implements Registrable {

/**
* Use Singleton trait.
* {@inheritDoc}
*/
use Singleton;

/**
* Protected class constructor
*
* @return void
*/
protected function __construct() {
$this->setup_hooks();
}

/**
* Function to setup hooks.
*
* @return void
*/
public function setup_hooks(): void {
public function register_hooks(): void {
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' ] );
Expand Down Expand Up @@ -66,7 +50,7 @@
*
* @return array Modified plugin action links.
*/
public function add_setup_page_link( $links ): array {

Check failure on line 53 in inc/Hooks.php

View workflow job for this annotation

GitHub Actions / Run PHP static analysis

Method OneDesign\Hooks::add_setup_page_link() has parameter $links with no value type specified in iterable type array.
$setup_link = sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( 'admin.php?page=onedesign-settings' ) ),
Expand Down Expand Up @@ -224,7 +208,7 @@
$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(

Check warning on line 211 in inc/Hooks.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

get_posts() is uncached unless the "suppress_filters" parameter is set to false. If the suppress_filter parameter is set to false this can be safely ignored. More Info: https://docs.wpvip.com/technical-references/caching/uncached-functions/.
[
'post_type' => 'wp_template_part',
'name' => sanitize_text_field( $template_part['slug'] ),
Expand Down
116 changes: 116 additions & 0 deletions inc/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* The main plugin file.
*
* @package OneDesign
*/

declare( strict_types = 1 );

namespace OneDesign;

use OneDesign\Contracts\Traits\Singleton;

/**
* Class - Main
*/
final class Main {
use Singleton;

/**
* Registrable classes are entrypoints that "hook" into WordPress.
* They should implement the Registrable interface.
*
* @var class-string<\OneDesign\Contracts\Interfaces\Registrable>[]
*/
private const REGISTRABLE_CLASSES = [
Modules\Core\Assets::class,
Modules\Settings\Admin::class,
Modules\Multisite\Multisite::class,
Modules\Rest\Rest::class,
Modules\Rest\Basic_Options_Controller::class,
Modules\Rest\Patterns_Controller::class,
Modules\Rest\Templates_Controller::class,
Modules\Rest\Multisite_Controller::class,
Modules\Post_Types\Pattern::class,
Modules\Post_Types\Template::class,
Modules\Post_Types\Meta::class,
Hooks::class,
CPT_Restriction::class,
Plugin_Configs\Secret_Key::class,
];

/**
* {@inheritDoc}
*/
public static function instance(): self {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
self::$instance->setup();
}

return self::$instance;
}

/**
* Setup the plugin.
*/
private function setup(): void {
// Ensure pretty permalinks are enabled.
if ( ! $this->has_pretty_permalinks() ) {
return;
}

// Load the plugin classes.
$this->load();

// Do other stuff here like dep-checking, telemetry, etc.
}

/**
* Returns whether pretty permalinks are enabled.
*
* Will also render an admin notice if not enabled.
*/
private function has_pretty_permalinks(): bool {
if ( ! empty( get_option( 'permalink_structure' ) ) ) {
return true;
}

foreach ( [
'admin_notices',
'network_admin_notices',
] as $hook ) {
add_action(
$hook,
static function () {
wp_admin_notice(
sprintf(
/* translators: 1: Plugin name */
__( 'OneDesign: The plugin requires pretty permalinks to be enabled. Please go to <a href="%s">Permalink Settings</a> and enable an option other than <code>Plain</code>.', 'onedesign' ),
admin_url( 'options-permalink.php' ),
),
[
'type' => 'error',
'dismissible' => false,
]
);
}
);
}

return false;
}

/**
* Load the plugin classes.
*/
private function load(): void {
foreach ( self::REGISTRABLE_CLASSES as $class_name ) {
$instance = new $class_name();
$instance->register_hooks();
}

// Do other generalizable stuff here.
}
}
23 changes: 9 additions & 14 deletions inc/classes/class-assets.php → inc/Modules/Core/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@
* @package OneDesign
*/

namespace OneDesign;
namespace OneDesign\Modules\Core;

use OneDesign\Contracts\Interfaces\Registrable;
use OneDesign\Plugin_Configs\Constants;
use OneDesign\Post_Types\{ Pattern, Template };
use OneDesign\Traits\Singleton;
use OneDesign\Modules\Post_Types\{ Pattern, Template };
use OneDesign\Utils;

/**
* Class Assets
*/
class Assets {

/**
* Use Singleton trait.
*/
use Singleton;
class Assets implements Registrable {

/**
* Localized data for scripts.
Expand All @@ -29,17 +25,16 @@
private static array $localized_data = [];

/**
* Protected class constructor
* Class constructor.
*/
protected function __construct() {
$this->setup_hooks();
public function __construct() {
self::build_localized_data();
}

/**
* Setup WordPress hooks
* {@inheritDoc}
*/
public function setup_hooks(): void {
public function register_hooks(): void {
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_scripts' ], 20, 1 );
add_action( 'admin_enqueue_scripts', [ $this, 'add_admin_scripts' ], 20, 1 );
}
Expand Down Expand Up @@ -212,7 +207,7 @@
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

Check warning on line 210 in inc/Modules/Core/Assets.php

View workflow job for this annotation

GitHub Actions / Run PHPCS coding standards checks

File inclusion using variable (`$asset_meta_file`). Probably needs manual inspection.
: [
'dependencies' => [],
'version' => $this->get_file_version( $file, $ver ),
Expand Down
Loading
Loading