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, unit, password, 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.
'ons or value, default will be yes|no
'options' => array(
'x' => 'Home X',
'y' => 'Home Y',
'z' => 'Home Z',
'new' => array(
'label' => 'New',
'description' => '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 {
use \StorePress\AdminUtils\Singleton;
public function plugin_file(): string {
return plugin_a()->get_plugin_file();
}
public function license_key(): string {
return plugin_a()->get_option( 'license' );
}
public function product_id(): int {
return 100;
}
/**
* Translatable Strings.
*
* @abstract
*
* @return array{
* 'license_key_empty_message': string,
* 'check_update_link_text': string,
* 'rollback_action_running': string,
* 'rollback_action_button': string,
* 'rollback_cancel_button': string,
* 'rollback_current_version': string,
* 'rollback_last_updated': string,
* 'rollback_view_changelog': string,
* 'rollback_page_title': string,
* 'rollback_page_title': string,
* 'rollback_link_text': string,
* 'rollback_failed': string,
* 'rollback_success': string,
* 'rollback_plugin_not_available': string,
* 'rollback_no_access': string,
* }
*/
public function localize_strings(): array {
return array(
'license_key_empty_message' => 'License key is not available.',
'check_update_link_text' => 'Check Update',
'rollback_action_running' => 'Rolling back',
'rollback_action_button' => 'Rollback',
'rollback_cancel_button' => 'Cancel',
'rollback_current_version' => 'Current version',
'rollback_last_updated' => 'Last updated %s ago.',
'rollback_view_changelog' => 'View Changelog',
'rollback_page_title' => 'Rollback Plugin',
'rollback_link_text' => 'Rollback',
'rollback_failed' => 'Rollback failed.',
'rollback_success' => 'Rollback success: %s rolled back to version %s.',
'rollback_plugin_not_available' => 'Plugin is not available.',
'rollback_no_access' => 'Sorry, you are not allowed to rollback plugins for this site.',
);
}
// 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';
}
// If you need to send additional arguments to update server.
// Check get_request_args() method.
public function additional_request_args(): array {
return array(
'domain'=> $this->get_client_hostname();
);
}
}
namespace StorePress\A;
defined( 'ABSPATH' ) || die( 'Keep Silent' );
class InactiveFeedback extends \StorePress\AdminUtils\Deactivation_Feedback {
use \StorePress\AdminUtils\Singleton;
// Where to send feedback data.
public function api_url(): string {
return 'https://state.example.com/wp-json/feedback/v1/deactivate';
}
public function reasons(): array {
$current_user = wp_get_current_user();
return array(
'temporary_deactivation' => array(
'title' => esc_html__( 'It\'s a temporary deactivation.', 'text-domain' ),
),
'dont_know_about' => array(
'title' => esc_html__( 'I couldn\'t understand how to make it work.', 'text-domain' ),
'message' => __( 'Its Plugin A.', 'text-domain' ),
),
'no_longer_needed' => array(
'title' => esc_html__( 'I no longer need the plugin.', 'text-domain' ),
),
'found_a_better_plugin' => array(
'title' => esc_html__( 'I found a better plugin.', 'text-domain' ),
'input' => array(
'placeholder'=>esc_html__( 'Please let us know which one', 'text-domain' ),
),
),
'broke_site_layout' => array(
'title' => __( 'The plugin <strong>broke my layout</strong> or some functionality.', 'text-domain' ),
'message' => __( '<a target="_blank" href="#">Please open a support ticket</a>, we will fix it immediately.', 'text-domain' ),
),
'plugin_setup_help' => array(
'title' => __( 'I need someone to <strong>setup this plugin.</strong>', 'text-domain' ),
'input' => array(
'placeholder'=>esc_html__( 'Your email address.', 'woo-variation-swatches' ),
'value'=>sanitize_email( $current_user->user_email )
),
'message' => __( 'Please provide your email address to contact with you <br />and help you to set up and configure this plugin.', 'text-domain' ),
),
'plugin_config_too_complicated' => array(
'title' => __( 'The plugin is <strong>too complicated to configure.</strong>', 'text-domain' ),
'message' => __( '<a target="_blank" href="#">Have you checked our documentation?</a>.', 'text-domain' ),
),
'need_specific_feature' => array(
'title' => esc_html__( 'I need specific feature that you don\'t support.', 'text-domain' ),
'input' => array(
'placeholder'=>esc_html__( 'Please share with us.', 'text-domain' ),
),
),
'other' => array(
'title' => esc_html__( 'Other', 'text-domain' ),
'input' => array(
'placeholder'=>esc_html__( 'Please share the reason', 'text-domain' ),
),
)
);
}
public function options(): array {
return plugin_a()->get_settings()->get_options();
}
public function title(): string {
return 'QUICK FEEDBACK';
}
public function sub_title(): string {
return 'May we have a little info about why you are deactivating?';
}
public function plugin_file(): string {
return plugin_a()->get_plugin_file();
}
/**
* Get Dialog Button.
*
* @return array<int, mixed>
* array(
* array(
* 'type' => 'button',
* 'label' => __( 'Send feedback & Deactivate' ),
* 'attributes' => array(
* 'disabled' => true,
* 'type' => 'submit',
* 'data-action' => 'submit',
* 'data-label' => __( 'Send feedback & Deactivate' ),
* 'data-processing' => __( 'Deactivate...' ),
* 'class' => array( 'button', 'button-primary' ),
* ),
* 'spinner' => true,
* ),
*
* array(
* 'type' => 'link',
* 'label' => __( 'Skip & Deactivate' ),
* 'attributes' => array(
* 'href' => '#',
* 'class' => array( 'skip-deactivate' ),
* ),
* ),
* )
*/
public function get_buttons(): array {
return array(
array(
'type' => 'button',
'label' => __( 'Send feedback & Deactivate' ),
'attributes' => array(
'disabled' => true,
'type' => 'submit',
'data-action' => 'submit',
'data-label' => __( 'Send feedback & Deactivate' ),
'data-processing' => __( 'Deactivate...' ),
'class' => array( 'button', 'button-primary' ),
),
'spinner' => true,
),
array(
'type' => 'link',
'label' => __( 'Skip & Deactivate' ),
'attributes' => array(
'href' => '#',
'class' => array( 'skip-deactivate' ),
),
),
);
}
/**
* Dialog width. - Optional.
*
* @return string
*/
public function get_dialog_width(): string {
return ''; // 600px
}
}