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
2 changes: 1 addition & 1 deletion build/dlx-pw-patterns-view-rtl.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/dlx-pw-patterns-view.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-a11y', 'wp-api-fetch', 'wp-blob', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-escape-html', 'wp-i18n', 'wp-primitives', 'wp-private-apis', 'wp-url', 'wp-warning'), 'version' => '955bb876a115920b9df1');
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-a11y', 'wp-api-fetch', 'wp-blob', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-date', 'wp-element', 'wp-escape-html', 'wp-i18n', 'wp-primitives', 'wp-private-apis', 'wp-url', 'wp-warning'), 'version' => 'fde4307e99d955fe66e5');
2 changes: 1 addition & 1 deletion build/dlx-pw-patterns-view.css

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions build/dlx-pw-patterns-view.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dlx-pw-admin-css.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '027cba19b988a20e2814');
<?php return array('dependencies' => array(), 'version' => '76069a08eb155534d8a6');
2 changes: 1 addition & 1 deletion dist/dlx-pw-admin-css.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dlx-pw-admin.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-a11y', 'wp-components', 'wp-i18n'), 'version' => '18f74730cc0728b4408a');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-a11y', 'wp-components', 'wp-i18n'), 'version' => '24775400f5ea69837f49');
2 changes: 1 addition & 1 deletion dist/dlx-pw-admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dlx-pw-network-admin-settings.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-a11y', 'wp-components', 'wp-compose', 'wp-i18n', 'wp-keycodes', 'wp-url'), 'version' => '45c0910a7241255adb1d');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-a11y', 'wp-components', 'wp-compose', 'wp-i18n', 'wp-keycodes', 'wp-url'), 'version' => '14610467a9de86d99cc7');
2 changes: 1 addition & 1 deletion dist/dlx-pw-network-admin-settings.js

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions pattern-wrangler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pattern Wrangler
* Plugin URI: https://dlxplugins.com/plugins/pattern-wrangler/
* Description: Manage your block patterns.
* Version: 2.1.3
* Version: 2.2.0
* Requires at least: 6.8
* Requires PHP: 7.2
* Author: DLX Plugins
Expand All @@ -24,7 +24,7 @@

require_once __DIR__ . '/functions.php';

define( 'DLXPW_PATTERN_WRANGLER_VERSION', '2.1.3' );
define( 'DLXPW_PATTERN_WRANGLER_VERSION', '2.2.0' );
define( 'DLXPW_PATTERN_WRANGLER_FILE', __FILE__ );

// Support for site-level autoloading.
Expand Down Expand Up @@ -115,6 +115,11 @@ public function init() {
// Set global current screen to not is admin request. This should only apply to the PW preview screen Ajax request.
if ( ! isset( $GLOBALS['current_screen'] ) ) {
$GLOBALS['current_screen'] = new class() {
/**
* Check if in admin.
*
* @return bool
*/
public function in_admin() {
return false;
}
Expand All @@ -131,3 +136,13 @@ function () {
$pattern_wrangler->plugins_loaded();
}
);

/**
* Run action on activation.
*/
register_activation_hook(
__FILE__,
function () {
update_option( 'dlx_pw_activation_date', time() );
}
);
20 changes: 20 additions & 0 deletions php/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public function run() {
// For reseeting the network options.
add_action( 'wp_ajax_dlx_pw_reset_network_settings', array( $this, 'ajax_reset_network_options' ) );

// For dismissing the ratings nag.
add_action( 'wp_ajax_dlx_pw_dismiss_ratings_nag', array( $this, 'ajax_dismiss_ratings_nag' ) );

// For initializing settings links on the plugins screen.
add_action( 'admin_init', array( $this, 'init_settings_links' ) );

Expand Down Expand Up @@ -121,6 +124,20 @@ public function plugin_settings_link( $settings ) {
}
}

/**
* Dismiss the ratings nag.
*/
public function ajax_dismiss_ratings_nag() {
// Get nonce.
$nonce = sanitize_text_field( filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_SPECIAL_CHARS ) );
if ( ! wp_verify_nonce( $nonce, 'dlx-pw-admin-dismiss-ratings-nag' ) || ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array() );
}
update_user_meta( get_current_user_id(), 'dlx_pw_dismissed_rating', true );
// Dismiss the ratings nag.
wp_send_json_success( array() );
}

