messages = $messages; $this->mailchimp = $mailchimp; } /** * Add hooks */ public function add_hooks() { add_action( 'register_shortcode_ui', array( $this, 'register_shortcake_ui' ) ); add_action( 'mc4wp_save_form', array( $this, 'update_form_stylesheets' ) ); add_action( 'mc4wp_admin_edit_form', array( $this, 'process_save_form' ) ); add_action( 'mc4wp_admin_add_form', array( $this, 'process_add_form' ) ); add_filter( 'mc4wp_admin_menu_items', array( $this, 'add_menu_item' ), 5 ); add_action( 'mc4wp_admin_show_forms_page-edit-form', array( $this, 'show_edit_page' ) ); add_action( 'mc4wp_admin_show_forms_page-add-form', array( $this, 'show_add_page' ) ); add_action( 'mc4wp_admin_enqueue_assets', array( $this, 'enqueue_assets' ), 10, 2 ); } /** * @param string $suffix * @param string $page */ public function enqueue_assets( $suffix, $page = '' ) { if( $page !== 'forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit-form' ) { return; } wp_register_script( 'mc4wp-forms-admin', MC4WP_PLUGIN_URL . 'assets/js/forms-admin' . $suffix . '.js', array( 'mc4wp-admin' ), MC4WP_VERSION, true ); wp_enqueue_script( 'mc4wp-forms-admin'); wp_localize_script( 'mc4wp-forms-admin', 'mc4wp_forms_i18n', array( 'addToForm' => __( "Add to form", 'mailchimp-for-wp' ), 'agreeToTerms' => __( "I have read and agree to the terms & conditions", 'mailchimp-for-wp' ), 'agreeToTermsShort' => __( "Agree to terms", 'mailchimp-for-wp' ), 'agreeToTermsLink' => __( 'Link to your terms & conditions page', 'mailchimp-for-wp' ), 'city' => __( 'City', 'mailchimp-for-wp' ), 'checkboxes' => __( 'Checkboxes', 'mailchimp-for-wp' ), 'choices' => __( 'Choices', 'mailchimp-for-wp' ), 'choiceType' => __( "Choice type", 'mailchimp-for-wp' ), 'chooseField' => __( "Choose a field to add to the form", 'mailchimp-for-wp' ), 'close' => __( 'Close', 'mailchimp-for-wp' ), 'country' => __( 'Country', 'mailchimp-for-wp' ), 'dropdown' => __( 'Dropdown', 'mailchimp-for-wp' ), 'fieldType' => __( 'Field type', 'mailchimp-for-wp' ), 'fieldLabel' => __( "Field label", 'mailchimp-for-wp' ), 'formAction' => __( 'Form action', 'mailchimp-for-wp' ), 'formActionDescription' => __( 'This field will allow your visitors to choose whether they would like to subscribe or unsubscribe', 'mailchimp-for-wp' ), 'formFields' => __( 'Form fields', 'mailchimp-for-wp' ), 'forceRequired' => __( 'This field is marked as required in MailChimp.', 'mailchimp-for-wp' ), 'initialValue' => __( "Initial value", 'mailchimp-for-wp' ), 'interestCategories' => __( 'Interest categories', 'mailchimp-for-wp' ), 'isFieldRequired' => __( "Is this field required?", 'mailchimp-for-wp' ), 'listChoice' => __( 'List choice', 'mailchimp-for-wp' ), 'listChoiceDescription' => __( 'This field will allow your visitors to choose a list to subscribe to.', 'mailchimp-for-wp' ), 'listFields' => __( 'List fields', 'mailchimp-for-wp' ), 'min' => __( 'Min', 'mailchimp-for-wp' ), 'max' => __( 'Max', 'mailchimp-for-wp' ), 'noAvailableFields' => __( 'No available fields. Did you select a MailChimp list in the form settings?', 'mailchimp-for-wp' ), 'optional' => __( 'Optional', 'mailchimp-for-wp' ), 'placeholder' => __( 'Placeholder', 'mailchimp-for-wp' ), 'placeholderHelp' => __( "Text to show when field has no value.", 'mailchimp-for-wp' ), 'preselect' => __( 'Preselect', 'mailchimp-for-wp' ), 'remove' => __( 'Remove', 'mailchimp-for-wp' ), 'radioButtons' => __( 'Radio buttons', 'mailchimp-for-wp' ), 'streetAddress' => __( 'Street Address', 'mailchimp-for-wp' ), 'state' => __( 'State', 'mailchimp-for-wp' ), 'subscribe' => __( 'Subscribe', 'mailchimp-for-wp' ), 'submitButton' => __( 'Submit button', 'mailchimp-for-wp' ), 'wrapInParagraphTags' => __( "Wrap in paragraph tags?", 'mailchimp-for-wp' ), 'value' => __( "Value", 'mailchimp-for-wp' ), 'valueHelp' => __( "Text to prefill this field with.", 'mailchimp-for-wp' ), 'zip' => __( 'ZIP', 'mailchimp-for-wp' ), )); } /** * @param $items * * @return mixed */ public function add_menu_item( $items ) { $items['forms'] = array( 'title' => __( 'Forms', 'mailchimp-for-wp' ), 'text' => __( 'Form', 'mailchimp-for-wp' ), 'slug' => 'forms', 'callback' => array( $this, 'show_forms_page' ), 'load_callback' => array( $this, 'redirect_to_form_action' ), 'position' => 10 ); return $items; } /** * Act on the "add form" form */ public function process_add_form() { check_admin_referer( 'add_form', '_mc4wp_nonce' ); $form_data = $_POST['mc4wp_form']; $form_content = include MC4WP_PLUGIN_DIR . 'config/default-form-content.php'; // Fix for MultiSite stripping KSES for roles other than administrator remove_all_filters( 'content_save_pre' ); $form_id = wp_insert_post( array( 'post_type' => 'mc4wp-form', 'post_status' => 'publish', 'post_title' => $form_data['name'], 'post_content' => $form_content, ) ); // if settings were passed, save those too. if( isset( $form_data['settings'] ) ) { update_post_meta( $form_id, '_mc4wp_settings', $form_data['settings'] ); } // set default form ID $this->set_default_form_id( $form_id ); $this->messages->flash( __( "Success! Form successfully saved.", 'mailchimp-for-wp' ) ); wp_redirect( mc4wp_get_edit_form_url( $form_id ) ); exit; } /** * Saves a form to the database * * @param array $data * @return int */ public function save_form( $data ) { $keys = array( 'settings' => array(), 'messages' => array(), 'name' => '', 'content' => '' ); $data = array_merge( $keys, $data ); $data = $this->sanitize_form_data( $data ); $post_data = array( 'post_type' => 'mc4wp-form', 'post_status' => ! empty( $data['status'] ) ? $data['status'] : 'publish', 'post_title' => $data['name'], 'post_content' => $data['content'] ); // if an `ID` is given, make sure post is of type `mc4wp-form` if( ! empty( $data['ID'] ) ) { $post = get_post( $data['ID'] ); if( $post instanceof WP_Post && $post->post_type === 'mc4wp-form' ) { $post_data['ID'] = $data['ID']; // merge new settings with current settings to allow passing partial data $current_settings = get_post_meta( $post->ID, '_mc4wp_settings', true ); if( is_array( $current_settings ) ) { $data['settings'] = array_merge( $current_settings, $data['settings'] ); } } } // Fix for MultiSite stripping KSES for roles other than administrator remove_all_filters( 'content_save_pre' ); $form_id = wp_insert_post( $post_data ); update_post_meta( $form_id, '_mc4wp_settings', $data['settings'] ); // save form messages in individual meta keys foreach( $data['messages'] as $key => $message ) { update_post_meta( $form_id, 'text_' . $key, $message ); } /** * Runs right after a form is updated. * * @since 3.0 * * @param int $form_id */ do_action( 'mc4wp_save_form', $form_id ); return $form_id; } /** * @param array $data * @return array */ public function sanitize_form_data( $data ) { $raw_data = $data; // strip