File: //proc/self/cwd/wp-content/plugins/reset-thumbnails/includes/class-reset-thumbnails-settings.php
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class ResetThumbnails {
/**
* The single instance of Reset_Thumbnails_settings.
* @var object
* @access private
* @since 1.0.0
*/
private static $_instance = null;
/**
* The main plugin object.
* @var object
* @access public
* @since 1.0.0
*/
public $parent = null;
/**
* Prefix for plugin settings.
* @var string
* @access public
* @since 1.0.0
*/
public $base = '';
/**
* Available settings for plugin.
* @var array
* @access public
* @since 1.0.0
*/
public $settings = array();
public function __construct ( $parent ) {
$this->parent = $parent;
$this->base = 'kai_';
// Initialise settings
add_action( 'init', array( $this, 'init_settings' ), 11 );
// Register plugin settings
add_action( 'admin_init' , array( $this, 'register_settings' ) );
// Add settings page to menu
add_action( 'admin_menu' , array( $this, 'add_menu_item' ) );
// Add settings link to plugins page
add_filter( 'plugin_action_links_' . plugin_basename( $this->parent->file ) , array( $this, 'add_settings_link' ) );
}
/**
* Initialise settings
* @return void
*/
public function init_settings () {
$this->settings = $this->settings_fields();
}
/**
* Add settings page to admin menu
* @return void
*/
public function add_menu_item () {
$page = add_options_page( __( 'Reset Thumbnails', 'reset-thumbnails' ) , __( 'Reset Thumbnails', 'reset-thumbnails' ) , 'manage_options' , $this->parent->_token . '_settings' , array( $this, 'settings_page' ) );
add_action( 'admin_print_styles-' . $page, array( $this, 'settings_assets' ) );
}
/**
* Load settings JS & CSS
* @return void
*/
public function settings_assets () {
// We're including the farbtastic script & styles here because they're needed for the colour picker
// If you're not including a colour picker field then you can leave these calls out as well as the farbtastic dependency for the wpt-admin-js script below
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
// We're including the WP media scripts here because they're needed for the image upload field
// If you're not including an image upload then you can leave this function call out
wp_enqueue_media();
wp_register_script( $this->parent->_token . '-settings-js', $this->parent->assets_url . 'js/settings' . $this->parent->script_suffix . '.js', array( 'farbtastic', 'jquery' ), '1.0.0' );
wp_enqueue_script( $this->parent->_token . '-settings-js' );
wp_localize_script($this->parent->_token . '-settings-js', 'kai', array(
'url' => admin_url('admin-ajax.php')
));
}
/**
* Add settings link to plugin list table
* @param array $links Existing links
* @return array Modified links
*/
public function add_settings_link ( $links ) {
$settings_link = '<a href="options-general.php?page=' . $this->parent->_token . '_settings">' . __( 'Settings', 'reset-thumbnails' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
/**
* Build settings fields
* @return array Fields to be displayed on settings page
*/
private function settings_fields () {
$helper = new Reset_Thumbnails_Helper();
$post_type = get_post_types();
unset($post_type['nav_menu_item']);
unset($post_type['revision']);
unset($post_type['shop_webhook']);
unset($post_type['vc_grid_item']);
unset($post_type['shop_coupon']);
unset($post_type['shop_order_refund']);
unset($post_type['shop_order']);
unset($post_type['product_variation']);
unset($post_type['wpcf7_contact_form']);
$settings['standard'] = array(
'title' => __( 'Basic Settings', 'reset-thumbnails' ),
'description' => __( '*Note : Choose "attachment" is set default image to all media attachments', 'reset-thumbnails' ),
'fields' => array(
array(
'id' => 'choose_post_type',
'label' => __( 'Choose Post Type', 'reset-thumbnails' ),
'description' => __( '', 'reset-thumbnails' ),
'type' => 'select',
'options' => $post_type,
'default' => 'post'
),
array(
'id' => 'default_image',
'label' => __( 'Default Thumbnail Image' , 'reset-thumbnails' ),
'description' => __( 'This will upload an image to your media library and use it for set thumbnail.', 'reset-thumbnails' ),
'type' => 'image',
'default' => '',
'placeholder' => ''
),
array(
'id' => 'change_ID',
'label' =>__('Change ID','reset-thumbnails'),
'description' => __('Set all media attached to using attached ID.','reset-thumbnails'),
'type' => 'checkbox',
'default' => 'off'
)
)
);
$settings = apply_filters( $this->parent->_token . '_settings_fields', $settings );
return $settings;
}
/**
* Register plugin settings
* @return void
*/
public function register_settings () {
if ( is_array( $this->settings ) ) {
// Check posted/selected tab
$current_section = '';
if ( isset( $_POST['tab'] ) && $_POST['tab'] ) {
$current_section = $_POST['tab'];
} else {
if ( isset( $_GET['tab'] ) && $_GET['tab'] ) {
$current_section = $_GET['tab'];
}
}
foreach ( $this->settings as $section => $data ) {
if ( $current_section && $current_section != $section ) continue;
// Add section to page
add_settings_section( $section, $data['title'], array( $this, 'settings_section' ), $this->parent->_token . '_settings' );
foreach ( $data['fields'] as $field ) {
// Validation callback for field
$validation = '';
if ( isset( $field['callback'] ) ) {
$validation = $field['callback'];
}
// Register field
$option_name = $this->base . $field['id'];
register_setting( $this->parent->_token . '_settings', $option_name, $validation );
// Add field to page
add_settings_field( $field['id'], $field['label'], array( $this->parent->admin, 'display_field' ), $this->parent->_token . '_settings', $section, array( 'field' => $field, 'prefix' => $this->base ) );
}
if ( ! $current_section ) break;
}
}
}
public function settings_section ( $section ) {
$html = '<p> ' . $this->settings[ $section['id'] ]['description'] . '</p>' . "\n";
echo $html;
}
/**
* Load settings page content
* @return void
*/
public function settings_page () {
// Build page HTML
$html = '<div class="wrap" id="' . $this->parent->_token . '_settings">' . "\n";
$html .= '<h2>' . __( 'Reset Thumbnails Settings' , 'reset-thumbnails' ) . '</h2>' . "\n";
$html .= '<p>' . __( 'Reset Thumbnails allows you to set the default thumbnails for your all image attachments or aplly for your all post thumbnail. This is very handy if you want to reset your all images attachmented.' , 'reset-thumbnails' ) . '</p>' . "\n";
$html .= '<p>' . __( 'Visit <a href="http://wptrack.net" target="_blank">my home</a> to helped with this plugin.' , 'reset-thumbnails' ) . '</p>' . "\n";
$tab = '';
if ( isset( $_GET['tab'] ) && $_GET['tab'] ) {
$tab .= $_GET['tab'];
}
// Show page tabs
if ( is_array( $this->settings ) && 1 < count( $this->settings ) ) {
$html .= '<h2 class="nav-tab-wrapper">' . "\n";
$c = 0;
foreach ( $this->settings as $section => $data ) {
// Set tab class
$class = 'nav-tab';
if ( ! isset( $_GET['tab'] ) ) {
if ( 0 == $c ) {
$class .= ' nav-tab-active';
}
} else {
if ( isset( $_GET['tab'] ) && $section == $_GET['tab'] ) {
$class .= ' nav-tab-active';
}
}
// Set tab link
$tab_link = add_query_arg( array( 'tab' => $section ) );
if ( isset( $_GET['settings-updated'] ) ) {
$tab_link = remove_query_arg( 'settings-updated', $tab_link );
}
// Output tab
$html .= '<a href="' . $tab_link . '" class="' . esc_attr( $class ) . '">' . esc_html( $data['title'] ) . '</a>' . "\n";
++$c;
}
$html .= '</h2>' . "\n";
}
$html .= '<form method="post" action="options.php" enctype="multipart/form-data">' . "\n";
// Get settings fields
ob_start();
settings_fields( $this->parent->_token . '_settings' );
do_settings_sections( $this->parent->_token . '_settings' );
echo '<div class="ajax-loading"></div>';
echo '<button class="button js-reset-action">Reset Thumbnails</button>';
$html .= ob_get_clean();
$html .= '<p class="submit">' . "\n";
$html .= '<input type="hidden" name="tab" value="' . esc_attr( $tab ) . '" />' . "\n";
$html .= '<input name="Submit" type="submit" class="button-primary" value="' . esc_attr( __( 'Save Settings' , 'reset-thumbnails' ) ) . '" />' . "\n";
$html .= '</p>' . "\n";
$html .= '</form>' . "\n";
$html .= '</div>' . "\n";
echo $html;
}
/**
* Main Reset_Thumbnails_settings Instance
*
* Ensures only one instance of Reset_Thumbnails_settings is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see Reset_Thumbnails()
* @return Main Reset_Thumbnails_settings instance
*/
public static function instance ( $parent ) {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self( $parent );
}
return self::$_instance;
} // End instance()
/**
* Cloning is forbidden.
*
* @since 1.0.0
*/
public function __clone () {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), $this->parent->_version );
} // End __clone()
/**
* Unserializing instances of this class is forbidden.
*
* @since 1.0.0
*/
public function __wakeup () {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), $this->parent->_version );
} // End __wakeup()
}