1. Go to this page and download the library: Download storepress/admin-utils library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace Plugin_A;
class AdminPage extends \StorePress\AdminUtils\Settings {
public function localize_strings(): array {
return array(
'unsaved_warning_text' => 'The changes you made will be lost if you navigate away from this page.',
'reset_warning_text' => 'Are you sure to reset?',
'reset_button_text' => 'Reset All',
'settings_link_text' => 'Settings',
'settings_updated_message_text' => 'Settings Saved',
'settings_deleted_message_text' => 'Settings Reset',
'settings_error_message_text' => 'Settings Not saved.',
);
}
public function parent_menu_title(): string {
return 'StorePress';
}
public function page_title(): string {
return 'Plugin A Page Title';
}
/*
// Parent menu id.
public function parent_menu() {
return 'edit.php?post_type=wporg_product';
}
*/
public function menu_title(): string {
return 'Plugin A Menu';
}
// Settings page slug.
public function page_id(): string {
return 'plugin-a';
}
// Option name to save data.
public function settings_id(): string {
return 'plugin_a_option';
}
public function plugin_file(): string {
return __FILE__;
}
public function get_default_sidebar() {
echo 'Default sidebar';
}
// To Disable rest api.
// URL will be: `/wp-json/<page_id>/<rest_api_version>/settings`
public function show_in_rest(): ?string {
return false;
}
// NOTE: You have to create and proper access to get REST API response.
// Create: "Application Passwords" from "WP Admin -> Users -> Profile" to use.
// Will return: /wp-json/my-custom-uri/settings
public function show_in_rest(): ?string {
return 'my-custom-uri';
}
// Settings and Rest API Display Capability. Default is: manage_options
public function capability(): string {
return 'edit_posts';
}
// Change rest api version. Default is: v1
public function rest_api_version(): string {
return 'v2';
}
// Adding custom scripts.
public function enqueue_scripts() {
parent::enqueue_scripts();
if ( $this->has_field_type( 'wc-enhanced-select' ) ) {
wp_enqueue_style( 'woocommerce_admin_styles' );
wp_enqueue_script( 'wc-enhanced-select' );
}
}
// Adding Custom TASK:
public function get_custom_action_uri(): string {
return wp_nonce_url( $this->get_settings_uri( array( 'action' => 'custom-action' ) ), $this->get_nonce_action() );
}
// Task: 02
public function process_actions($current_action){
parent::process_actions($current_action);
if ( 'custom-action' === $current_action ) {
$this->process_action_custom();
}
}
// Task: 03
public function process_action_custom(){
check_admin_referer( $this->get_nonce_action() );
// Process your task.
wp_safe_redirect( $this->get_action_uri( array( 'message' => 'custom-action-done' ) ) );
exit;
}
// Task: 04
public function settings_messages(){
parent::settings_messages();
$message = $this->get_message_query_arg_value();
if ( 'custom-action-done' === $message ) {
$this->add_settings_message( 'Custom action done successfully.' );
}
if ( 'custom-action-fail' === $message ) {
$this->add_settings_message( 'Custom action failed.', 'error' );
}
}
}
array(
'id' => 'input3', // Field ID.
'type' => 'text', // text, toggle, code, small-text, tiny-text, large-text, textarea, email, url, number, color, select, wc-enhanced-select, radio, checkbox
'title' => 'Input Label',
// Optional.
'full_width' => true, // To make field full width.
'description' => 'Input field description',
'default' => 'Hello World', // default value can be string or array
'default' => array('x','y'), // default value can be string or array
'placeholder' => '' // Placeholder
'suffix' => '' // Field suffix.
'html_attributes' => array('min' => 10) // Custom html attributes.
'html_datalist' => array('value 1', 'value 2') // HTML Datalist for suggestion.
'ription' => 'New Item',
),
)
),
namespace Plugin_A;
class Settings extends AdminSettings {
/**
* @return self
*/
public static function instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
}
namespace Plugin_A;
class Upgrade_Notice extends \StorePress\AdminUtils\Upgrade_Notice {
/**
* @return self
*/
public static function instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
public function plugin_file(): string {
return plugin_a()->get_pro_plugin_file();
}
public function compatible_version(): string {
return '3.0.0'; // passed from parent plugin
}
public function localize_notice_format(): string {
return __( 'You are using an incompatible version of <strong>%1$s - (%2$s)</strong>. Please upgrade to version <strong>%3$s</strong> or upper.', 'plugin-x');
}
// Optional
public function show_admin_notice(): bool {
return true;
}
// Optional
public function show_plugin_row_notice(): bool {
return true;
}
}
/**
* Plugin Name: Plugin A
* Tested up to: 6.4.1
* Update URI: https://update.example.com/
*/
namespace Plugin_A;
class Updater extends \StorePress\AdminUtils\Updater {
/**
* @return self
*/
public static function instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
public function plugin_file(): string {
return plugin_a()->get_plugin_file();
}
public function license_key(): string {
return plugin_a()->get_option( 'license' );
}
public function license_key_empty_message(): string {
return 'add license empty message';
}
public function check_update_link_text(): string {
return 'check now...';
}
public function product_id(): string {
return 000;
}
// Without hostname. Host name will prepend from Update URI
public function update_server_path(): string {
return '/updater-api/wp-json/plugin-updater/v1/check-update';
}
public function plugin_icons(): array {
return [ '2x' => '', '1x' => '', 'svg' => '', ];
}
public function plugin_banners(): array {
return [ '2x' => '', '1x' => '' ];
}
// If you need to send additional arguments to update server.
public function additional_request_args(): array {
return array(
'domain'=> wp_parse_url( sanitize_url( site_url() ), PHP_URL_HOST );
);
}
}