Skip to content

Commit

Permalink
Files updated
Browse files Browse the repository at this point in the history
* Updated Settings_API
* Renamed Better_Search to Better_Search_Core_Query
  • Loading branch information
ajaydsouza committed Sep 12, 2024
1 parent e6f85fe commit dff641e
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 203 deletions.
54 changes: 16 additions & 38 deletions includes/admin/settings/class-metabox-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/**
* Class to display and save a Metabox.
*
* @link https://webberzone.com
* @since 2.0.0
* @since 3.3.0
*
* @package WebberZone\Better_Search
*/
Expand All @@ -18,16 +17,17 @@
/**
* ATA Metabox class to register the metabox for ata_snippets post type.
*
* @since 2.0.0
* @since 3.5.0
*/
#[\AllowDynamicProperties]
class Metabox_API {

/**
* Current version number
*
* @var string
*/
const VERSION = '2.2.0';
const VERSION = '2.3.0';

/**
* Settings Key.
Expand Down Expand Up @@ -77,10 +77,11 @@ class Metabox_API {
* @param array|string $args {
* Array or string of arguments. Default is blank array.
*
* @type string $settings_key Admin menu type. See add_custom_menu_page() for options.
* @type string $prefix Parent menu slug.
* @type string $post_type Admin menu slug.
* @type array $registered_settings Settings fields array.
* @type string $settings_key Settings key - is used to prepare the form fields. It is not the meta key.
* @type string $prefix Used to create the meta keys. The meta key format is _{$prefix}_{$setting_id}.
* @type string|array|\WP_Screen $post_type The post type(s) on which to show the box.
* @type array $registered_settings Settings fields array.
* @type string $checkbox_modified_text Text to show to indicate a checkbox has been modified from its default value.
* }
*/
public function __construct( $args ) {
Expand All @@ -99,9 +100,9 @@ public function __construct( $args ) {
$this->$name = $value;
}

add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( "save_post_{$this->post_type}", array( $this, 'save' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
}

/**
Expand Down Expand Up @@ -154,34 +155,11 @@ public static function enqueue_scripts_styles() {
)
);

wp_enqueue_script(
'wz-admin-js',
plugins_url( 'js/admin-scripts' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-codemirror-js',
plugins_url( 'js/apply-codemirror' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-taxonomy-suggest-js',
plugins_url( 'js/taxonomy-suggest' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-media-selector-js',
plugins_url( 'js/media-selector' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
// Enqueue WZ Admin JS.
wp_enqueue_script( 'wz-admin-js' );
wp_enqueue_script( 'wz-codemirror-js' );
wp_enqueue_script( 'wz-taxonomy-suggest-js' );
wp_enqueue_script( 'wz-media-selector-js' );
}

/**
Expand Down Expand Up @@ -363,7 +341,7 @@ public function sanitize_post_meta( $settings ) {
}
}
$settings[ $fields['ids_field'] ] = join( ',', $ids );
$settings[ $key ] = \WebberZone\Better_Search\Util\Helpers::str_putcsv( $names );
$settings[ $key ] = Settings_Sanitize::str_putcsv( $names );
} else {
$settings[ $fields['ids_field'] ] = '';
}
Expand Down
115 changes: 86 additions & 29 deletions includes/admin/settings/class-settings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Portions of this code have been inspired by Easy Digital Downloads, WordPress Settings Sandbox, WordPress Settings API class, etc.
*
* @link https://webberzone.com
* @since 1.7.0
* @since 3.3.0
*
* @package WebberZone\Better_Search
*/
Expand All @@ -21,16 +21,17 @@
/**
* Settings API wrapper class
*
* @version 2.3.0
* @version 2.5.1
*/
#[\AllowDynamicProperties]
class Settings_API {

/**
* Current version number
*
* @var string
*/
const VERSION = '2.3.0';
const VERSION = '2.5.2';

/**
* Settings Key.
Expand Down Expand Up @@ -332,7 +333,7 @@ public function add_custom_menu_page( $menu ) {
'parent_slug' => 'options-general.php',
'page_title' => '',
'menu_title' => '',
'capability' => 'manage_options',
'capability' => $this->get_capability_for_menu(),
'menu_slug' => '',
'function' => array( $this, 'plugin_settings' ),

Expand Down Expand Up @@ -415,13 +416,81 @@ public function admin_menu() {
add_action( 'load-' . $this->settings_page, array( $this, 'settings_help' ) );
}

/**
* Get the appropriate capability for the menu based on the user's roles and settings.
*
* @param array $roles Array of roles to check.
* @param string $base_capability The default capability.
* @param \WP_User $current_user The current user object.
* @param array $role_capabilities Array of role capabilities.
* @return string The capability to use for the menu.
*/
public static function get_capability_for_menu( $roles = array(), $base_capability = 'manage_options', $current_user = null, $role_capabilities = array() ) {
if ( ! $current_user ) {
$current_user = wp_get_current_user();
}

if ( empty( $roles ) || in_array( 'administrator', $current_user->roles, true ) ) {
return $base_capability;
}

if ( empty( $role_capabilities ) ) {
$role_capabilities = array(
'editor' => 'edit_others_posts',
'author' => 'publish_posts',
'contributor' => 'edit_posts',
'subscriber' => 'read',
);
}

foreach ( $current_user->roles as $role ) {
if ( in_array( $role, $roles, true ) && isset( $role_capabilities[ $role ] ) ) {
return $role_capabilities[ $role ];
}
}

return $base_capability;
}

/**
* Enqueue scripts and styles.
*
* @param string $hook The current admin page.
*/
public function admin_enqueue_scripts( $hook ) {

$minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

// Settings API scripts.
wp_register_script(
'wz-admin-js',
plugins_url( 'js/settings-admin-scripts' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_register_script(
'wz-codemirror-js',
plugins_url( 'js/apply-codemirror' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_register_script(
'wz-taxonomy-suggest-js',
plugins_url( 'js/taxonomy-suggest' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_register_script(
'wz-media-selector-js',
plugins_url( 'js/media-selector' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);

if ( $hook === $this->settings_page ) {
self::enqueue_scripts_styles();
}
Expand All @@ -432,8 +501,6 @@ public function admin_enqueue_scripts( $hook ) {
*/
public static function enqueue_scripts_styles() {

$minimize = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

wp_enqueue_style( 'wp-color-picker' );

wp_enqueue_media();
Expand All @@ -452,27 +519,9 @@ public static function enqueue_scripts_styles() {
)
);

wp_enqueue_script(
'wz-admin-js',
plugins_url( 'js/admin-scripts' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-codemirror-js',
plugins_url( 'js/apply-codemirror' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script(
'wz-taxonomy-suggest-js',
plugins_url( 'js/taxonomy-suggest' . $minimize . '.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
wp_enqueue_script( 'wz-admin-js' );
wp_enqueue_script( 'wz-codemirror-js' );
wp_enqueue_script( 'wz-taxonomy-suggest-js' );
}

/**
Expand Down Expand Up @@ -527,6 +576,7 @@ public function admin_init() {
'field_class' => '',
'field_attributes' => '',
'placeholder' => '',
'pro' => false,
)
);

Expand All @@ -547,7 +597,14 @@ public function admin_init() {
}

// Register the settings into the options table.
register_setting( $settings_key, $settings_key, array( $this, 'settings_sanitize' ) );
register_setting(
$settings_key,
$settings_key,
array(
'sanitize_callback' => array( $this, 'settings_sanitize' ),
'show_in_rest' => true,
)
);
}

/**
Expand Down Expand Up @@ -787,7 +844,7 @@ public function plugin_settings() {
?>
<div class="wrap">
<h1><?php echo esc_html( $this->translation_strings['page_header'] ); ?></h1>
<?php do_action( $this->prefix . '_settings_page_header' ); ?>
<?php do_action( $this->prefix . '_settings_page_header' ); ?>

<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
Expand Down
Loading

0 comments on commit dff641e

Please sign in to comment.