'custom_action',
'label' => '',
'class' => 'button-secondary',
'wait_label' => '',
'success_label' => '',
'error_label' => '',
'success_callback' => '',
'error_callback' => '',
);
/**
* This is first called when an ajax button is clicked. This checks whether the nonce
* is valid and if we should continue;
*
* @return void
*/
public function ajaxSecurityChecker() {
if ( empty( $_POST['nonce'] ) ) {
wp_send_json_error( __( 'Security check failed, please refresh the page and try again.', TF_I18NDOMAIN ) );
}
if ( ! wp_verify_nonce( $_POST['nonce'], 'tf-ajax-button' ) ) {
wp_send_json_error( __( 'Security check failed, please refresh the page and try again.', TF_I18NDOMAIN ) );
}
}
/**
* This is last called when an ajax button is clicked. This just exist with a successful state,
* since doing nothing reads as an error with wp.ajax
*
* @return void
*/
public function ajaxLastSuccess() {
wp_send_json_success();
}
/**
* Constructor, fixes the settings to allow for multiple ajax buttons in a single option
*
* @param $settings Array Option settings
* @param $owner Object The container of the option
* @return void
*/
function __construct( $settings, $owner ) {
parent::__construct( $settings, $owner );
add_action( 'admin_head', array( __CLASS__, 'createAjaxScript' ) );
// Adjust the settings
foreach ( $this->defaultSecondarySettings as $key => $default ) {
if ( ! is_array( $this->settings[ $key ] ) ) {
$this->settings[ $key ] = array( $this->settings[ $key ] );
}
}
while ( count( $this->settings['label'] ) < count( $this->settings['action'] ) ) {
$this->settings['label'][] = $this->settings['label'][ count( $this->settings['label'] ) - 1 ];
}
while ( count( $this->settings['class'] ) < count( $this->settings['action'] ) ) {
$this->settings['class'][] = 'button-secondary';
}
while ( count( $this->settings['wait_label'] ) < count( $this->settings['action'] ) ) {
$this->settings['wait_label'][] = $this->settings['wait_label'][ count( $this->settings['wait_label'] ) - 1 ];
}
while ( count( $this->settings['error_label'] ) < count( $this->settings['action'] ) ) {
$this->settings['error_label'][] = $this->settings['error_label'][ count( $this->settings['error_label'] ) - 1 ];
}
while ( count( $this->settings['success_label'] ) < count( $this->settings['action'] ) ) {
$this->settings['success_label'][] = $this->settings['success_label'][ count( $this->settings['success_label'] ) - 1 ];
}
while ( count( $this->settings['success_callback'] ) < count( $this->settings['action'] ) ) {
$this->settings['success_callback'][] = '';
}
while ( count( $this->settings['error_callback'] ) < count( $this->settings['action'] ) ) {
$this->settings['error_callback'][] = __( 'Something went wrong', TF_I18NDOMAIN );
}
foreach ( $this->settings['label'] as $i => $label ) {
if ( empty( $label ) ) {
$this->settings['label'][ $i ] = __( 'Click me', TF_I18NDOMAIN );
}
}
foreach ( $this->settings['wait_label'] as $i => $label ) {
if ( empty( $label ) ) {
$this->settings['wait_label'][ $i ] = __( 'Please wait...', TF_I18NDOMAIN );
}
}
foreach ( $this->settings['error_label'] as $i => $label ) {
if ( empty( $label ) ) {
$this->settings['error_label'][ $i ] = $this->settings['label'][ $i ];
}
}
foreach ( $this->settings['success_label'] as $i => $label ) {
if ( empty( $label ) ) {
$this->settings['success_label'][ $i ] = $this->settings['label'][ $i ];
}
}
/**
* Create ajax handlers for security and last resort success returns
*/
foreach ( $this->settings['action'] as $i => $action ) {
if ( ! empty( $action ) ) {
add_action( 'wp_ajax_' . $action, array( $this, 'ajaxSecurityChecker' ), 1 );
add_action( 'wp_ajax_' . $action, array( $this, 'ajaxLastSuccess' ), 99999 );
}
}
}
/**
* Renders the option
*
* @return void
*/
public function display() {
$this->echoOptionHeader();
foreach ( $this->settings['action'] as $i => $action ) {
printf( '',
$this->settings['class'][ $i ],
esc_attr( $action ),
esc_attr( $this->settings['label'][ $i ] ),
esc_attr( $this->settings['wait_label'][ $i ] ),
esc_attr( $this->settings['error_label'][ $i ] ),
esc_attr( $this->settings['success_label'][ $i ] ),
esc_attr( wp_create_nonce( 'tf-ajax-button' ) ),
esc_attr( $this->settings['success_callback'][ $i ] ),
esc_attr( $this->settings['error_callback'][ $i ] ),
esc_attr( $this->settings['label'][ $i ] )
);
}
$this->echoOptionFooter();
}
/**
* Prints the Javascript needed by ajax buttons. The script is only echoed once
*
* @return void
*/
public static function createAjaxScript() {
if ( ! self::$firstLoad ) {
return;
}
self::$firstLoad = false;
?>
getID());
$wp_customize->add_control( new TitanFrameworkOptionAjaxButtonControl( $wp_customize, '', array(
'label' => $this->settings['name'],
'section' => $section->getID(),
'settings' => $this->getID(),
'description' => $this->settings['desc'],
'priority' => $priority,
'options' => $this->settings,
) ) );
}
}
/*
* WP_Customize_Control with description
*/
add_action( 'customize_register', 'registerTitanFrameworkOptionAjaxButtonControl', 1 );
function registerTitanFrameworkOptionAjaxButtonControl() {
class TitanFrameworkOptionAjaxButtonControl extends WP_Customize_Control {
public $description;
public $options;
public function render_content() {
TitanFrameworkOptionAjaxButton::createAjaxScript();
?>