/**
* Save the options via Ajax.
*/
Expand Down Expand Up @@ -506,6 +523,8 @@ public function enqueue_admin_scripts() {
'isMultisite' => Functions::is_multisite(),
'networkAdminSettingsUrl' => Functions::get_network_settings_url(),
'isUserNetworkAdmin' => current_user_can( 'manage_network' ),
'canShowRatingsNag' => Functions::can_show_ratings_nag(),
'dismissRatingsNagNonce' => wp_create_nonce( 'dlx-pw-admin-dismiss-ratings-nag' ),
)
);
\wp_set_script_translations( 'dlx-pw-admin', 'pattern-wrangler' );
Expand Down Expand Up @@ -554,6 +573,7 @@ public function enqueue_admin_scripts_patterns() {
'networkAdminSettingsUrl' => Functions::get_network_settings_url(),
'isUserNetworkAdmin' => current_user_can( 'manage_network' ),
'getSiteBaseUrl' => esc_url( admin_url() ),
'doNotShowAgain' => get_user_meta( get_current_user_id(), 'dlx_pw_do_not_show_again', true ) ?? false,
)
);
\wp_set_script_translations( 'dlx-pw-patterns-view', 'pattern-wrangler' );
Expand Down
26 changes: 24 additions & 2 deletions php/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public static function get_pattern_categories() {
$options = Options::get_options();

// Get registered block categories.
$pattern_categories = \WP_Block_Pattern_Categories_Registry::get_instance();
$pattern_categories = $pattern_categories->get_all_registered();
$pattern_categories = Patterns::get_instance();
$pattern_categories = $pattern_categories->get_registered_categories();

// Get all registered block patterns. We'll use this for a count.
$pattern_registry = \WP_Block_Patterns_Registry::get_instance();
Expand Down Expand Up @@ -200,6 +200,28 @@ function ( $a, $b ) {
);
}

/**
* Check if the ratings nag can be shown.
*
* @return bool true if can be shown, false if not.
*/
public static function can_show_ratings_nag() {
$activation_date = get_option( 'dlx_pw_activation_date' );
if ( ! $activation_date ) {
update_option( 'dlx_pw_activation_date', time() );
}
// Check user meta if rating has been dismissed.
$dismissed_rating = (bool) get_user_meta( get_current_user_id(), 'dlx_pw_dismissed_rating', true );
if ( $dismissed_rating ) {
return false;
}
$days_since_activation = ( time() - $activation_date ) / DAY_IN_SECONDS;
if ( $days_since_activation >= 30 ) {
return true;
}
return false;
}


/**
* Check if a pattern ID is valid.
Expand Down
17 changes: 0 additions & 17 deletions php/Network_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ public function run() {

// Init the network admin menu.
add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ), 100 );

// Add site state to URL column.
// add_filter( 'display_site_states', array( $this, 'add_pattern_source_state' ), 10, 2 );

// Add sites list table column.
// add_filter( 'wpmu_blogs_columns', array( $this, 'add_patterns_column' ) );

// Add content to the custom column.
// add_action( 'manage_sites_custom_column', array( $this, 'manage_patterns_column' ), 10, 2 );

// add_action( 'network_site_info_form', array( $this, 'add_pattern_configuration_field' ) );

// For hooking into the save action of the site info screen.
// add_action( 'wp_update_site', array( $this, 'save_site_configuration' ) );

// Save pattern configuration.
// add_action( 'wpmu_update_blog_options', array( $this, 'save_pattern_configuration' ) );
}

/**
Expand Down
33 changes: 32 additions & 1 deletion php/Patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ class Patterns {
*/
private static $instance = null;

