PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailchimp-for-wp/includes/forms/class-admin.php

https://gitlab.com/bhargavi_dcw/dflocal
PHP | 444 lines | 242 code | 77 blank | 125 comment | 16 complexity | 5990ba4fc91fedaa48465a6d0c4c99cb MD5 | raw file
  1. <?php
  2. /**
  3. * Class MC4WP_Forms_Admin
  4. *
  5. * @ignore
  6. * @access private
  7. */
  8. class MC4WP_Forms_Admin {
  9. /**
  10. * @var MC4WP_Admin_Messages
  11. */
  12. protected $messages;
  13. /**
  14. * @var MC4WP_MailChimp
  15. */
  16. protected $mailchimp;
  17. /**
  18. * @param MC4WP_Admin_Messages $messages
  19. * @param MC4WP_MailChimp $mailchimp
  20. */
  21. public function __construct( MC4WP_Admin_Messages $messages, MC4WP_MailChimp $mailchimp ) {
  22. $this->messages = $messages;
  23. $this->mailchimp = $mailchimp;
  24. require dirname( __FILE__ ) . '/admin-functions.php';
  25. }
  26. /**
  27. * Add hooks
  28. */
  29. public function add_hooks() {
  30. add_action( 'register_shortcode_ui', array( $this, 'register_shortcake_ui' ) );
  31. add_action( 'mc4wp_save_form', array( $this, 'update_form_stylesheets' ) );
  32. add_action( 'mc4wp_admin_preview_form', array( $this, 'prepare_form_preview' ) );
  33. add_action( 'mc4wp_admin_edit_form', array( $this, 'process_save_form' ) );
  34. add_action( 'mc4wp_admin_add_form', array( $this, 'process_add_form' ) );
  35. add_filter( 'mc4wp_admin_menu_items', array( $this, 'add_menu_item' ), 5 );
  36. add_action( 'mc4wp_admin_show_forms_page-edit-form', array( $this, 'show_edit_page' ) );
  37. add_action( 'mc4wp_admin_show_forms_page-add-form', array( $this, 'show_add_page' ) );
  38. add_action( 'mc4wp_admin_enqueue_assets', array( $this, 'enqueue_assets' ), 10, 2 );
  39. }
  40. /**
  41. * @param string $suffix
  42. * @param string $page
  43. */
  44. public function enqueue_assets( $suffix, $page = '' ) {
  45. if( $page !== 'forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit-form' ) {
  46. return;
  47. }
  48. wp_register_script( 'mc4wp-forms-admin', MC4WP_PLUGIN_URL . 'assets/js/forms-admin' . $suffix . '.js', array( 'mc4wp-admin' ), MC4WP_VERSION, true );
  49. wp_enqueue_script( 'mc4wp-forms-admin');
  50. wp_localize_script( 'mc4wp-forms-admin', 'mc4wp_forms_i18n', array(
  51. 'addToForm' => __( "Add to form", 'mailchimp-for-wp' ),
  52. 'city' => __( 'City', 'mailchimp-for-wp' ),
  53. 'checkboxes' => __( 'Checkboxes', 'mailchimp-for-wp' ),
  54. 'choices' => __( 'Choices', 'mailchimp-for-wp' ),
  55. 'choiceType' => __( "Choice type", 'mailchimp-for-wp' ),
  56. 'chooseField' => __( "Choose a MailChimp field to add to the form", 'mailchimp-for-wp' ),
  57. 'close' => __( 'Close', 'mailchimp-for-wp' ),
  58. 'country' => __( 'Country', 'mailchimp-for-wp' ),
  59. 'dropdown' => __( 'Dropdown', 'mailchimp-for-wp' ),
  60. 'fieldLabel' => __( "Field label", 'mailchimp-for-wp' ),
  61. 'formAction' => __( 'Form action', 'mailchimp-for-wp' ),
  62. 'formActionDescription' => __( 'This field will allow your visitors to choose whether they would like to subscribe or unsubscribe', 'mailchimp-for-wp' ),
  63. 'forceRequired' => __( 'This field is marked as required in MailChimp.', 'mailchimp-for-wp' ),
  64. 'isFieldRequired' => __( "Is this field required?", 'mailchimp-for-wp' ),
  65. 'listChoice' => __( 'List choice', 'mailchimp-for-wp' ),
  66. 'listChoiceDescription' => __( 'This field will allow your visitors to choose a list to subscribe to.', 'mailchimp-for-wp' ),
  67. 'min' => __( 'Min', 'mailchimp-for-wp' ),
  68. 'max' => __( 'Max', 'mailchimp-for-wp' ),
  69. 'noAvailableFields' => __( 'No available fields. Did you select a MailChimp list in the form settings?', 'mailchimp-for-wp' ),
  70. 'optional' => __( 'Optional', 'mailchimp-for-wp' ),
  71. 'placeholder' => __( 'Placeholder', 'mailchimp-for-wp' ),
  72. 'placeholderHelp' => __( "Text to show when field has no value.", 'mailchimp-for-wp' ),
  73. 'preselect' => __( 'Preselect', 'mailchimp-for-wp' ),
  74. 'remove' => __( 'Remove', 'mailchimp-for-wp' ),
  75. 'radioButtons' => __( 'Radio buttons', 'mailchimp-for-wp' ),
  76. 'streetAddress' => __( 'Street Address', 'mailchimp-for-wp' ),
  77. 'state' => __( 'State', 'mailchimp-for-wp' ),
  78. 'subscribe' => __( 'Subscribe', 'mailchimp-for-wp' ),
  79. 'submitButton' => __( 'Submit button', 'mailchimp-for-wp' ),
  80. 'wrapInParagraphTags' => __( "Wrap in paragraph tags?", 'mailchimp-for-wp' ),
  81. 'value' => __( "Initial value", 'mailchimp-for-wp' ),
  82. 'valueHelp' => __( "Text to prefill this field with.", 'mailchimp-for-wp' ),
  83. 'zip' => __( 'ZIP', 'mailchimp-for-wp' ),
  84. ));
  85. }
  86. /**
  87. * @param $items
  88. *
  89. * @return mixed
  90. */
  91. public function add_menu_item( $items ) {
  92. $items['forms'] = array(
  93. 'title' => __( 'Forms', 'mailchimp-for-wp' ),
  94. 'text' => __( 'Forms', 'mailchimp-for-wp' ),
  95. 'slug' => 'forms',
  96. 'callback' => array( $this, 'show_forms_page' ),
  97. 'load_callback' => array( $this, 'redirect_to_form_action' ),
  98. 'position' => 10
  99. );
  100. return $items;
  101. }
  102. /**
  103. * Act on the "add form" form
  104. */
  105. public function process_add_form() {
  106. check_admin_referer( 'add_form', '_mc4wp_nonce' );
  107. $form_data = stripslashes_deep( $_POST['mc4wp_form'] );
  108. $form_content = include MC4WP_PLUGIN_DIR . 'config/default-form-content.php';
  109. // Fix for MultiSite stripping KSES for roles other than administrator
  110. remove_all_filters( 'content_save_pre' );
  111. $form_id = wp_insert_post(
  112. array(
  113. 'post_type' => 'mc4wp-form',
  114. 'post_status' => 'publish',
  115. 'post_title' => $form_data['name'],
  116. 'post_content' => $form_content,
  117. )
  118. );
  119. update_post_meta( $form_id, '_mc4wp_settings', $form_data['settings'] );
  120. $this->messages->flash( __( "<strong>Success!</strong> Form successfully saved.", 'mailchimp-for-wp' ) );
  121. wp_redirect( mc4wp_get_edit_form_url( $form_id ) );
  122. exit;
  123. }
  124. /**
  125. * Saves a form to the database
  126. *
  127. * @param array $data
  128. * @return int
  129. */
  130. public function save_form( $data ) {
  131. static $keys = array(
  132. 'settings' => array(),
  133. 'messages' => array(),
  134. 'name' => '',
  135. 'content' => ''
  136. );
  137. $data = array_merge( $keys, $data );
  138. $data = $this->sanitize_form_data( $data );
  139. $post_data = array(
  140. 'post_type' => 'mc4wp-form',
  141. 'post_status' => ! empty( $data['status'] ) ? $data['status'] : 'publish',
  142. 'post_title' => $data['name'],
  143. 'post_content' => $data['content']
  144. );
  145. // if an `ID` is given, make sure post is of type `mc4wp-form`
  146. if( ! empty( $data['ID'] ) ) {
  147. $post = get_post( $data['ID'] );
  148. if( $post instanceof WP_Post && $post->post_type === 'mc4wp-form' ) {
  149. $post_data['ID'] = $data['ID'];
  150. // merge new settings with current settings to allow passing partial data
  151. $current_settings = get_post_meta( $post->ID, '_mc4wp_settings', true );
  152. if( is_array( $current_settings ) ) {
  153. $data['settings'] = array_merge( $current_settings, $data['settings'] );
  154. }
  155. }
  156. }
  157. // Fix for MultiSite stripping KSES for roles other than administrator
  158. remove_all_filters( 'content_save_pre' );
  159. $form_id = wp_insert_post( $post_data );
  160. update_post_meta( $form_id, '_mc4wp_settings', $data['settings'] );
  161. // save form messages in individual meta keys
  162. foreach( $data['messages'] as $key => $message ) {
  163. update_post_meta( $form_id, 'text_' . $key, $message );
  164. }
  165. /**
  166. * Runs right after a form is updated.
  167. *
  168. * @since 3.0
  169. *
  170. * @param int $form_id
  171. */
  172. do_action( 'mc4wp_save_form', $form_id );
  173. return $form_id;
  174. }
  175. /**
  176. * @param array $data
  177. * @return array
  178. */
  179. public function sanitize_form_data( $data ) {
  180. $raw_data = $data;
  181. // strip <form> tags from content
  182. $data['content'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['content'] );
  183. // sanitize text fields
  184. $data['settings']['redirect'] = sanitize_text_field( $data['settings']['redirect'] );
  185. // strip tags from messages
  186. foreach( $data['messages'] as $key => $message ) {
  187. $data['messages'][$key] = strip_tags( $message, '<strong><b><br><a><script><u><em><i><span><img>' );
  188. }
  189. // make sure lists is an array
  190. if( ! isset( $data['settings']['lists'] ) ) {
  191. $data['settings']['lists'] = array();
  192. }
  193. $data['settings']['lists'] = array_filter( (array) $data['settings']['lists'] );
  194. /**
  195. * Filters the form data just before it is saved.
  196. *
  197. * @param array $data Sanitized array of form data.
  198. * @param array $raw_data Raw array of form data.
  199. *
  200. * @since 3.0.8
  201. */
  202. $data = (array) apply_filters( 'mc4wp_form_sanitized_data', $data, $raw_data );
  203. return $data;
  204. }
  205. /**
  206. * Saves a form
  207. */
  208. public function process_save_form( ) {
  209. check_admin_referer( 'edit_form', '_mc4wp_nonce' );
  210. $form_id = (int) $_POST['mc4wp_form_id'];
  211. $form_data = stripslashes_deep( $_POST['mc4wp_form'] );
  212. $form_data['ID'] = $form_id;
  213. $this->save_form( $form_data );
  214. // update default form id?
  215. $default_form_id = (int) get_option( 'mc4wp_default_form_id', 0 );
  216. if( empty( $default_form_id ) ) {
  217. update_option( 'mc4wp_default_form_id', $form_id );
  218. }
  219. $previewer = new MC4WP_Form_Previewer( $form_id );
  220. $this->messages->flash( __( "<strong>Success!</strong> Form successfully saved.", 'mailchimp-for-wp' ) . sprintf( ' <a href="%s">', $previewer->get_preview_url() ) . __( 'Preview form', 'mailchimp-for-wp' ) . '</a>' );
  221. }
  222. /**
  223. * Goes through each form and aggregates array of stylesheet slugs to load.
  224. *
  225. * @hooked `mc4wp_save_form`
  226. */
  227. public function update_form_stylesheets() {
  228. $stylesheets = array();
  229. $forms = mc4wp_get_forms();
  230. foreach( $forms as $form ) {
  231. $stylesheet = $form->get_stylesheet();
  232. if( ! empty( $stylesheet ) && ! in_array( $stylesheet, $stylesheets ) ) {
  233. $stylesheets[] = $stylesheet;
  234. }
  235. }
  236. update_option( 'mc4wp_form_stylesheets', $stylesheets );
  237. }
  238. /**
  239. * Prepares a Form Preview
  240. */
  241. public function prepare_form_preview() {
  242. $form_id = (int) $_POST['mc4wp_form_id'];
  243. $preview_id = (int) get_option( 'mc4wp_form_preview_id', 0 );
  244. // get data
  245. $form_data = stripslashes_deep( $_POST['mc4wp_form'] );
  246. $form_data['ID'] = $preview_id;
  247. $form_data['status'] = 'preview';
  248. $real_preview_id = $this->save_form( $form_data );
  249. if( $real_preview_id != $preview_id ) {
  250. update_option( 'mc4wp_form_preview_id', $real_preview_id, false );
  251. }
  252. // redirect to preview
  253. $previewer = new MC4WP_Form_Previewer( $form_id, $real_preview_id );
  254. wp_redirect( $previewer->get_preview_url() );
  255. exit;
  256. }
  257. /**
  258. * Redirect to correct form action
  259. *
  260. * @ignore
  261. */
  262. public function redirect_to_form_action() {
  263. if( ! empty( $_GET['view'] ) ) {
  264. return;
  265. }
  266. // query first available form and go there
  267. $forms = mc4wp_get_forms( array( 'numberposts' => 1 ) );
  268. if( $forms ) {
  269. // if we have a post, go to the "edit form" screen
  270. $form = array_pop( $forms );
  271. $redirect_url = mc4wp_get_edit_form_url( $form->ID );
  272. } else {
  273. // we don't have a form yet, go to "add new" screen
  274. $redirect_url = mc4wp_get_add_form_url();
  275. }
  276. wp_redirect( $redirect_url );
  277. exit;
  278. }
  279. /**
  280. * Show the Forms Settings page
  281. *
  282. * @internal
  283. */
  284. public function show_forms_page() {
  285. $view = ! empty( $_GET['view'] ) ? $_GET['view'] : '';
  286. /**
  287. * @ignore
  288. */
  289. do_action( 'mc4wp_admin_show_forms_page', $view );
  290. /**
  291. * @ignore
  292. */
  293. do_action( 'mc4wp_admin_show_forms_page-' . $view );
  294. }
  295. /**
  296. * Show the "Edit Form" page
  297. *
  298. * @internal
  299. */
  300. public function show_edit_page() {
  301. $form_id = ( ! empty( $_GET['form_id'] ) ) ? (int) $_GET['form_id'] : 0;
  302. $lists = $this->mailchimp->get_lists();
  303. try{
  304. $form = mc4wp_get_form( $form_id );
  305. } catch( Exception $e ) {
  306. echo '<h2>' . __( "Form not found.", 'mailchimp-for-wp' ) . '</h2>';
  307. echo '<p>' . $e->getMessage() . '</p>';
  308. echo '<p><a href="javascript:history.go(-1);"> &lsaquo; '. __( 'Go back' ) .'</a></p>';
  309. return;
  310. }
  311. $opts = $form->settings;
  312. $active_tab = ( isset( $_GET['tab'] ) ) ? $_GET['tab'] : 'fields';
  313. require dirname( __FILE__ ) . '/views/edit-form.php';
  314. }
  315. /**
  316. * Shows the "Add Form" page
  317. *
  318. * @internal
  319. */
  320. public function show_add_page() {
  321. $lists = $this->mailchimp->get_lists();
  322. $number_of_lists = count( $lists );
  323. require dirname( __FILE__ ) . '/views/add-form.php';
  324. }
  325. /**
  326. * Get URL for a tab on the current page.
  327. *
  328. * @since 3.0
  329. * @internal
  330. * @param $tab
  331. * @return string
  332. */
  333. public function tab_url( $tab ) {
  334. return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) );
  335. }
  336. /**
  337. * Registers UI for when shortcake is activated
  338. */
  339. public function register_shortcake_ui() {
  340. $assets = new MC4WP_Form_Asset_Manager();
  341. $assets->load_stylesheets();
  342. $forms = mc4wp_get_forms();
  343. $options = array();
  344. foreach( $forms as $form ) {
  345. $options[ $form->ID ] = $form->name;
  346. }
  347. /**
  348. * Register UI for your shortcode
  349. *
  350. * @param string $shortcode_tag
  351. * @param array $ui_args
  352. */
  353. shortcode_ui_register_for_shortcode( 'mc4wp_form', array(
  354. 'label' => esc_html__( 'MailChimp Sign-Up Form', 'mailchimp-for-wp' ),
  355. 'listItemImage' => 'dashicons-feedback',
  356. 'attrs' => array(
  357. array(
  358. 'label' => esc_html__( 'Select the form to show' ,'mailchimp-for-wp' ),
  359. 'attr' => 'id',
  360. 'type' => 'select',
  361. 'options' => $options
  362. )
  363. ),
  364. )
  365. );
  366. }
  367. }