render_tinymce_popup(); break; case 'generateshortcode': $this->generate_shortcode(); break; case 'subscribe': $this->subscribe(); break; default: break; } if ( isset( $_REQUEST['cm_ajax_response'] ) && $_REQUEST['cm_ajax_response'] == 'ajax' ) { die(); } } /** * Render the tinymce popup * */ function render_tinymce_popup() { if ( ! current_user_can( 'edit_posts' ) ) return; include_once('tinymce_popup.php'); } /** * Generate a shortcode linked to the chosen API settings * */ function generate_shortcode() { if ( ! current_user_can( 'edit_posts' ) ) return; if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'cm_ajax_generate_shortcode' ) ) return; $current_shortcodes = get_option( 'cm_ajax_shortcodes' ); $matched_shortcode = FALSE; if ( is_array( $current_shortcodes ) && count( $current_shortcodes ) ) { // Try and find it in the existing shortcodes foreach ( $current_shortcodes as $shortcode_id => $shortcode ) { if ( $shortcode['account_api_key'] == $_POST['cm_ajax_tinymce_subscriber_account_api_key'] && $shortcode['list_api_key'] == $_POST['cm_ajax_tinymce_subscriber_list_api_key'] && $shortcode['show_name_field'] == $_POST['cm_ajax_tinymce_subscriber_show_name_field'] ) { // Support for adding list names to the array if the shortcode was created // with plugin version < 0.5 if ( ! isset ( $shortcode['list_name'] ) || $shortcode['list_name'] == 'Unknown List' || empty($shortcode['list_name']) ) { $current_shortcodes[$shortcode_id]['list_name'] = $this->get_list_name( $shortcode['account_api_key'], $shortcode['list_api_key'] ); update_option( 'cm_ajax_shortcodes', $current_shortcodes ); } // Found an existing shortcode - return it echo $shortcode_id; return; } } } // Create a new shortcode id $current_shortcodes[] = array( 'account_api_key' => $_POST['cm_ajax_tinymce_subscriber_account_api_key'], 'list_api_key' => $_POST['cm_ajax_tinymce_subscriber_list_api_key'], 'list_name' => $this->get_list_name( $_POST['cm_ajax_tinymce_subscriber_account_api_key'], $_POST['cm_ajax_tinymce_subscriber_list_api_key'] ) ); $matched_shortcode = count( $current_shortcodes ) - 1 ; if ( isset ( $_POST['cm_ajax_tinymce_subscriber_show_name_field']) ) { $current_shortcodes[$matched_shortcode]['show_name_field'] = $_POST['cm_ajax_tinymce_subscriber_show_name_field'] ; } update_option( 'cm_ajax_shortcodes', $current_shortcodes ); echo $matched_shortcode; return; } /** * Parse the shortcode, and render the form * */ function cm_ajax_subscribe( $atts, $content=null, $code="" ) { $args = shortcode_atts( array( 'id' => '' ), $atts ); $shortcode_id = $args['id']; $shortcode_options = get_option( 'cm_ajax_shortcodes' ); if ( ! isset ( $shortcode_options[$shortcode_id] ) ) return ' '; $settings = $shortcode_options[$shortcode_id]; ob_start(); if ( isset( $this->result ) ) { if ( $this->result ) { $success_style = ''; $failed_style = 'style="display: none;"'; $submit_style = 'style="display: none;"'; } else { $success_style = 'style="display: none;"'; $failed_style = ''; $submit_style = ''; } } else { $success_style = 'style="display: none;"'; $failed_style = 'style="display: none;"'; $submit_style = ''; } // Main signup form ?>
$_POST['cm-ajax-email'], 'Resubscribe' => true, ); if ( $settings['show_name_field'] ) { $record['Name'] = $_POST['cm-ajax-name']; } $result = $cm->add( $record ); if ( isset( $_POST['cm_ajax_response'] ) && $_POST['cm_ajax_response'] == 'ajax' ) { if ( $result->was_successful() ) { echo 'SUCCESS'; } else { echo 'FAILED'; echo ( $result->response->Code ) . ': '; echo ( $result->response->Message ); } } else { $this->result = $result->was_successful(); } return; } function get_list_name( $account_api_key, $list_api_key ) { $cm = new CS_REST_Lists( $list_api_key, $account_api_key ); $result = $cm->get(); if ( $result->was_successful() ) { return $result->response->Title; } else { return __( 'Unknown List', 'cm_ajax' ); } } } $CM_ajax_shortcode = new CM_ajax_shortcode();