/**
* Holds the registered categories *before* filters. Can access after `init` action.
*
* @var WP_Block_Pattern_Categories_Registry $registered_categories_registry
*/
private static $registered_categories_registry = null;

/**
* Return an instance of the class
*
Expand Down Expand Up @@ -153,6 +160,20 @@ function ( $caps, $cap ) {
add_action( 'admin_notices', array( $this, 'add_admin_notices' ) );
}

/**
* Get registered categories *after* filters.
*
* @return WP_Block_Pattern_Categories_Registry
*/
public function get_registered_categories() {
if ( null === self::$registered_categories_registry ) {
$pattern_categories = \WP_Block_Pattern_Categories_Registry::get_instance();
$pattern_categories = $pattern_categories->get_all_registered();
self::$registered_categories_registry = $pattern_categories;
}
return self::$registered_categories_registry;
}

/**
* Add admin notices to the patterns post type list view.
*/
Expand Down Expand Up @@ -213,9 +234,19 @@ public function maybe_deregister_paused_registered_patterns() {

// Get disabled patterns.
$options = Options::get_disabled_patterns();

// Get the registered patterns.
$patterns = \WP_Block_Patterns_Registry::get_instance();
$all_patterns = $patterns->get_all_registered();
$registered_pattern_slugs = array();
foreach ( $all_patterns as $pattern ) {
$registered_pattern_slugs[] = $pattern['name'];
}

// Loop through all patterns and deregister any that are disabled.
foreach ( $options as $option ) {
$option = sanitize_text_field( $option );
if ( empty( $option ) ) {
if ( empty( $option ) || ! in_array( $option, $registered_pattern_slugs, true ) ) {
continue;
}

Expand Down
66 changes: 40 additions & 26 deletions php/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ public function rest_get_users_permissions_callback() {
*/
public function rest_api_register() {

// todo - for multisite site pattern library.
// register_rest_route(
// 'dlxplugins/pattern-wrangler/v1',
// '/search/sites',
// array(
// 'methods' => 'POST',
// 'permission_callback' => array( $this, 'rest_get_users_permissions_callback' ),
// 'callback' => array( $this, 'rest_get_sites' ),
// 'sanitize_callback' => array( $this, 'rest_api_sanitize' ),
// 'validate_callback' => array( $this, 'rest_api_validate' ),
// )
// );

/**
* For retrieving site patterns for a site.
*/
Expand Down Expand Up @@ -178,7 +165,8 @@ public function rest_api_register() {
*/
public function rest_delete_pattern( $request ) {

$items = $request->get_param( 'items' );
$items = $request->get_param( 'items' );
$do_not_show_again = filter_var( $request->get_param( 'doNotShowAgain' ), FILTER_VALIDATE_BOOLEAN );

foreach ( $items as $item ) {
$pattern_id = $item['id'];
Expand All @@ -196,6 +184,12 @@ public function rest_delete_pattern( $request ) {
wp_delete_post( $pattern_id, true );
}

if ( $do_not_show_again ) {
if ( current_user_can( 'publish_posts' ) ) {
update_user_meta( get_current_user_id(), 'dlx_pw_do_not_show_again', true );
}
}

return rest_ensure_response( array( 'success' => true ) );
}

Expand All @@ -208,7 +202,8 @@ public function rest_delete_pattern( $request ) {
*/
public function rest_pause_pattern( $request ) {

$items = $request->get_param( 'items' );
$items = $request->get_param( 'items' );
$do_not_show_again = filter_var( $request->get_param( 'doNotShowAgain' ), FILTER_VALIDATE_BOOLEAN );

foreach ( $items as $item ) {
$pattern_id = $item['id'];
Expand Down Expand Up @@ -238,6 +233,11 @@ public function rest_pause_pattern( $request ) {
Options::set_disabled_patterns( $disabled_patterns );
}
}
if ( $do_not_show_again ) {
if ( current_user_can( 'publish_posts' ) ) {
update_user_meta( get_current_user_id(), 'dlx_pw_do_not_show_again', true );
}
}

return rest_ensure_response( array( 'success' => true ) );
}
Expand All @@ -251,7 +251,8 @@ public function rest_pause_pattern( $request ) {
*/
public function rest_publish_pattern( $request ) {

$items = $request->get_param( 'items' );
$items = $request->get_param( 'items' );
$do_not_show_again = filter_var( $request->get_param( 'doNotShowAgain' ), FILTER_VALIDATE_BOOLEAN );

foreach ( $items as $item ) {
$pattern_id = $item['id'];
Expand Down Expand Up @@ -281,6 +282,11 @@ public function rest_publish_pattern( $request ) {
Options::set_disabled_patterns( array_values( $disabled_patterns ) );
}
}
if ( $do_not_show_again ) {
if ( current_user_can( 'publish_posts' ) ) {
update_user_meta( get_current_user_id(), 'dlx_pw_do_not_show_again', true );
}
}

return rest_ensure_response( array( 'success' => true ) );
}
Expand All @@ -299,10 +305,11 @@ public function rest_create_pattern( $request ) {
return rest_ensure_response( array( 'error' => 'Invalid nonce or user does not have permission to create patterns.' ) );
}

$pattern_title = sanitize_text_field( $request->get_param( 'patternTitle' ) );
$pattern_categories = Functions::sanitize_array_recursive( $request->get_param( 'patternCategories' ) ); // Cats are in format, [name, id].
$pattern_sync_status = sanitize_text_field( $request->get_param( 'patternSyncStatus' ) );
$pattern_copy_id = sanitize_text_field( $request->get_param( 'patternCopyId' ) ); // 0 if not copying.
$pattern_title = sanitize_text_field( $request->get_param( 'patternTitle' ) );
$pattern_categories = Functions::sanitize_array_recursive( $request->get_param( 'patternCategories' ) ); // Cats are in format, [name, id].
$pattern_sync_status = sanitize_text_field( $request->get_param( 'patternSyncStatus' ) );
$pattern_copy_id = sanitize_text_field( $request->get_param( 'patternCopyId' ) ); // 0 if not copying.
$disable_registered_pattern = filter_var( $request->get_param( 'disableRegisteredPattern' ), FILTER_VALIDATE_BOOLEAN );

// Get categories into right format.
$categories = array();
Expand Down Expand Up @@ -338,6 +345,14 @@ public function rest_create_pattern( $request ) {
}
}

// If disable registered pattern, disable the pattern.
if ( $disable_registered_pattern ) {
$disabled_patterns = Options::get_disabled_patterns();
$disabled_patterns[] = sanitize_text_field( $pattern_copy_id );
$disabled_patterns = array_unique( $disabled_patterns );
Options::set_disabled_patterns( $disabled_patterns );
}

// Add terms.
$terms_affected_ids = wp_set_post_terms( $pattern_id, $terms_to_add, 'wp_pattern_category' );

Expand Down Expand Up @@ -496,7 +511,7 @@ public function rest_get_all_patterns() {
'slug' => $registered_category['slug'],
'enabled' => true,
'count' => 0,
'mappedTo' => false,
'mappedTo' => $registered_category['mappedTo'] ?? false,
'registered' => true,
'id' => 0,
);
Expand Down Expand Up @@ -597,11 +612,10 @@ public function rest_get_all_patterns() {
$categories = array();
$category_slugs = array();
foreach ( $pattern['categories'] as $category ) {
$category_registry = \WP_Block_Pattern_Categories_Registry::get_instance();
$category = $category_registry->get_registered( $category );
if ( $category ) {
$categories[] = $category['label'];
$category_slugs[] = sanitize_title( $category['name'] );
if ( array_key_exists( $category, $all_categories ) ) {
$cat = $all_categories[ $category ];
$categories[] = $cat['label'];
$category_slugs[] = sanitize_title( $cat['slug'] );
}
}

Expand Down
Loading