GFPDFModelModel_Form_Settings

Model_Welcome_Screen.

Defined (1)

The class is defined in the following location(s).

/src/model/Model_Form_Settings.php  
  1. class Model_Form_Settings extends Helper_Abstract_Model { 
  2.  
  3. /** 
  4. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  5. * @var \GFPDF\Helper\Helper_Form 
  6. * @since 4.0 
  7. */ 
  8. protected $gform; 
  9.  
  10. /** 
  11. * Holds our log class 
  12. * @var \Monolog\Logger|LoggerInterface 
  13. * @since 4.0 
  14. */ 
  15. protected $log; 
  16.  
  17. /** 
  18. * Holds our Helper_Data object 
  19. * which we can autoload with any data needed 
  20. * @var \GFPDF\Helper\Helper_Data 
  21. * @since 4.0 
  22. */ 
  23. protected $data; 
  24.  
  25. /** 
  26. * Holds our Helper_Abstract_Options / Helper_Options_Fields object 
  27. * Makes it easy to access global PDF settings and individual form PDF settings 
  28. * @var \GFPDF\Helper\Helper_Options_Fields 
  29. * @since 4.0 
  30. */ 
  31. protected $options; 
  32.  
  33. /** 
  34. * Holds our Helper_Misc object 
  35. * Makes it easy to access common methods throughout the plugin 
  36. * @var \GFPDF\Helper\Helper_Misc 
  37. * @since 4.0 
  38. */ 
  39. protected $misc; 
  40.  
  41. /** 
  42. * Holds our Helper_Notices object 
  43. * which we can use to queue up admin messages for the user 
  44. * @var \GFPDF\Helper\Helper_Misc 
  45. * @since 4.0 
  46. */ 
  47. protected $notices; 
  48.  
  49. /** 
  50. * Holds our Helper_Templates object 
  51. * used to ease access to our PDF templates 
  52. * @var \GFPDF\Helper\Helper_Templates 
  53. * @since 4.0 
  54. */ 
  55. protected $templates; 
  56.  
  57. /** 
  58. * Setup our class by injecting all our dependancies 
  59. * @param \GFPDF\Helper\Helper_Abstract_Form $gform Our abstracted Gravity Forms helper functions 
  60. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  61. * @param \GFPDF\Helper\Helper_Data $data Our plugin data store 
  62. * @param \GFPDF\Helper\Helper_Abstract_Options $options Our options class which allows us to access any settings 
  63. * @param \GFPDF\Helper\Helper_Misc $misc Our miscellaneous class 
  64. * @param \GFPDF\Helper\Helper_Notices $notices Our notice class used to queue admin messages and errors 
  65. * @param \GFPDF\Helper\Helper_Templates $templates 
  66. * @since 4.0 
  67. */ 
  68. public function __construct( Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Data $data, Helper_Abstract_Options $options, Helper_Misc $misc, Helper_Notices $notices, Helper_Templates $templates ) { 
  69.  
  70. /** Assign our internal variables */ 
  71. $this->gform = $gform; 
  72. $this->log = $log; 
  73. $this->data = $data; 
  74. $this->options = $options; 
  75. $this->misc = $misc; 
  76. $this->notices = $notices; 
  77. $this->templates = $templates; 
  78.  
  79. /** 
  80. * Add the form settings tab. 
  81. * Override this function to add the tab conditionally. 
  82. * @param array $tabs The list of existing tags 
  83. * @return array modified list of $tabs 
  84. * @since 4.0 
  85. */ 
  86. public function add_form_settings_menu( $tabs ) { 
  87. $tabs[] = [ 
  88. 'name' => $this->data->slug,  
  89. 'label' => $this->data->short_title,  
  90. 'query' => [ 'pid' => null ],  
  91. ]; 
  92.  
  93. return $tabs; 
  94.  
  95. /** 
  96. * Setup the PDF Settings List View Logic 
  97. * @param integer $form_id The Gravity Form ID 
  98. * @return void 
  99. * @since 4.0 
  100. */ 
  101. public function process_list_view( $form_id ) { 
  102.  
  103. /** prevent unauthorized access */ 
  104. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  105. $this->log->addWarning( 'Lack of User Capabilities.' ); 
  106. wp_die( esc_html__( 'You do not have permission to access this page', 'gravity-forms-pdf-extended' ) ); 
  107.  
  108. $controller = $this->getController(); 
  109.  
  110. /** get the form object */ 
  111. $form = $this->gform->get_form( $form_id ); 
  112.  
  113. /** load our list table */ 
  114. $pdf_table = new Helper_PDF_List_Table( $form, $this->gform, $this->misc, $this->templates ); 
  115. $pdf_table->prepare_items(); 
  116.  
  117. /** pass to view */ 
  118. $controller->view->list( [ 
  119. 'title' => $this->data->title,  
  120. 'add_new_url' => $add_new_url = add_query_arg( [ 'pid' => 0 ] ),  
  121. 'list_items' => $pdf_table,  
  122. ] ); 
  123.  
  124. /** 
  125. * Setup the PDF Settings Add/Edit View Logic 
  126. * @param integer $form_id The Gravity Form ID 
  127. * @param integer $pdf_id The PDF configuration ID 
  128. * @return void 
  129. * @since 4.0 
  130. */ 
  131. public function show_edit_view( $form_id, $pdf_id ) { 
  132.  
  133. /** prevent unauthorized access */ 
  134. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  135. $this->log->addWarning( 'Lack of User Capabilities.' ); 
  136. wp_die( esc_html__( 'You do not have permission to access this page', 'gravity-forms-pdf-extended' ) ); 
  137.  
  138. $controller = $this->getController(); 
  139.  
  140. /** get the form object */ 
  141. $form = $this->gform->get_form( $form_id ); 
  142.  
  143. /** parse input and get required information */ 
  144. if ( ! $pdf_id ) { 
  145. if ( rgpost( 'gform_pdf_id' ) ) { 
  146. $pdf_id = rgpost( 'gform_pdf_id' ); 
  147. } else { 
  148. $pdf_id = uniqid(); 
  149.  
  150. /** re-register all our settings to show form-specific options */ 
  151. $this->options->register_settings( $this->options->get_registered_fields() ); 
  152.  
  153. /** re-register our Gravity Forms Notifications */ 
  154. $this->register_notifications( $form['notifications'] ); 
  155.  
  156. /** Pull the PDF settings */ 
  157. $pdf = $this->options->get_pdf( $form_id, $pdf_id ); 
  158.  
  159. /** prepare our data */ 
  160. $label = ( ! is_wp_error( $pdf ) && ! isset( $pdf['status'] ) ) ? esc_html__( 'Update PDF', 'gravity-forms-pdf-extended' ) : esc_html__( 'Add PDF', 'gravity-forms-pdf-extended' ); 
  161.  
  162. /** pass to view */ 
  163. $controller->view->add_edit( [ 
  164. 'pdf_id' => $pdf_id,  
  165. 'title' => $label,  
  166. 'button_label' => $label,  
  167. 'form' => $form,  
  168. 'pdf' => $pdf,  
  169. 'wp_editor_loaded' => class_exists( '_WP_Editors' ),  
  170. ] ); 
  171.  
  172. /** 
  173. * Validate, Sanatize and Update PDF settings 
  174. * @param integer $form_id The Gravity Form ID 
  175. * @param integer $pdf_id The PDF configuration ID 
  176. * @return boolean 
  177. * @since 4.0 
  178. */ 
  179. public function process_submission( $form_id, $pdf_id ) { 
  180.  
  181. /** prevent unauthorized access */ 
  182. if ( ! $this->gform->has_capability( 'gravityforms_edit_settings' ) ) { 
  183.  
  184. $this->log->addCritical( 'Lack of User Capabilities.', [ 
  185. 'user' => wp_get_current_user(),  
  186. 'user_meta' => get_user_meta( get_current_user_id() ),  
  187. ] ); 
  188.  
  189. wp_die( esc_html__( 'You do not have permission to access this page', 'gravity-forms-pdf-extended' ) ); 
  190.  
  191. /** Check Nonce is valid */ 
  192. if ( ! wp_verify_nonce( rgpost( 'gfpdf_save_pdf' ), 'gfpdf_save_pdf' ) ) { 
  193. $this->log->addWarning( 'Nonce Verification Failed.' ); 
  194. $this->notices->add_error( esc_html__( 'There was a problem saving your PDF settings. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  195.  
  196. return false; 
  197.  
  198. /** Check if we have a new PDF ID */ 
  199. if ( empty( $pdf_id ) ) { 
  200. $pdf_id = ( rgpost( 'gform_pdf_id' ) ) ? rgpost( 'gform_pdf_id' ) : false; 
  201.  
  202. $input = rgpost( 'gfpdf_settings' ); 
  203.  
  204. /** check appropriate settings */ 
  205. if ( ! is_array( $input ) || ! $pdf_id ) { 
  206. $this->log->addError( 'Invalid Data.', [ 
  207. 'post' => $input,  
  208. 'pid' => $pdf_id,  
  209. ] ); 
  210.  
  211. $this->notices->add_error( esc_html__( 'There was a problem saving your PDF settings. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  212.  
  213. return false; 
  214.  
  215. $sanitized = $this->settings_sanitize( $input ); 
  216.  
  217. /** Update our GFPDF settings */ 
  218. $sanitized['id'] = $pdf_id; 
  219. $sanitized['status'] = 'sanitizing'; /** used as a switch to tell when a record has been saved to the database, or stuck in validation */ 
  220.  
  221. /** Save current PDF state */ 
  222. $pdf = $this->options->get_pdf( $form_id, $pdf_id ); 
  223. $sanitized['active'] = ( ! is_wp_error( $pdf ) && isset( $pdf['active'] ) ) ? $pdf['active'] : true; 
  224.  
  225. $this->options->update_pdf( $form_id, $pdf_id, $sanitized, false ); 
  226.  
  227. /** Do validation */ 
  228. if ( empty( $sanitized['name'] ) || empty( $sanitized['filename'] ) || 
  229. ( $sanitized['pdf_size'] == 'CUSTOM' && ( (int) $sanitized['custom_pdf_size'][0] === 0 || (int) $sanitized['custom_pdf_size'][1] === 0 ) ) 
  230. ) { 
  231.  
  232. $this->log->addNotice( 'Validation failed.' ); 
  233. $this->notices->add_error( esc_html__( 'PDF could not be saved. Please enter all required information below.', 'gravity-forms-pdf-extended' ) ); 
  234.  
  235. return false; 
  236.  
  237. /** Remove our status */ 
  238. unset( $sanitized['status'] ); 
  239.  
  240. /** Update the database */ 
  241. $did_update = $this->options->update_pdf( $form_id, $pdf_id, $sanitized ); 
  242.  
  243. /** If it updated, let's update the global variable */ 
  244. if ( $did_update !== false ) { 
  245.  
  246. $this->log->addNotice( 'Successfully Saved.', [ 
  247. 'form_id' => $form_id,  
  248. 'pdf_id' => $pdf_id,  
  249. 'settings' => $sanitized,  
  250. ] ); 
  251.  
  252. $this->notices->add_notice( sprintf( esc_html__( 'PDF saved successfully. %sBack to PDF list.%s', 'gravity-forms-pdf-extended' ), '<a href="' . remove_query_arg( 'pid' ) . '">', '</a>' ) ); 
  253.  
  254. return true; 
  255.  
  256. $this->log->addError( 'Database Update Failed.' ); 
  257. $this->notices->add_error( esc_html__( 'There was a problem saving your PDF settings. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  258.  
  259. return false; 
  260.  
  261. /** 
  262. * Apply gfield_error class when validation fails, highlighting field blocks with problems 
  263. * @param array $fields Array of fields to process 
  264. * @return array Modified list of fields 
  265. * @since 4.0 
  266. */ 
  267. public function validation_error( $fields ) { 
  268.  
  269. /** 
  270. * Check if we actually need to do any validating 
  271. * Because of the way the Gravity Forms Settings page is processed we are hooking into the core 
  272. * "gfpdf_form_settings" filter which runs when ever update_option( 'pdf_form_settings' ) is run. 
  273. * We don't need to do any validation when not on the GF PDF Settings page 
  274. */ 
  275. if ( empty( $_POST['gfpdf_save_pdf'] ) ) { 
  276. return $fields; 
  277.  
  278. /** Check we have a valid nonce, or throw an error */ 
  279. if ( ! wp_verify_nonce( rgpost( 'gfpdf_save_pdf' ), 'gfpdf_save_pdf' ) ) { 
  280.  
  281. $this->log->addWarning( 'Nonce Verification Failed.' ); 
  282. $this->notices->add_error( esc_html__( 'There was a problem saving your PDF settings. Please try again.', 'gravity-forms-pdf-extended' ) ); 
  283.  
  284. return false; 
  285.  
  286. $input = rgpost( 'gfpdf_settings' ); 
  287.  
  288. /** Throw errors on required fields */ 
  289. foreach ( $fields as $key => &$field ) { 
  290.  
  291. if ( isset( $field['required'] ) && $field['required'] === true ) { 
  292.  
  293. /** Get field value */ 
  294. $value = ( isset( $input[ $field['id'] ] ) ) ? $input[ $field['id'] ] : ''; 
  295.  
  296. /** Set a class if it doesn't exist */ 
  297. $field['class'] = ( isset( $field['class'] ) ) ? $field['class'] : ''; 
  298.  
  299. /** Add way to skip the highlighting of errors */ 
  300. $skip_errors = apply_filters( 'gfpdf_skip_highlight_errors', false, $field, $input ); 
  301.  
  302. if ( $skip_errors ) { 
  303. continue; 
  304.  
  305. /** If the value is an array ensure all items have values */ 
  306. if ( is_array( $value ) ) { 
  307.  
  308. $size = sizeof( $value ); 
  309. if ( sizeof( array_filter( $value ) ) !== $size ) { 
  310. $field['class'] .= ' gfield_error'; 
  311. } else { 
  312.  
  313. /** 
  314. * If string, sanitize and add error if appropriate 
  315. * See https://gravitypdf.com/documentation/v4/gfpdf_form_settings_sanitize/ for more details about this filter 
  316. */ 
  317. $value = apply_filters( 'gfpdf_form_settings_sanitize_text', $value, $key ); 
  318. if ( empty( $value ) ) { 
  319. $field['class'] .= ' gfield_error'; 
  320.  
  321. return $fields; 
  322.  
  323. /** 
  324. * Do further checks to see if the custom PDF size should in fact be marked as an error 
  325. * Because it is dependant on the paper size option in some cases it shouldn't be highlighted 
  326. * @param boolean $skip Whether to skip error highlighting checks 
  327. * @param array $field The Gravity Form field 
  328. * @param array $input The user input 
  329. * @return boolean 
  330. * @since 4.0 
  331. */ 
  332. public function check_custom_size_error_highlighting( $skip, $field, $input ) { 
  333.  
  334. if ( $field['id'] == 'custom_pdf_size' ) { 
  335.  
  336. /** Skip if not currently being shown */ 
  337. if ( $input['pdf_size'] !== 'CUSTOM' ) { 
  338. return true; 
  339.  
  340. return $skip; 
  341.  
  342. /** 
  343. * Similar to Helper_Abstract_Options->settings_sanitize() except we don't need as robust validation and error checking 
  344. * @param array $input Fields to process 
  345. * @return array Sanitized fields 
  346. * @since 4.0 
  347. */ 
  348. public function settings_sanitize( $input = [] ) { 
  349.  
  350. $settings = $this->options->get_registered_fields(); 
  351. $sections = [ 
  352. 'form_settings',  
  353. 'form_settings_appearance',  
  354. 'form_settings_custom_appearance',  
  355. 'form_settings_advanced',  
  356. ]; 
  357.  
  358. foreach ( $sections as $s ) { 
  359. $input = apply_filters( 'gfpdf_settings_' . $s . '_sanitize', $input ); 
  360.  
  361. /** Loop through each setting being saved and pass it through a sanitization filter */ 
  362. if ( is_array( $input ) && 0 < sizeof( $input ) ) { 
  363. foreach ( $input as $key => $value ) { 
  364.  
  365. foreach ( $sections as $s ) { 
  366.  
  367. /** only process field if found in the section */ 
  368. if ( isset( $settings[ $s ][ $key ] ) ) { 
  369. $type = isset( $settings[ $s ][ $key ]['type'] ) ? $settings[ $s ][ $key ]['type'] : false; 
  370.  
  371. /** 
  372. * General filter 
  373. * See https://gravitypdf.com/documentation/v4/gfpdf_form_settings_sanitize/ for more details about this filter 
  374. */ 
  375. $input[ $key ] = apply_filters( 'gfpdf_form_settings_sanitize', $input[ $key ], $key, $input, $settings[ $s ][ $key ] ); 
  376.  
  377. if ( $type ) { 
  378. /** 
  379. * Field type specific filter 
  380. * See https://gravitypdf.com/documentation/v4/gfpdf_form_settings_sanitize/ for more details about this filter 
  381. */ 
  382. $input[ $key ] = apply_filters( 'gfpdf_form_settings_sanitize_' . $type, $input[ $key ], $key, $input, $settings[ $s ][ $key ] ); 
  383.  
  384. return $input; 
  385.  
  386. /** 
  387. * Check if we are on the Form Settings Edit page and gets the appropriate template name 
  388. * @return string The current saved PDF template 
  389. * @since 4.0 
  390. */ 
  391. public function get_template_name_from_current_page() { 
  392.  
  393. $pid = ( ! empty( $_GET['pid'] ) ) ? rgget( 'pid' ) : rgpost( 'gform_pdf_id' ); 
  394. $form_id = ( isset( $_GET['id'] ) ) ? (int) $_GET['id'] : 0; 
  395.  
  396. /** If we don't have a specific PDF we'll use the defaults */ 
  397. if ( empty( $pid ) || empty( $form_id ) ) { 
  398. $template = $this->options->get_option( 'default_template', 'zadani' ); 
  399. } else { 
  400. /** Load the PDF configuration */ 
  401. $pdf = $this->options->get_pdf( $form_id, $pid ); 
  402.  
  403. if ( ! is_wp_error( $pdf ) ) { 
  404. $template = $pdf['template']; 
  405. } else { 
  406. $template = ''; 
  407.  
  408. return $template; 
  409.  
  410. /** 
  411. * If the PDF ID exists (either POST or GET) and we have a template with a config file 
  412. * we will load any fields loaded in the config file 
  413. * @param array $settings Any existing settings loaded 
  414. * @return array 
  415. * @since 4.0 
  416. */ 
  417. public function register_custom_appearance_settings( $settings ) { 
  418. $template = $this->get_template_name_from_current_page(); 
  419. $class = $this->templates->get_config_class( $template ); 
  420.  
  421. return $this->setup_custom_appearance_settings( $class, $settings ); 
  422.  
  423. /** 
  424. * To allow for correct backwards compatibility with our v3 templates we need to hide the font, size and colour 
  425. * information when selected. To allow this behaviour we're going to assign a 'data-template_group' attribute 
  426. * to the template select box which our JS can pick up and use to toggle those fields 
  427. * @param array $settings The current PDF settings 
  428. * @return array 
  429. * @since 4.0 
  430. */ 
  431. public function register_template_group( $settings ) { 
  432.  
  433. /** Add our template group */ 
  434. if ( isset( $settings['template'] ) && is_array( $settings['template'] ) ) { 
  435.  
  436. $template_info = $this->templates->get_template_info_by_id( $this->get_template_name_from_current_page() ); 
  437.  
  438. /** Ensure the key we want is an array, otherwise set it to one */ 
  439. $settings['template']['data'] = ( isset( $settings['template']['data'] ) && is_array( $settings['template']['data'] ) ) ? $settings['template']['data'] : []; 
  440. $settings['template']['data']['template_group'] = $template_info['group']; 
  441.  
  442. return $settings; 
  443.  
  444. /** 
  445. * Load our custom appearance settings (if needed) 
  446. * @param object $class The template configuration class 
  447. * @param array $settings Any current settings 
  448. * @return array 
  449. * @since 4.0 
  450. */ 
  451. public function setup_custom_appearance_settings( $class, $settings = [] ) { 
  452.  
  453. /** If class isn't an instance of our interface return $settings */ 
  454. if ( ! ( $class instanceof Helper_Interface_Config ) ) { 
  455.  
  456. $this->log->addWarning( 'Instanceof Failed.', [ 
  457. 'object' => get_class( $class ),  
  458. 'type' => 'Helper_Interface_Config',  
  459. ] ); 
  460.  
  461. return $settings; 
  462.  
  463. /** 
  464. * Now we have the class initialised, let's load our configuration array 
  465. */ 
  466. $template_settings = $class->configuration(); 
  467.  
  468. /** register any custom fields */ 
  469. if ( isset( $template_settings['fields'] ) && is_array( $template_settings['fields'] ) ) { 
  470. foreach ( $template_settings['fields'] as $key => $field ) { 
  471. $settings[ $key ] = $field; 
  472.  
  473. $settings = $this->setup_core_custom_appearance_settings( $settings, $class, $template_settings ); 
  474.  
  475. $this->log->addNotice( 'Setup Template-Specific Settings', [ 
  476. 'settings' => $settings,  
  477. ] ); 
  478.  
  479. return $settings; 
  480.  
  481. /** 
  482. * Setup any core fields that are registered to the PDF template 
  483. * @param array $settings Any current settings 
  484. * @param \GFPDF\Helper\Helper_Interface_Config $class The template configuration class 
  485. * @param array $template_settings Loaded configuration array 
  486. * @return array 
  487. * @since 4.0 
  488. */ 
  489. public function setup_core_custom_appearance_settings( $settings = [], Helper_Interface_Config $class, $template_settings ) { 
  490.  
  491. /** register our core fields */ 
  492. $core_fields = [ 
  493. 'show_form_title' => [ $this->options, 'get_form_title_display_field' ],  
  494. 'show_page_names' => [ $this->options, 'get_page_names_display_field' ],  
  495. 'show_html' => [ $this->options, 'get_html_display_field' ],  
  496. 'show_section_content' => [ $this->options, 'get_section_content_display_field' ],  
  497. 'enable_conditional' => [ $this->options, 'get_conditional_display_field' ],  
  498. 'show_empty' => [ $this->options, 'get_empty_display_field' ],  
  499.  
  500. 'background_color' => [ $this->options, 'get_background_color_field' ],  
  501. 'background_image' => [ $this->options, 'get_background_image_field' ],  
  502. 'header' => [ $this->options, 'get_header_field' ],  
  503. 'first_header' => [ $this->options, 'get_first_page_header_field' ],  
  504. 'footer' => [ $this->options, 'get_footer_field' ],  
  505. 'first_footer' => [ $this->options, 'get_first_page_footer_field' ],  
  506. ]; 
  507.  
  508. /** See https://gravitypdf.com/documentation/v4/gfpdf_core_template_fields_list/ for more details about this filter */ 
  509. $core_fields = apply_filters( 'gfpdf_core_template_fields_list', $core_fields, $template_settings, $class ); 
  510.  
  511. foreach ( $core_fields as $id => $method ) { 
  512.  
  513. if ( isset( $template_settings['core'][ $id ] ) && $template_settings['core'][ $id ] === true ) { 
  514. $settings[ $id ] = call_user_func( $method ); 
  515.  
  516. return $settings; 
  517.  
  518. /** 
  519. * Auto strip the .pdf extension when sanitizing 
  520. * @param string $value The value entered by the user 
  521. * @param string $key The field to be parsed 
  522. * @return string The sanitized data 
  523. */ 
  524. public function parse_filename_extension( $value, $key ) { 
  525.  
  526. if ( $key == 'filename' ) { 
  527. $value = $this->misc->remove_extension_from_string( $value ); 
  528.  
  529. return $value; 
  530.  
  531. /** 
  532. * Auto decode the JSON conditional logic string 
  533. * @param string $value The value entered by the user 
  534. * @param string $key The field to be parsed 
  535. * @return string The sanitized data 
  536. */ 
  537. public function decode_json( $value, $key ) { 
  538.  
  539. if ( $key == 'conditionalLogic' ) { 
  540. return json_decode( $value, true ); 
  541.  
  542. return $value; 
  543.  
  544.  
  545. /** 
  546. * Update our notification form settings which is specific to the PDF Form Settings Page (i.e we need an actual $form object which isn't present when we originally register the settings) 
  547. * @param array $notifications The current form notifications 
  548. * @return void 
  549. * @since 4.0 
  550. */ 
  551. public function register_notifications( $notifications ) { 
  552.  
  553. /** Loop through notifications and format it to our standard */ 
  554. if ( is_array( $notifications ) ) { 
  555. $options = []; 
  556.  
  557. /** Filter out the save and continue notifications */ 
  558. $omit = [ 'form_saved', 'form_save_email_requested' ]; 
  559.  
  560. foreach ( $notifications as $notification ) { 
  561. $event = ( isset( $notification['event'] ) ) ? $notification['event'] : ''; 
  562.  
  563. if ( ! in_array( $event, $omit ) ) { 
  564. $options[ $notification['id'] ] = $notification['name']; 
  565.  
  566. /** Apply our settings update */ 
  567. $this->options->update_registered_field( 'form_settings', 'notification', 'options', $options ); 
  568.  
  569. /** 
  570. * AJAX Endpoint for deleting PDF Settings 
  571. * @return string JSON 
  572. * @internal param $_POST ['nonce'] a valid nonce 
  573. * @internal param $_POST ['fid'] a valid form ID 
  574. * @internal param $_POST ['pid'] a valid PDF ID 
  575. * @since 4.0 
  576. */ 
  577. public function delete_gf_pdf_setting() { 
  578.  
  579. $fid = ( isset( $_POST['fid'] ) ) ? (int) $_POST['fid'] : 0; 
  580. $pid = ( isset( $_POST['pid'] ) ) ? $_POST['pid'] : ''; 
  581. $nonce_id = "gfpdf_delete_nonce_{$fid}_{$pid}"; 
  582.  
  583. /** User / CORS validation */ 
  584. $this->misc->handle_ajax_authentication( 'Delete PDF Settings', 'gravityforms_edit_settings', $nonce_id ); 
  585.  
  586. /** Delete PDF settings */ 
  587. $results = $this->options->delete_pdf( $fid, $pid ); 
  588.  
  589. if ( $results && ! is_wp_error( $results ) ) { 
  590.  
  591. $this->log->addNotice( 'AJAX Endpoint Successful' ); 
  592.  
  593. $return = [ 
  594. 'msg' => esc_html__( 'PDF successfully deleted.', 'gravity-forms-pdf-extended' ),  
  595. ]; 
  596.  
  597. echo json_encode( $return ); 
  598. wp_die(); 
  599.  
  600. $errors = []; 
  601. if ( is_wp_error( $results ) ) { 
  602. $errors = [ 
  603. 'WP_Error_Message' => $results->get_error_message(),  
  604. 'WP_Error_Code' => $results->get_error_code(),  
  605. ]; 
  606.  
  607. $this->log->addError( 'AJAX Endpoint Failed', $errors ); 
  608.  
  609. /** Internal Server Error */ 
  610. wp_die( '500', 500 ); 
  611.  
  612. /** 
  613. * AJAX Endpoint for duplicating PDF Settings 
  614. * @return string JSON 
  615. * @internal param $_POST ['nonce'] a valid nonce 
  616. * @internal param $_POST ['fid'] a valid form ID 
  617. * @internal param $_POST ['pid'] a valid PDF ID 
  618. * @since 4.0 
  619. */ 
  620. public function duplicate_gf_pdf_setting() { 
  621.  
  622. $fid = ( isset( $_POST['fid'] ) ) ? (int) $_POST['fid'] : 0; 
  623. $pid = ( isset( $_POST['pid'] ) ) ? $_POST['pid'] : ''; 
  624.  
  625. $nonce_id = "gfpdf_duplicate_nonce_{$fid}_{$pid}"; 
  626.  
  627. /** User / CORS validation */ 
  628. $this->misc->handle_ajax_authentication( 'Duplicate PDF Settings', 'gravityforms_edit_settings', $nonce_id ); 
  629.  
  630. /** Duplicate PDF config */ 
  631. $config = $this->options->get_pdf( $fid, $pid ); 
  632.  
  633. if ( ! is_wp_error( $config ) ) { 
  634. $config['id'] = uniqid(); 
  635. $config['name'] = $config['name'] . ' (copy)'; 
  636. $config['active'] = false; 
  637.  
  638. $results = $this->options->update_pdf( $fid, $config['id'], $config ); 
  639.  
  640. if ( $results ) { 
  641. $this->log->addNotice( 'AJAX Endpoint Successful' ); 
  642.  
  643. /** @todo just use the same nonce for all requests since WP nonces aren't one-time user (time based) */ 
  644. $dup_nonce = wp_create_nonce( "gfpdf_duplicate_nonce_{$fid}_{$config['id']}" ); 
  645. $del_nonce = wp_create_nonce( "gfpdf_delete_nonce_{$fid}_{$config['id']}" ); 
  646. $state_nonce = wp_create_nonce( "gfpdf_state_nonce_{$fid}_{$config['id']}" ); 
  647.  
  648. $return = [ 
  649. 'msg' => esc_html__( 'PDF successfully duplicated.', 'gravity-forms-pdf-extended' ),  
  650. 'pid' => $config['id'],  
  651. 'name' => $config['name'],  
  652. 'dup_nonce' => $dup_nonce,  
  653. 'del_nonce' => $del_nonce,  
  654. 'state_nonce' => $state_nonce,  
  655. ]; 
  656.  
  657. echo json_encode( $return ); 
  658. wp_die(); 
  659.  
  660. $this->log->addError( 'AJAX Endpoint Failed', [ 
  661. 'WP_Error_Message' => $config->get_error_message(),  
  662. 'WP_Error_Code' => $config->get_error_code(),  
  663. ] ); 
  664.  
  665. /** Internal Server Error */ 
  666. wp_die( '500', 500 ); 
  667.  
  668. /** 
  669. * AJAX Endpoint for changing the PDF Settings state 
  670. * @return string JSON 
  671. * @internal param $_POST ['nonce'] a valid nonce 
  672. * @internal param $_POST ['fid'] a valid form ID 
  673. * @internal param $_POST ['pid'] a valid PDF ID 
  674. * @since 4.0 
  675. */ 
  676. public function change_state_pdf_setting() { 
  677.  
  678. $fid = ( isset( $_POST['fid'] ) ) ? (int) $_POST['fid'] : 0; 
  679. $pid = ( isset( $_POST['pid'] ) ) ? $_POST['pid'] : ''; 
  680. $nonce_id = "gfpdf_state_nonce_{$fid}_{$pid}"; 
  681.  
  682. /** User / CORS validation */ 
  683. $this->misc->handle_ajax_authentication( 'Change PDF Settings State', 'gravityforms_edit_settings', $nonce_id ); 
  684.  
  685. /** Change the PDF state */ 
  686. $config = $this->options->get_pdf( $fid, $pid ); 
  687.  
  688. if ( ! is_wp_error( $config ) ) { 
  689.  
  690. /** toggle state */ 
  691. $config['active'] = ( $config['active'] === true ) ? false : true; 
  692. $state = ( $config['active'] ) ? esc_attr__( 'Active', 'gravity-forms-pdf-extended' ) : esc_attr__( 'Inactive', 'gravity-forms-pdf-extended' ); 
  693. $src = $this->gform->get_plugin_url() . '/images/active' . intval( $config['active'] ) . '.png'; 
  694.  
  695. $results = $this->options->update_pdf( $fid, $config['id'], $config ); 
  696.  
  697. if ( $results ) { 
  698. $this->log->addNotice( 'AJAX Endpoint Successful' ); 
  699.  
  700. $return = [ 
  701. 'state' => $state,  
  702. 'src' => $src,  
  703. 'fid' => $fid,  
  704. 'pid' => $config['id'],  
  705. ]; 
  706.  
  707. echo json_encode( $return ); 
  708. wp_die(); 
  709.  
  710. $this->log->addError( 'AJAX Endpoint Failed', [ 
  711. 'WP_Error_Message' => $config->get_error_message(),  
  712. 'WP_Error_Code' => $config->get_error_code(),  
  713. ] ); 
  714.  
  715. /** Internal Server Error */ 
  716. wp_die( '500', 500 ); 
  717.  
  718. /** 
  719. * AJAX Endpoint for rendering the template field settings options 
  720. * @return string JSON 
  721. * @internal param $_POST ['template'] the template to select 
  722. * @since 4.0 
  723. */ 
  724. public function render_template_fields() { 
  725.  
  726. /** User / CORS validation */ 
  727. $this->misc->handle_ajax_authentication( 'Render Template Custom Fields', 'gravityforms_edit_settings' ); 
  728.  
  729. /** get the current template */ 
  730. $template = ( isset( $_POST['template'] ) ) ? $_POST['template'] : ''; 
  731. $type = ( isset( $_POST['type'] ) ) ? $_POST['type'] : ''; 
  732. $class = $this->templates->get_config_class( $template ); 
  733. $settings = $this->setup_custom_appearance_settings( $class ); 
  734.  
  735. /** Only handle fields when in the PDF Forms Settings, and not in the general settings */ 
  736. if ( $type != 'gfpdf_settings[default_template]' ) { 
  737.  
  738. /** Get the template type so we can return out to the browser */ 
  739. $template_data = $this->templates->get_template_info_by_id( $template ); 
  740. $template_type = mb_strtolower( $template_data['group'] ); 
  741.  
  742. /** add our filter to override what template gets rendered (by default it is the current selected template in the config) */ 
  743. add_filter( 'gfpdf_form_settings_custom_appearance', function () use ( &$settings ) { 
  744. /** check if the template has any configuration */ 
  745. return $settings; 
  746. }, 100 ); 
  747.  
  748. /** Ensure our new fields are registered */ 
  749. $this->options->register_settings( $this->options->get_registered_fields() ); 
  750.  
  751. /** generate the HTML */ 
  752. ob_start(); 
  753.  
  754. do_settings_fields( 'gfpdf_settings_form_settings_custom_appearance', 'gfpdf_settings_form_settings_custom_appearance' ); 
  755.  
  756. $html = ob_get_clean(); 
  757.  
  758. /** 
  759. * Pass the required wp_editor IDs and settings in our AJAX response so the client 
  760. * can correctly load the instances. 
  761. */ 
  762. $editors = []; 
  763.  
  764. foreach ( $settings as $field ) { 
  765. if ( isset( $field['type'] ) && $field['type'] == 'rich_editor' ) { 
  766. $editors[] = 'gfpdf_settings_' . $field['id']; 
  767.  
  768. $editor_init = ( isset( $this->data->tiny_mce_editor_settings ) ) ? $this->data->tiny_mce_editor_settings : null; 
  769. $html = ( isset( $html ) && strlen( trim( $html ) ) > 0 ) ? $html : null; 
  770. $editors = ( isset( $editors ) ) ? $editors : null; 
  771. $template_type = ( isset( $template_type ) ) ? $template_type : null; 
  772.  
  773. $return = [ 
  774. 'fields' => $html,  
  775. 'editors' => $editors,  
  776. 'editor_init' => $editor_init,  
  777. 'template_type' => $template_type,  
  778. ]; 
  779.  
  780. $this->log->addNotice( 'AJAX Endpoint Successful', $return ); 
  781.  
  782. echo json_encode( $return ); 
  783.  
  784. /** end AJAX function */ 
  785. wp_die();