GFPDFHelperHelper_Abstract_Options

Class to set up the settings api callbacks.

Defined (1)

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

/src/helper/abstract/Helper_Abstract_Options.php  
  1. abstract class Helper_Abstract_Options implements Helper_Interface_Filters { 
  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_Templates object 
  27. * used to ease access to our PDF templates 
  28. * @var \GFPDF\Helper\Helper_Templates 
  29. * @since 4.0 
  30. */ 
  31. protected $templates; 
  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_Notices 
  45. * @since 4.0 
  46. */ 
  47. protected $notices; 
  48.  
  49. /** 
  50. * Holds the current global user settings 
  51. * @var array 
  52. * @since 4.0 
  53. */ 
  54. private $settings = []; 
  55.  
  56. /** 
  57. * Holds the Gravity Form PDF Settings 
  58. * @var array 
  59. * @since 4.0 
  60. */ 
  61. private $form_settings = []; 
  62.  
  63.  
  64. /** 
  65. * Helper_Abstract_Options constructor. 
  66. * @param \Monolog\Logger|LoggerInterface $log 
  67. * @param \GFPDF\Helper\Helper_Abstract_Form $gform 
  68. * @param \GFPDF\Helper\Helper_Data $data 
  69. * @param \GFPDF\Helper\Helper_Misc $misc 
  70. * @param \GFPDF\Helper\Helper_Notices $notices 
  71. * @param \GFPDF\Helper\Helper_Templates $templates 
  72. * @since 4.0 
  73. */ 
  74. public function __construct( LoggerInterface $log, Helper_Abstract_Form $gform, Helper_Data $data, Helper_Misc $misc, Helper_Notices $notices, Helper_Templates $templates ) { 
  75.  
  76. /** Assign our internal variables */ 
  77. $this->log = $log; 
  78. $this->gform = $gform; 
  79. $this->data = $data; 
  80. $this->misc = $misc; 
  81. $this->notices = $notices; 
  82. $this->templates = $templates; 
  83.  
  84. /** 
  85. * Returns an array of registered fields 
  86. * @return array 
  87. * @since 4.0 
  88. */ 
  89. abstract public function get_registered_fields(); 
  90.  
  91. /** 
  92. * Initialise the options API 
  93. * @return void 
  94. * @since 4.0 
  95. */ 
  96. public function init() { 
  97. $this->set_plugin_settings(); 
  98. $this->add_filters(); 
  99.  
  100.  
  101. /** 
  102. * Add our filters 
  103. * @return void 
  104. * @since 4.0 
  105. */ 
  106. public function add_filters() { 
  107.  
  108. /** Register our core santize functions */ 
  109. add_filter( 'gfpdf_settings_sanitize', [ $this, 'sanitize_required_field' ], 10, 4 ); 
  110. add_filter( 'gfpdf_settings_sanitize', [ $this, 'sanitize_all_fields' ], 10, 4 ); 
  111.  
  112. add_filter( 'gfpdf_settings_sanitize_text', [ $this, 'sanitize_trim_field' ] ); 
  113. add_filter( 'gfpdf_settings_sanitize_textarea', [ $this, 'sanitize_trim_field' ] ); 
  114. add_filter( 'gfpdf_settings_sanitize_number', [ $this, 'sanitize_number_field' ] ); 
  115. add_filter( 'gfpdf_settings_sanitize_paper_size', [ $this, 'sanitize_paper_size' ] ); 
  116.  
  117. /** 
  118. * Get the plugin's settings from the database 
  119. * @since 4.0 
  120. * @return void 
  121. */ 
  122. public function set_plugin_settings() { 
  123. /** assign our settings */ 
  124. $this->settings = $this->get_settings(); 
  125.  
  126. /** 
  127. * Add all settings sections and fields 
  128. * @param array $fields Fields that should be registered 
  129. * @since 4.0 
  130. * @return void 
  131. */ 
  132. public function register_settings( $fields = [] ) { 
  133. global $wp_settings_fields; 
  134.  
  135. foreach ( $fields as $tab => $settings ) { 
  136.  
  137. /** Clear all previously set types */ 
  138. $group = 'gfpdf_settings_' . $tab; 
  139. if ( isset( $wp_settings_fields[ $group ] ) ) { 
  140. unset( $wp_settings_fields[ $group ] ); 
  141.  
  142. foreach ( $settings as $option ) { 
  143.  
  144. $name = isset( $option['name'] ) ? $option['name'] : ''; 
  145.  
  146. add_settings_field( 
  147. 'gfpdf_settings[' . $option['id'] . ']',  
  148. $name,  
  149. method_exists( $this, $option['type'] . '_callback' ) ? [ 
  150. $this,  
  151. $option['type'] . '_callback',  
  152. ] : [ $this, 'missing_callback' ],  
  153. 'gfpdf_settings_' . $tab,  
  154. 'gfpdf_settings_' . $tab,  
  155. 'id' => isset( $option['id'] ) ? $option['id'] : null,  
  156. 'desc' => ! empty( $option['desc'] ) ? $option['desc'] : '',  
  157. 'desc2' => ! empty( $option['desc2'] ) ? $option['desc2'] : '',  
  158. 'type' => isset( $option['type'] ) ? $option['type'] : null,  
  159. 'name' => isset( $option['name'] ) ? $option['name'] : null,  
  160. 'size' => isset( $option['size'] ) ? $option['size'] : null,  
  161. 'options' => isset( $option['options'] ) ? $option['options'] : '',  
  162. 'std' => isset( $option['std'] ) ? $option['std'] : '',  
  163. 'min' => isset( $option['min'] ) ? $option['min'] : null,  
  164. 'max' => isset( $option['max'] ) ? $option['max'] : null,  
  165. 'step' => isset( $option['step'] ) ? $option['step'] : null,  
  166. 'chosen' => isset( $option['chosen'] ) ? $option['chosen'] : null,  
  167. 'class' => isset( $option['class'] ) ? $option['class'] : null,  
  168. 'inputClass' => isset( $option['inputClass'] ) ? $option['inputClass'] : null,  
  169. 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null,  
  170. 'tooltip' => isset( $option['tooltip'] ) ? $option['tooltip'] : null,  
  171. 'multiple' => isset( $option['multiple'] ) ? $option['multiple'] : null,  
  172. 'required' => isset( $option['required'] ) ? $option['required'] : null,  
  173. 'uploaderTitle' => isset( $option['uploaderTitle'] ) ? $option['uploaderTitle'] : null,  
  174. 'uploaderButtonText' => isset( $option['uploaderButtonText'] ) ? $option['uploaderButtonText'] : null,  
  175. 'toggle' => isset( $option['toggle'] ) ? $option['toggle'] : null,  
  176. 'data' => isset( $option['data'] ) ? $option['data'] : null,  
  177. ); 
  178.  
  179. /** Creates our settings in the options table */ 
  180. register_setting( 'gfpdf_settings', 'gfpdf_settings', [ $this, 'settings_sanitize' ] ); 
  181.  
  182. /** 
  183. * Update a current registered settings 
  184. * @param string $group_id The top-level group we're updating 
  185. * @param string $setting_id The section group we're updating 
  186. * @param string $option_id The option we are updating 
  187. * @param mixed $option_value The new option value 
  188. * @return boolean True on success, false on failure 
  189. * @since 4.0 
  190. */ 
  191. public function update_registered_field( $group_id, $setting_id, $option_id, $option_value ) { 
  192. global $wp_settings_fields; 
  193.  
  194. $group = 'gfpdf_settings_' . $group_id; 
  195. $setting = "gfpdf_settings[$setting_id]"; 
  196.  
  197. /** Check if our setting exists */ 
  198. if ( isset( $wp_settings_fields[ $group ][ $group ][ $setting ]['args'][ $option_id ] ) ) { 
  199. $wp_settings_fields[ $group ][ $group ][ $setting ]['args'][ $option_id ] = $option_value; 
  200.  
  201. return true; 
  202.  
  203. return false; 
  204.  
  205. /** 
  206. * Get Settings 
  207. * Retrieves all plugin settings 
  208. * @since 4.0 
  209. * @return array GFPDF settings 
  210. */ 
  211. public function get_settings() { 
  212.  
  213. /** 
  214. * We are storing temporary settings in a transient when validation fails. 
  215. * This allows us to keep track of the updated fields without updating main settings in the DB 
  216. * We'll check if the transient exists and use it, otherwise get the main plugin settings from the options table 
  217. */ 
  218. $tmp_settings = get_transient( 'gfpdf_settings_user_data' ); 
  219. $is_temp = ( $tmp_settings !== false ) ? true : false; 
  220.  
  221. if ( $is_temp ) { 
  222. delete_transient( 'gfpdf_settings_user_data' ); 
  223.  
  224. $settings = ( $is_temp ) ? (array) $tmp_settings : get_option( 'gfpdf_settings', [] ); 
  225.  
  226. /** See https://gravitypdf.com/documentation/v4/gfpdf_get_settings/ for more details about this filter */ 
  227.  
  228. return apply_filters( 'gfpdf_get_settings', $settings, $is_temp ); 
  229.  
  230. /** 
  231. * Get form settings if on that page in the admin area (by having ID and PID set in the $_GET or $_POST variables) 
  232. * Use get_pdf( $form_id, $pdf_id ) if you want to get a particular PDF setting 
  233. * @return array The stored form settings 
  234. * @since 4.0 
  235. */ 
  236. public function get_form_settings() { 
  237.  
  238. /** get GF settings */ 
  239. $form_id = ( ! empty( $_GET['id'] ) ) ? (int) rgget( 'id' ) : (int) rgpost( 'id' ); 
  240. $pid = ( ! empty( $_GET['pid'] ) ) ? rgget( 'pid' ) : rgpost( 'gform_pdf_id' ); 
  241.  
  242. /** return early if no ID set */ 
  243. if ( ! $form_id || ! $pid ) { 
  244. return []; 
  245.  
  246. $settings = $this->get_pdf( $form_id, $pid ); 
  247.  
  248. if ( ! is_wp_error( $settings ) ) { 
  249. /** get the selected form settings */ 
  250. return $settings; 
  251.  
  252. $this->log->addError( 'Settings Retreival Error', [ 
  253. 'form_id' => $form_id,  
  254. 'pid' => $pid,  
  255. 'WP_Error_Message' => $settings->get_error_message(),  
  256. 'WP_Error_Code' => $settings->get_error_code(),  
  257. ] ); 
  258.  
  259. /** there was an error */ 
  260.  
  261. return []; 
  262.  
  263. /** 
  264. * Get Form Settings 
  265. * Retrieves all form PDF settings 
  266. * @since 4.0 
  267. * @param integer $form_id The Gravity Form ID 
  268. * @return array|WP_Error An array of GFPDF settings, or WP_Error 
  269. */ 
  270. public function get_form_pdfs( $form_id ) { 
  271.  
  272. if ( ! isset( $this->data->form_settings ) ) { 
  273. $this->data->form_settings = []; 
  274.  
  275. $form_id = (int) $form_id; 
  276.  
  277. if ( 0 === $form_id ) { 
  278.  
  279. $error = new WP_Error( 'invalid_id', esc_html__( 'You must pass in a valid form ID', 'gravity-forms-pdf-extended' ) ); 
  280. $this->log->addError( 'Error Getting Settings.', [ 
  281. 'WP_Error_Message' => $error->get_error_message(),  
  282. 'WP_Error_Code' => $error->get_error_code(),  
  283. ] ); 
  284.  
  285. return $error; 
  286.  
  287. /** If we haven't pulled the form meta data from the database do so now */ 
  288. if ( ! isset( $this->data->form_settings[ $form_id ] ) ) { 
  289.  
  290. $form = $this->gform->get_form( $form_id ); 
  291.  
  292. if ( empty( $form ) ) { 
  293.  
  294. $error = new WP_Error( 'invalid_id', esc_html__( 'You must pass in a valid form ID', 'gravity-forms-pdf-extended' ) ); 
  295. $this->log->addError( 'Error Getting Settings.', [ 
  296. 'WP_Error_Message' => $error->get_error_message(),  
  297. 'WP_Error_Code' => $error->get_error_code(),  
  298. ] ); 
  299.  
  300. return $error; 
  301.  
  302. /** Pull the settings from the $form object, if they exist */ 
  303. $settings = ( isset( $form['gfpdf_form_settings'] ) ) ? $form['gfpdf_form_settings'] : []; 
  304.  
  305. $this->data->form_settings[ $form_id ] = $settings; 
  306.  
  307. /** return the form meta data */ 
  308.  
  309. return $this->data->form_settings[ $form_id ]; 
  310.  
  311. /** 
  312. * Get pdf config 
  313. * Looks to see if the specified setting exists, returns default if not 
  314. * @since 4.0 
  315. * @param integer $form_id The Gravity Form ID 
  316. * @param string $pdf_id The Gravity Form PDF ID 
  317. * @return array|WP_Error 
  318. */ 
  319. public function get_pdf( $form_id, $pdf_id ) { 
  320.  
  321. $this->log->addNotice( 'Getting Settings.', [ 
  322. 'form_id' => $form_id,  
  323. 'pdf_id' => $pdf_id,  
  324. ] ); 
  325.  
  326. $gfpdf_options = $this->get_form_pdfs( $form_id ); 
  327.  
  328. if ( ! is_wp_error( $gfpdf_options ) ) { 
  329.  
  330. /** Get our PDF array if it exists */ 
  331. $pdf = ! empty( $gfpdf_options[ $pdf_id ] ) ? $gfpdf_options[ $pdf_id ] : new WP_Error( 'invalid_pdf_id', esc_html__( 'You must pass in a valid PDF ID', 'gravity-forms-pdf-extended' ) ); 
  332.  
  333. if ( ! is_wp_error( $pdf ) ) { 
  334. /** See https://gravitypdf.com/documentation/v4/gfpdf_pdf_config/ for more details about these filters */ 
  335. $pdf = apply_filters( 'gfpdf_pdf_config', $pdf, $form_id ); 
  336. $pdf = apply_filters( 'gfpdf_pdf_config_' . $form_id, $pdf, $form_id ); 
  337.  
  338. return $pdf; 
  339.  
  340. /** return WP_Error */ 
  341.  
  342. return $pdf; 
  343.  
  344. /** return WP_Error */ 
  345.  
  346. return $gfpdf_options; 
  347.  
  348.  
  349. /** 
  350. * Create a new PDF configuration option for that form 
  351. * @param integer $form_id The form ID 
  352. * @param array $pdf The settings array 
  353. * @return mixed 
  354. * @since 4.0 
  355. */ 
  356. public function add_pdf( $form_id, $pdf = [] ) { 
  357.  
  358. $this->log->addNotice( 'Adding Settings.', [ 
  359. 'form_id' => $form_id,  
  360. 'new_settings' => $pdf,  
  361. ] ); 
  362.  
  363. /** First let's grab the current settings */ 
  364. $options = $this->get_form_pdfs( $form_id ); 
  365.  
  366. if ( ! is_wp_error( $options ) ) { 
  367.  
  368. /** check the ID, if any */ 
  369. $pdf['id'] = ( isset( $pdf['id'] ) ) ? $pdf['id'] : uniqid(); 
  370. $pdf['active'] = ( isset( $pdf['active'] ) ) ? $pdf['active'] : true; 
  371.  
  372. /** See https://gravitypdf.com/documentation/v4/gfpdf_form_add_pdf/ for more details about these filters */ 
  373. $pdf = apply_filters( 'gfpdf_form_add_pdf', $pdf, $form_id ); 
  374. $pdf = apply_filters( 'gfpdf_form_add_pdf_' . $form_id, $pdf, $form_id ); 
  375.  
  376. $results = $this->update_pdf( $form_id, $pdf['id'], $pdf, true, false ); 
  377.  
  378. if ( $results ) { 
  379.  
  380. /** return the ID if successful */ 
  381. $this->log->addNotice( 'Successfuly Added.', [ 
  382. 'pdf' => $pdf,  
  383. ] ); 
  384.  
  385. return $pdf['id']; 
  386.  
  387. $this->log->addError( 'Error Saving.', [ 
  388. 'error' => $results,  
  389. 'pdf' => $pdf,  
  390. ] ); 
  391.  
  392. return false; 
  393.  
  394. /** 
  395. * Update an pdf config 
  396. * Updates a Gravity PDF setting value in both the db and the global variable. 
  397. * Warning: Passing in an empty, false or null string value will remove 
  398. * the key from the gfpdf_options array. 
  399. * @since 4.0 
  400. * @param integer $form_id The Gravity Form ID 
  401. * @param string $pdf_id The PDF Setting ID 
  402. * @param bool|int|string $pdf The PDF settings array 
  403. * @param bool $update_db Whether we should just update the local PDF settings array, or update the DB as well 
  404. * @param bool $filters Whether we should apply the update filters 
  405. * @return bool True if updated, false if not. 
  406. */ 
  407. public function update_pdf( $form_id, $pdf_id, $pdf = '', $update_db = true, $filters = true ) { 
  408.  
  409. $this->log->addNotice( 'Updating Settings.', [ 
  410. 'form_id' => $form_id,  
  411. 'pdf_id' => $pdf_id,  
  412. 'new_settings' => $pdf,  
  413. ] ); 
  414.  
  415. if ( empty( $pdf ) || ! is_array( $pdf ) || sizeof( $pdf ) == 0 ) { 
  416. /** No value was passed in so we will delete the PDF */ 
  417. $remove_option = $this->delete_pdf( $form_id, $pdf_id ); 
  418.  
  419. return $remove_option; 
  420.  
  421. /** First let's grab the current settings */ 
  422. $options = $this->get_form_pdfs( $form_id ); 
  423.  
  424. if ( ! is_wp_error( $options ) ) { 
  425.  
  426. /** Don't run when adding a new PDF */ 
  427. if ( $filters ) { 
  428.  
  429. $this->log->addNotice( 'Trigger Filters.' ); 
  430.  
  431. /** See https://gravitypdf.com/documentation/v4/gfpdf_form_update_pdf/ for more details about these filters */ 
  432. $pdf = apply_filters( 'gfpdf_form_update_pdf', $pdf, $form_id, $pdf_id ); 
  433. $pdf = apply_filters( 'gfpdf_form_update_pdf_' . $form_id, $pdf, $form_id, $pdf_id ); 
  434.  
  435. /** Next let's try to update the value */ 
  436. $options[ $pdf_id ] = $pdf; 
  437.  
  438. /** get the up-to-date form object and merge in the results */ 
  439. $form = $this->gform->get_form( $form_id ); 
  440.  
  441. /** Update our GFPDF settings */ 
  442. $form['gfpdf_form_settings'] = $options; 
  443.  
  444. $did_update = false; 
  445. if ( $update_db ) { 
  446.  
  447. $this->log->addNotice( 'Update Form.', [ 
  448. 'form_id' => $form['id'],  
  449. ] ); 
  450.  
  451. /** Update the database, if able */ 
  452. $did_update = $this->gform->update_form( $form ); 
  453.  
  454. if ( ! $update_db || $did_update !== false ) { 
  455.  
  456. /** If it updated successfully let's update the global variable */ 
  457. $this->log->addNotice( 'Save Local Form Cache.' ); 
  458.  
  459. $this->data->form_settings[ $form_id ] = $options; 
  460.  
  461. /** true if successful, false if failed */ 
  462.  
  463. return $did_update; 
  464.  
  465. return false; 
  466.  
  467. /** 
  468. * Remove an option 
  469. * Removes an Gravity PDF setting value in both the db and the global variable. 
  470. * @since 4.0 
  471. * @param integer $form_id The Gravity Form ID 
  472. * @param string $pdf_id The Gravity Form PDF ID 
  473. * @return bool True if updated, false if not. 
  474. */ 
  475. public function delete_pdf( $form_id, $pdf_id ) { 
  476.  
  477. $this->log->addNotice( 'Deleting Setting.', [ 
  478. 'form_id' => $form_id,  
  479. 'pdf_id' => $pdf_id,  
  480. ] ); 
  481.  
  482. /** First let's grab the current settings */ 
  483. $options = $this->get_form_pdfs( $form_id ); 
  484.  
  485. if ( ! is_wp_error( $options ) ) { 
  486.  
  487. /** Next let's try to update the value */ 
  488. if ( isset( $options[ $pdf_id ] ) ) { 
  489.  
  490. $this->log->addNotice( 'Found Setting. Now deleting...', [ 
  491. 'pdf' => $options[ $pdf_id ],  
  492. ] ); 
  493.  
  494. unset( $options[ $pdf_id ] ); 
  495.  
  496. /** get the form and merge in the results */ 
  497. $form = $this->gform->get_form( $form_id ); 
  498.  
  499. /** Update our GFPDF settings */ 
  500. $form['gfpdf_form_settings'] = $options; 
  501.  
  502. /** update the database, if able */ 
  503. $did_update = $this->gform->update_form( $form ); 
  504.  
  505. /** If it updated, let's update the global variable */ 
  506. if ( $did_update !== false ) { 
  507.  
  508. $this->log->addNotice( 'Setting Deleted.', [ 
  509. 'form_id' => $form_id,  
  510. 'pdf_id' => $pdf_id,  
  511. ] ); 
  512.  
  513. $this->data->form_settings[ $form_id ] = $options; 
  514.  
  515. /** true if successful, false if failed */ 
  516.  
  517. return $did_update; 
  518.  
  519. $this->log->addError( 'PDF Delete Failed.', [ 
  520. 'form_id' => $form_id,  
  521. 'pdf_id' => $pdf_id,  
  522. ] ); 
  523.  
  524. return false; 
  525.  
  526. /** 
  527. * Get a global setting option 
  528. * Looks to see if the specified setting exists, returns default if not 
  529. * @since 4.0 
  530. * @param string $key The options key to get 
  531. * @param bool $default The default option value if the key isn't found 
  532. * @return mixed 
  533. */ 
  534. public function get_option( $key = '', $default = false ) { 
  535.  
  536. $gfpdf_options = $this->settings; 
  537.  
  538. $value = ( ! empty( $gfpdf_options[ $key ] ) ) ? $gfpdf_options[ $key ] : $default; 
  539.  
  540. /** See https://gravitypdf.com/documentation/v4/gfpdf_get_option/ for more details about these filters */ 
  541. $value = apply_filters( 'gfpdf_get_option', $value, $key, $default ); 
  542. $value = apply_filters( 'gfpdf_get_option_' . $key, $value, $key, $default ); 
  543.  
  544. return $value; 
  545.  
  546. /** 
  547. * Update a global setting option 
  548. * Updates a Gravity PDF setting value in both the db and the global variable. 
  549. * Warning: Passing in an empty, false or null string value will remove 
  550. * the key from the gfpdf_options array. 
  551. * @since 4.0 
  552. * @param string $key The Key to update 
  553. * @param string|bool|int $value The value to set the key to 
  554. * @return boolean True if updated, false if not. 
  555. */ 
  556. public function update_option( $key = '', $value = false ) { 
  557.  
  558. if ( empty( $key ) ) { 
  559. $this->log->addError( 'Option Update Error', [ 
  560. 'key' => $key,  
  561. 'value' => $value,  
  562. ] ); 
  563.  
  564. return false; 
  565.  
  566. if ( empty( $value ) ) { 
  567. $remove_option = $this->delete_option( $key ); 
  568.  
  569. return $remove_option; 
  570.  
  571. /** First let's grab the current settings */ 
  572. $options = get_option( 'gfpdf_settings', [] ); 
  573.  
  574. /** See https://gravitypdf.com/documentation/v4/gfpdf_update_option/ for more details about these filters */ 
  575. $value = apply_filters( 'gfpdf_update_option', $value, $key ); 
  576. $value = apply_filters( 'gfpdf_update_option_' . $key, $value, $key ); 
  577.  
  578. /** Disable default sanitization (it shouldn't be triggered through this method) */ 
  579. remove_filter( 'sanitize_option_gfpdf_settings', [ $this, 'settings_sanitize' ] ); 
  580.  
  581. /** Next let's try to update the value */ 
  582. $options[ $key ] = $value; 
  583. $did_update = update_option( 'gfpdf_settings', $options ); 
  584.  
  585. /** Re-enable sanitization */ 
  586. add_filter( 'sanitize_option_gfpdf_settings', [ $this, 'settings_sanitize' ] ); 
  587.  
  588. /** If it updated, let's update the global variable */ 
  589. if ( $did_update ) { 
  590. $this->settings[ $key ] = $value; 
  591.  
  592. return $did_update; 
  593.  
  594. /** 
  595. * Remove a global setting option 
  596. * Removes an Gravity PDF setting value in both the db and the global variable. 
  597. * @since 4.0 
  598. * @param string $key The Key to delete 
  599. * @return boolean True if updated, false if not. 
  600. */ 
  601. public function delete_option( $key = '' ) { 
  602.  
  603. if ( empty( $key ) ) { 
  604. $this->log->addError( 'Option Delete Error' ); 
  605.  
  606. return false; 
  607.  
  608. // First let's grab the current settings 
  609. $options = get_option( 'gfpdf_settings', [] ); 
  610.  
  611. // Next let's try to update the value 
  612. if ( isset( $options[ $key ] ) ) { 
  613. unset( $options[ $key ] ); 
  614.  
  615. $did_update = update_option( 'gfpdf_settings', $options ); 
  616.  
  617. if ( $did_update ) { 
  618. $this->settings = $options; 
  619.  
  620. return $did_update; 
  621.  
  622. /** 
  623. * Get a list of user capabilities 
  624. * @return array The array of roles available 
  625. * @since 4.0 
  626. */ 
  627. public function get_capabilities() { 
  628.  
  629. /** sort through all roles and fetch unique capabilities */ 
  630. $roles = get_editable_roles(); 
  631. $capabilities = []; 
  632.  
  633. /** Add Gravity Forms Capabilities */ 
  634. $gf_caps = $this->gform->get_capabilities(); 
  635.  
  636. foreach ( $gf_caps as $gf_cap ) { 
  637. $capabilities[ esc_html__( 'Gravity Forms Capabilities', 'gravity-forms-pdf-extended' ) ][ $gf_cap ] = $gf_cap; 
  638.  
  639. foreach ( $roles as $role ) { 
  640. if ( isset( $role['capabilities'] ) && is_array( $role['capabilities'] ) ) { 
  641. foreach ( $role['capabilities'] as $cap => $val ) { 
  642. if ( ! isset( $capabilities[ $cap ] ) && ! in_array( $cap, $gf_caps ) ) { 
  643. $capabilities[ esc_html__( 'Active WordPress Capabilities', 'gravity-forms-pdf-extended' ) ][ $cap ] = $cap; 
  644.  
  645. /** sort alphabetically */ 
  646. foreach ( $capabilities as &$val ) { 
  647. ksort( $val ); 
  648.  
  649. /** See https://gravitypdf.com/documentation/v4/gfpdf_capabilities/ for more details about this filter */ 
  650. return apply_filters( 'gfpdf_capabilities', $capabilities ); 
  651.  
  652. /** 
  653. * Return our paper size 
  654. * @return array The array of paper sizes available 
  655. * @since 4.0 
  656. */ 
  657. public function get_paper_size() { 
  658. return apply_filters( 'gfpdf_get_paper_size', [ 
  659. esc_html__( 'Common Sizes', 'gravity-forms-pdf-extended' ) => [ 
  660. 'A4' => esc_html__( 'A4 (210 x 297mm)', 'gravity-forms-pdf-extended' ),  
  661. 'LETTER' => esc_html__( 'Letter (8.5 x 11in)', 'gravity-forms-pdf-extended' ),  
  662. 'LEGAL' => esc_html__( 'Legal (8.5 x 14in)', 'gravity-forms-pdf-extended' ),  
  663. 'LEDGER' => esc_html__( 'Ledger / Tabloid (11 x 17in)', 'gravity-forms-pdf-extended' ),  
  664. 'EXECUTIVE' => esc_html__( 'Executive (7 x 10in)', 'gravity-forms-pdf-extended' ),  
  665. 'CUSTOM' => esc_html__( 'Custom Paper Size', 'gravity-forms-pdf-extended' ),  
  666. ],  
  667.  
  668. esc_html__( '"A" Sizes', 'gravity-forms-pdf-extended' ) => [ 
  669. 'A0' => esc_html__( 'A0 (841 x 1189mm)', 'gravity-forms-pdf-extended' ),  
  670. 'A1' => esc_html__( 'A1 (594 x 841mm)', 'gravity-forms-pdf-extended' ),  
  671. 'A2' => esc_html__( 'A2 (420 x 594mm)', 'gravity-forms-pdf-extended' ),  
  672. 'A3' => esc_html__( 'A3 (297 x 420mm)', 'gravity-forms-pdf-extended' ),  
  673. 'A5' => esc_html__( 'A5 (210 x 297mm)', 'gravity-forms-pdf-extended' ),  
  674. 'A6' => esc_html__( 'A6 (105 x 148mm)', 'gravity-forms-pdf-extended' ),  
  675. 'A7' => esc_html__( 'A7 (74 x 105mm)', 'gravity-forms-pdf-extended' ),  
  676. 'A8' => esc_html__( 'A8 (52 x 74mm)', 'gravity-forms-pdf-extended' ),  
  677. 'A9' => esc_html__( 'A9 (37 x 52mm)', 'gravity-forms-pdf-extended' ),  
  678. 'A10' => esc_html__( 'A10 (26 x 37mm)', 'gravity-forms-pdf-extended' ),  
  679. ],  
  680.  
  681. esc_html__( '"B" Sizes', 'gravity-forms-pdf-extended' ) => [ 
  682. 'B0' => esc_html__( 'B0 (1414 x 1000mm)', 'gravity-forms-pdf-extended' ),  
  683. 'B1' => esc_html__( 'B1 (1000 x 707mm)', 'gravity-forms-pdf-extended' ),  
  684. 'B2' => esc_html__( 'B2 (707 x 500mm)', 'gravity-forms-pdf-extended' ),  
  685. 'B3' => esc_html__( 'B3 (500 x 353mm)', 'gravity-forms-pdf-extended' ),  
  686. 'B4' => esc_html__( 'B4 (353 x 250mm)', 'gravity-forms-pdf-extended' ),  
  687. 'B5' => esc_html__( 'B5 (250 x 176mm)', 'gravity-forms-pdf-extended' ),  
  688. 'B6' => esc_html__( 'B6 (176 x 125mm)', 'gravity-forms-pdf-extended' ),  
  689. 'B7' => esc_html__( 'B7 (125 x 88mm)', 'gravity-forms-pdf-extended' ),  
  690. 'B8' => esc_html__( 'B8 (88 x 62mm)', 'gravity-forms-pdf-extended' ),  
  691. 'B9' => esc_html__( 'B9 (62 x 44mm)', 'gravity-forms-pdf-extended' ),  
  692. 'B10' => esc_html__( 'B10 (44 x 31mm)', 'gravity-forms-pdf-extended' ),  
  693. ],  
  694.  
  695. esc_html__( '"C" Sizes', 'gravity-forms-pdf-extended' ) => [ 
  696. 'C0' => esc_html__( 'C0 (1297 x 917mm)', 'gravity-forms-pdf-extended' ),  
  697. 'C1' => esc_html__( 'C1 (917 x 648mm)', 'gravity-forms-pdf-extended' ),  
  698. 'C2' => esc_html__( 'C2 (648 x 458mm)', 'gravity-forms-pdf-extended' ),  
  699. 'C3' => esc_html__( 'C3 (458 x 324mm)', 'gravity-forms-pdf-extended' ),  
  700. 'C4' => esc_html__( 'C4 (324 x 229mm)', 'gravity-forms-pdf-extended' ),  
  701. 'C5' => esc_html__( 'C5 (229 x 162mm)', 'gravity-forms-pdf-extended' ),  
  702. 'C6' => esc_html__( 'C6 (162 x 114mm)', 'gravity-forms-pdf-extended' ),  
  703. 'C7' => esc_html__( 'C7 (114 x 81mm)', 'gravity-forms-pdf-extended' ),  
  704. 'C8' => esc_html__( 'C8 (81 x 57mm)', 'gravity-forms-pdf-extended' ),  
  705. 'C9' => esc_html__( 'C9 (57 x 40mm)', 'gravity-forms-pdf-extended' ),  
  706. 'C10' => esc_html__( 'C10 (40 x 28mm)', 'gravity-forms-pdf-extended' ),  
  707. ],  
  708.  
  709. esc_html__( '"RA" and "SRA" Sizes', 'gravity-forms-pdf-extended' ) => [ 
  710. 'RA0' => esc_html__( 'RA0 (860 x 1220mm)', 'gravity-forms-pdf-extended' ),  
  711. 'RA1' => esc_html__( 'RA1 (610 x 860mm)', 'gravity-forms-pdf-extended' ),  
  712. 'RA2' => esc_html__( 'RA2 (430 x 610mm)', 'gravity-forms-pdf-extended' ),  
  713. 'RA3' => esc_html__( 'RA3 (305 x 430mm)', 'gravity-forms-pdf-extended' ),  
  714. 'RA4' => esc_html__( 'RA4 (215 x 305mm)', 'gravity-forms-pdf-extended' ),  
  715. 'SRA0' => esc_html__( 'SRA0 (900 x 1280mm)', 'gravity-forms-pdf-extended' ),  
  716. 'SRA1' => esc_html__( 'SRA1 (640 x 900mm)', 'gravity-forms-pdf-extended' ),  
  717. 'SRA2' => esc_html__( 'SRA2 (450 x 640mm)', 'gravity-forms-pdf-extended' ),  
  718. 'SRA3' => esc_html__( 'SRA3 (320 x 450mm)', 'gravity-forms-pdf-extended' ),  
  719. 'SRA4' => esc_html__( 'SRA4 (225 x 320mm)', 'gravity-forms-pdf-extended' ),  
  720. ],  
  721. ] ); 
  722.  
  723.  
  724. /** 
  725. * Parse our installed font files 
  726. * @return array The array of fonts 
  727. * @since 4.0 
  728. */ 
  729. public function get_installed_fonts() { 
  730. $fonts = [ 
  731. esc_html__( 'Unicode', 'gravity-forms-pdf-extended' ) => [ 
  732. 'dejavusanscondensed' => 'Dejavu Sans Condensed',  
  733. 'dejavusans' => 'Dejavu Sans',  
  734. 'dejavuserifcondensed' => 'Dejavu Serif Condensed',  
  735. 'dejavuserif' => 'Dejavu Serif',  
  736. 'dejavusansmono' => 'Dejavu Sans Mono',  
  737.  
  738. 'freesans' => 'Free Sans',  
  739. 'freeserif' => 'Free Serif',  
  740. 'freemono' => 'Free Mono',  
  741.  
  742. 'mph2bdamase' => 'MPH 2B Damase',  
  743. ],  
  744.  
  745. esc_html__( 'Indic', 'gravity-forms-pdf-extended' ) => [ 
  746. 'lohitkannada' => 'Lohit Kannada',  
  747. 'pothana2000' => 'Pothana2000',  
  748. ],  
  749.  
  750. esc_html__( 'Arabic', 'gravity-forms-pdf-extended' ) => [ 
  751. 'xbriyaz' => 'XB Riyaz',  
  752. 'lateef' => 'Lateef',  
  753. 'kfgqpcuthmantahanaskh' => 'Bahif Uthman Taha',  
  754. ],  
  755.  
  756. esc_html__( 'Other', 'gravity-forms-pdf-extended' ) => [ 
  757. 'estrangeloedessa' => 'Estrangelo Edessa (Syriac)',  
  758. 'kaputaunicode' => 'Kaputa (Sinhala)',  
  759. 'abyssinicasil' => 'Abyssinica SIL (Ethiopic)',  
  760. 'aboriginalsans' => 'Aboriginal Sans (Cherokee / Canadian)',  
  761. 'jomolhari' => 'Jomolhari (Tibetan)',  
  762. 'sundaneseunicode' => 'Sundanese (Sundanese)',  
  763. 'taiheritagepro' => 'Tai Heritage Pro (Tai Viet)',  
  764. 'aegean' => 'Aegean (Greek)',  
  765. 'quivira' => 'Quivira (Greek)',  
  766. 'eeyekunicode' => 'Eeyek (Meetei Mayek)',  
  767. 'lannaalif' => 'Lanna Alif (Tai Tham)',  
  768. 'daibannasilbook' => 'Dai Banna SIL (New Tai Lue)',  
  769. 'garuda' => 'Garuda (Thai)',  
  770. 'khmeros' => 'Khmer OS (Khmer)',  
  771. 'dhyana' => 'Dhyana (Lao)',  
  772. 'tharlon' => 'TharLon (Myanmar / Burmese)',  
  773. 'padaukbook' => 'Padauk Book (Myanmar / Burmese)',  
  774. 'zawgyi-one' => 'Zawgyi One (Myanmar / Burmese)',  
  775. 'ayar' => 'Ayar Myanmar (Myanmar / Burmese)',  
  776. 'taameydavidclm' => 'Taamey David CLM (Hebrew)',  
  777. ],  
  778. ]; 
  779.  
  780. $fonts = $this->add_custom_fonts( $fonts ); 
  781.  
  782. return apply_filters( 'gfpdf_font_list', $fonts ); 
  783.  
  784. /** 
  785. * If any custom fonts add them to our font list 
  786. * @param array $fonts Current font list 
  787. * @since 4.0 
  788. * @return array The list of custom fonts installed in a preformatted array 
  789. */ 
  790. public function add_custom_fonts( $fonts = [] ) { 
  791.  
  792. $custom_fonts = $this->get_custom_fonts(); 
  793.  
  794. if ( sizeof( $custom_fonts ) > 0 ) { 
  795.  
  796. $user_defined_fonts = []; 
  797.  
  798. /** Loop through our fonts and assign them to a new array in the appropriate format */ 
  799. foreach ( $custom_fonts as $font ) { 
  800. $user_defined_fonts[ $font['shortname'] ] = $font['font_name']; 
  801.  
  802. /** Merge the new fonts at the beginning of the $fonts array */ 
  803. $fonts = $this->misc->array_unshift_assoc( $fonts, esc_html__( 'User-Defined Fonts', 'gravity-forms-pdf-extended' ), $user_defined_fonts ); 
  804.  
  805. return $fonts; 
  806.  
  807. /** 
  808. * Get a list of the custom fonts installed 
  809. * @return array 
  810. * @since 4.0 
  811. */ 
  812. public function get_custom_fonts() { 
  813. $fonts = $this->get_option( 'custom_fonts' ); 
  814.  
  815. if ( is_array( $fonts ) && sizeof( $fonts ) > 0 ) { 
  816. foreach ( $fonts as &$font ) { 
  817. $font['shortname'] = $this->get_font_short_name( $font['font_name'] ); 
  818.  
  819. return $fonts; 
  820.  
  821. return []; 
  822.  
  823. /** 
  824. * Get font shortname we can use an in array 
  825. * @param string $name The font name to convert 
  826. * @return string Shortname of font 
  827. * @since 4.0 
  828. */ 
  829. public function get_font_short_name( $name ) { 
  830. return mb_strtolower( str_replace( ' ', '', $name ), 'UTF-8' ); 
  831.  
  832. /** 
  833. * Get the font's display name from the font key 
  834. * @param string $font_key The font key to search for 
  835. * @return mixed (String / Object) The font display name or WP_Error 
  836. * @since 4.0 
  837. */ 
  838. public function get_font_display_name( $font_key ) { 
  839.  
  840. foreach ( $this->get_installed_fonts() as $groups ) { 
  841. if ( isset( $groups[ $font_key ] ) ) { 
  842. return $groups[ $font_key ]; 
  843.  
  844. return new WP_Error( 'font_not_found', esc_html__( 'Could not find Gravity PDF Font' ) ); 
  845.  
  846. /** 
  847. * Parse our PDF privilages 
  848. * @return array The array of privilages 
  849. * @since 4.0 
  850. */ 
  851. public function get_privilages() { 
  852. $privilages = [ 
  853. 'copy' => esc_html__( 'Copy', 'gravity-forms-pdf-extended' ),  
  854. 'print' => esc_html__( 'Print - Low Resolution', 'gravity-forms-pdf-extended' ),  
  855. 'print-highres' => esc_html__( 'Print - High Resolution', 'gravity-forms-pdf-extended' ),  
  856. 'modify' => esc_html__( 'Modify', 'gravity-forms-pdf-extended' ),  
  857. 'annot-forms' => esc_html__( 'Annotate', 'gravity-forms-pdf-extended' ),  
  858. 'fill-forms' => esc_html__( 'Fill Forms', 'gravity-forms-pdf-extended' ),  
  859. 'extract' => esc_html__( 'Extract', 'gravity-forms-pdf-extended' ),  
  860. 'assemble' => esc_html__( 'Assemble', 'gravity-forms-pdf-extended' ),  
  861. ]; 
  862.  
  863. return apply_filters( 'gfpdf_privilages_list', $privilages ); 
  864.  
  865. /** 
  866. * Increment the PDF Generation Counter 
  867. * To decrease load on the database we'll increment by 10 after a rand() function matches 
  868. * This is less accurate but we only need a rough guesstimation to prompt the user 
  869. * @return void 
  870. * @since 4.0 
  871. */ 
  872. public function increment_pdf_count() { 
  873.  
  874. $rand = rand( 1, 10 ); 
  875.  
  876. if ( 10 === $rand ) { 
  877. $total_pdf_count = (int) $this->get_option( 'pdf_count', 0 ); 
  878. $total_pdf_count += 10; 
  879. $this->update_option( 'pdf_count', $total_pdf_count ); 
  880.  
  881. /** 
  882. * Settings Sanitization 
  883. * Adds a settings error (for the updated message) 
  884. * Run on admin options.php page 
  885. * @since 4.0 
  886. * @param array $input The value inputted in the field 
  887. * @return string $input Sanitizied value 
  888. */ 
  889. public function settings_sanitize( $input = [] ) { 
  890.  
  891. $gfpdf_options = $this->settings; 
  892.  
  893. if ( empty( $_POST['_wp_http_referer'] ) || empty( $_POST['option_page'] ) || $_POST['option_page'] != 'gfpdf_settings' ) { 
  894. return $input; 
  895.  
  896. parse_str( $_POST['_wp_http_referer'], $referrer ); 
  897.  
  898. $all_settings = $this->get_registered_fields(); 
  899. $tab = isset( $referrer['tab'] ) ? $referrer['tab'] : 'general'; 
  900. $settings = ( ! empty( $all_settings[ $tab ] ) && $tab !== 'tools' ) ? $all_settings[ $tab ] : []; 
  901.  
  902. /** 
  903. * Get all setting types 
  904. */ 
  905. $tab_len = strlen( $tab ); 
  906. foreach ( $all_settings as $id => $s ) { 
  907. /** 
  908. * Check if extra item(s) belongs on page but isn't the existing page 
  909. * Note that this requires the section ID share a similar ID to what is referenced in $tab 
  910. */ 
  911. if ( $tab != $id && $tab == substr( $id, 0, $tab_len ) ) { 
  912. $settings = array_merge( $settings, $s ); 
  913.  
  914. $input = $input ? $input : []; 
  915. $input = apply_filters( 'gfpdf_settings_' . $tab . '_sanitize', $input ); 
  916.  
  917. /** 
  918. * Loop through the settings whitelist and add any missing required fields to the $input 
  919. * Prevalant with Select boxes 
  920. */ 
  921. foreach ( $settings as $key => $value ) { 
  922. if ( isset( $value['required'] ) && $value['required'] ) { 
  923. switch ( $value['type'] ) { 
  924. case 'select': 
  925. if ( ! isset( $input[ $key ] ) ) { 
  926. $input[ $key ] = []; 
  927. break; 
  928.  
  929. default: 
  930. if ( ! isset( $input[ $key ] ) ) { 
  931. $input[ $key ] = ''; 
  932. break; 
  933.  
  934. /** Loop through each setting being saved and pass it through a sanitization filter */ 
  935. foreach ( $input as $key => $value ) { 
  936.  
  937. /** Check if the input is apart of our whitelist, otherwise remove */ 
  938. if ( ! isset( $settings[ $key ] ) ) { 
  939. unset( $input[ $key ] ); 
  940. continue; 
  941.  
  942. /** Get the setting type (checkbox, select, etc) */ 
  943. $type = isset( $settings[ $key ]['type'] ) ? $settings[ $key ]['type'] : false; 
  944.  
  945. /** 
  946. * General filter 
  947. * See https://gravitypdf.com/documentation/v4/gfpdf_settings_sanitize/ for more details about this filter 
  948. */ 
  949. $input[ $key ] = apply_filters( 'gfpdf_settings_sanitize', $input[ $key ], $key, $input, $settings[ $key ] ); 
  950.  
  951. if ( $type ) { 
  952. /** 
  953. * Field type specific filter 
  954. * See https://gravitypdf.com/documentation/v4/gfpdf_settings_sanitize/ for more details about this filter 
  955. */ 
  956. $input[ $key ] = apply_filters( 'gfpdf_settings_sanitize_' . $type, $value, $key, $input, $settings[ $key ] ); 
  957.  
  958. /** check for errors */ 
  959. if ( count( get_settings_errors() ) === 0 ) { 
  960. /** Merge our new settings with the existing */ 
  961. $output = array_merge( $gfpdf_options, $input ); 
  962. add_settings_error( 'gfpdf-notices', '', esc_html__( 'Settings updated.', 'gravity-forms-pdf-extended' ), 'updated' ); 
  963. } else { 
  964. /** error is thrown. store the user data in a transient so fields are remembered */ 
  965. set_transient( 'gfpdf_settings_user_data', array_merge( $gfpdf_options, $input ), 30 ); 
  966.  
  967. /** return nothing */ 
  968.  
  969. return []; 
  970.  
  971. return $output; 
  972.  
  973.  
  974. /** 
  975. * Sanitize text / textarea fields 
  976. * @since 4.0 
  977. * @param array $input The field value 
  978. * @return string $input Sanitizied value 
  979. */ 
  980. public function sanitize_trim_field( $input ) { 
  981. return trim( $input ); 
  982.  
  983. /** 
  984. * Sanitize number fields 
  985. * @since 4.0 
  986. * @param array $input The field value 
  987. * @return string $input Sanitizied value 
  988. */ 
  989. public function sanitize_number_field( $input ) { 
  990. return (integer) $input; 
  991.  
  992. /** 
  993. * Converts negative numbers to positive numbers 
  994. * @param array $input The unsanitized paper size 
  995. * @return array The sanitized paper size 
  996. * @since 4.0 
  997. */ 
  998. public function sanitize_paper_size( $input ) { 
  999. if ( is_array( $input ) && sizeof( $input ) == 3 ) { 
  1000. $input[0] = abs( $input[0] ); 
  1001. $input[1] = abs( $input[1] ); 
  1002.  
  1003. return $input; 
  1004.  
  1005. /** 
  1006. * Sanitize all fields depending on type 
  1007. * @since 4.0 
  1008. * @param mixed $value The field's user input value 
  1009. * @param string $key The settings key 
  1010. * @param array $input All user fields 
  1011. * @param array $settings The field settings 
  1012. * @return string $input Sanitizied value 
  1013. */ 
  1014. public function sanitize_all_fields( $value, $key, $input, $settings ) { 
  1015.  
  1016. if ( ! isset( $settings['type'] ) ) { 
  1017. $settings['type'] = ''; 
  1018.  
  1019. switch ( $settings['type'] ) { 
  1020. case 'rich_editor': 
  1021. /** 
  1022. * Don't do any sanitization on input, which was causing problems with merge tags in HTML attributes. 
  1023. * See https://github.com/GravityPDF/gravity-pdf/issues/492 for more details. 
  1024. * @internal Devs should run the field through wp_kses_post() on output to correctly sanitize 
  1025. * @since 4.0.6 
  1026. */ 
  1027. return $value; 
  1028. break; 
  1029.  
  1030. case 'textarea': 
  1031. return wp_kses_post( $value ); 
  1032. break; 
  1033.  
  1034. /** treat as plain text */ 
  1035. default: 
  1036. if ( is_array( $value ) ) { 
  1037. array_walk_recursive( $value, function ( &$item ) { 
  1038. $item = wp_strip_all_tags( $item ); 
  1039. } ); 
  1040.  
  1041. return $value; 
  1042. } else { 
  1043. return wp_strip_all_tags( $value ); 
  1044.  
  1045. break; 
  1046.  
  1047. /** 
  1048. * Sanitize all required fields 
  1049. * @since 4.0 
  1050. * @param mixed $value The field's user input value 
  1051. * @param string $key The settings key 
  1052. * @param array $input All user fields 
  1053. * @param array $settings The field settings 
  1054. * @return string $input Sanitizied value 
  1055. */ 
  1056. public function sanitize_required_field( $value, $key, $input, $settings ) { 
  1057.  
  1058. if ( isset( $settings['required'] ) && $settings['required'] === true ) { 
  1059.  
  1060. switch ( $settings['type'] ) { 
  1061. case 'select': 
  1062. case 'multicheck': 
  1063. $size = count( $value ); 
  1064. if ( empty( $value ) || sizeof( array_filter( $value ) ) !== $size ) { 
  1065. /** throw error */ 
  1066. add_settings_error( 'gfpdf-notices', $key, esc_html__( 'PDF Settings could not be saved. Please enter all required information below.', 'gravity-forms-pdf-extended' ) ); 
  1067. break; 
  1068.  
  1069. case 'paper_size': 
  1070. if ( isset( $input['default_pdf_size'] ) && $input['default_pdf_size'] === 'CUSTOM' ) { 
  1071. if ( sizeof( array_filter( $value ) ) !== 3 ) { 
  1072. /** throw error */ 
  1073. add_settings_error( 'gfpdf-notices', $key, esc_html__( 'PDF Settings could not be saved. Please enter all required information below.', 'gravity-forms-pdf-extended' ) ); 
  1074. break; 
  1075.  
  1076. default: 
  1077. if ( strlen( trim( $value ) ) === 0 ) { 
  1078. /** throw error */ 
  1079. add_settings_error( 'gfpdf-notices', $key, esc_html__( 'PDF Settings could not be saved. Please enter all required information below.', 'gravity-forms-pdf-extended' ) ); 
  1080. break; 
  1081.  
  1082. return $value; 
  1083.  
  1084. /** 
  1085. * Gets the correct option value based on the field type 
  1086. * @param array $args The field articles 
  1087. * @return String The current value for that particular field 
  1088. * @since 4.0 
  1089. */ 
  1090. public function get_form_value( $args = [] ) { 
  1091.  
  1092. /** If callback method called directly (and not through the Settings API) */ 
  1093. if ( isset( $args['value'] ) ) { 
  1094. return $args['value']; 
  1095.  
  1096. /** Get our global Gravity PDF Settings */ 
  1097. $options = $this->settings; 
  1098.  
  1099. /** Get our PDF GF settings (if any) */ 
  1100. $pdf_form_settings = $this->get_form_settings(); 
  1101.  
  1102. if ( ! isset( $args['type'] ) ) { 
  1103. $args['type'] = ''; 
  1104.  
  1105. /** Fix up our conditional logic array so it returns a string value */ 
  1106. if ( $args['id'] == 'conditionalLogic' && isset( $pdf_form_settings['conditionalLogic'] ) ) { 
  1107. $pdf_form_settings['conditionalLogic'] = json_encode( $pdf_form_settings['conditionalLogic'] ); 
  1108.  
  1109. switch ( $args['type'] ) { 
  1110. case 'checkbox': 
  1111.  
  1112. if ( isset( $options[ $args['id'] ] ) ) { 
  1113. return checked( 1, $options[ $args['id'] ], false ); 
  1114.  
  1115. } elseif ( isset( $pdf_form_settings[ $args['id'] ] ) ) { 
  1116. return checked( 1, $pdf_form_settings[ $args['id'] ], false ); 
  1117.  
  1118. } elseif ( $args['std'] === true ) { 
  1119. return checked( 1, 1, false ); 
  1120.  
  1121. break; 
  1122.  
  1123. case 'multicheck': 
  1124.  
  1125. if ( isset( $options[ $args['id'] ][ $args['multi-key'] ] ) ) { 
  1126. return $args['multi-option']; 
  1127.  
  1128. } elseif ( isset( $pdf_form_settings[ $args['id'] ][ $args['multi-key'] ] ) ) { 
  1129. return $args['multi-option']; 
  1130.  
  1131. break; 
  1132.  
  1133. case 'radio': 
  1134.  
  1135. if ( isset( $options[ $args['id'] ] ) && isset( $args['options'][ $options[ $args['id'] ] ] ) ) { 
  1136. return $options[ $args['id'] ]; 
  1137.  
  1138. } elseif ( isset( $pdf_form_settings[ $args['id'] ] ) && isset( $args['options'][ $pdf_form_settings[ $args['id'] ] ] ) ) { 
  1139. return $pdf_form_settings[ $args['id'] ]; 
  1140.  
  1141. } elseif ( isset( $args['std'] ) && isset( $args['std'] ) ) { 
  1142. return $args['std']; 
  1143.  
  1144. break; 
  1145.  
  1146. case 'password': 
  1147.  
  1148. if ( isset( $options[ $args['id'] ] ) ) { 
  1149. return trim( $options[ $args['id'] ] ); 
  1150.  
  1151. } elseif ( isset( $pdf_form_settings[ $args['id'] ] ) ) { 
  1152. return trim( $pdf_form_settings[ $args['id'] ] ); 
  1153.  
  1154. break; 
  1155.  
  1156. case 'select': 
  1157. case 'paper_size': 
  1158. if ( isset( $options[ $args['id'] ] ) ) { 
  1159. return $options[ $args['id'] ]; 
  1160.  
  1161. } elseif ( isset( $pdf_form_settings[ $args['id'] ] ) ) { 
  1162. return $pdf_form_settings[ $args['id'] ]; 
  1163.  
  1164. } elseif ( isset( $args['std'] ) ) { 
  1165. return $args['std']; 
  1166. break; 
  1167.  
  1168. /** treat as a text or hidden callback */ 
  1169. default: 
  1170. if ( isset( $options[ $args['id'] ] ) ) { 
  1171. return trim( $options[ $args['id'] ] ); 
  1172.  
  1173. } elseif ( isset( $pdf_form_settings[ $args['id'] ] ) ) { 
  1174. return trim( $pdf_form_settings[ $args['id'] ] ); 
  1175.  
  1176. } elseif ( isset( $args['std'] ) ) { 
  1177. return $args['std']; 
  1178. break; 
  1179.  
  1180. /** if we made it here return empty string */ 
  1181.  
  1182. return ''; 
  1183.  
  1184. /** 
  1185. * Checkbox Callback 
  1186. * Renders checkboxes. 
  1187. * @since 4.0 
  1188. * @param array $args Arguments passed by the setting 
  1189. * @return void 
  1190. */ 
  1191. public function checkbox_callback( $args ) { 
  1192.  
  1193. /** get our selected value */ 
  1194. $checked = $this->get_form_value( $args ); 
  1195. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1196. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1197. $id = ( isset( $args['idOverride'] ) ) ? esc_attr( $args['idOverride'] ) : 'gfpdf_settings[' . esc_attr( $args['id'] ) . ']'; 
  1198.  
  1199. $html = '<input type="checkbox" id="' . $id . '" class="gfpdf_settings_' . $args['id'] . ' ' . $class . '" name="gfpdf_settings[' . $args['id'] . ']" value="1" ' . $checked . ' ' . $required . ' />'; 
  1200. $html .= '<label for="' . $id . '"> ' . wp_kses_post( $args['desc'] ) . '</label>'; 
  1201.  
  1202. if ( isset( $args['tooltip'] ) ) { 
  1203. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1204.  
  1205. echo $html; 
  1206.  
  1207. /** 
  1208. * Multicheck Callback 
  1209. * Renders multiple checkboxes. 
  1210. * @since 4.0 
  1211. * @param array $args Arguments passed by the setting 
  1212. * @return void 
  1213. */ 
  1214. public function multicheck_callback( $args ) { 
  1215.  
  1216. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1217. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1218. $args['id'] = esc_attr( $args['id'] ); 
  1219.  
  1220. if ( ! empty( $args['options'] ) ) { 
  1221. foreach ( $args['options'] as $key => $option ) { 
  1222.  
  1223. /** Set up multi-select option to pass to our form value getter */ 
  1224. $args['multi-key'] = esc_attr( $key ); 
  1225. $args['multi-option'] = $option; 
  1226.  
  1227. $enabled = $this->get_form_value( $args ); 
  1228.  
  1229. echo '<input name="gfpdf_settings[' . $args['id'] . '][' . esc_attr( $key ) . ']" id="gfpdf_settings[' . $args['id'] . '][' . esc_attr( $key ) . ']" class="gfpdf_settings_' . $args['id'] . ' ' . $class . '" type="checkbox" value="' . $option . '" ' . checked( $option, $enabled, false ) . ' ' . $required . ' /> '; 
  1230. echo '<label for="gfpdf_settings[' . $args['id'] . '][' . esc_attr( $key ) . ']">' . $option . '</label><br />'; 
  1231.  
  1232. echo '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1233.  
  1234. if ( isset( $args['tooltip'] ) ) { 
  1235. echo '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1236.  
  1237. /** 
  1238. * Radio Callback 
  1239. * Renders radio boxes. 
  1240. * @since 4.0 
  1241. * @param array $args Arguments passed by the setting 
  1242. * @return void 
  1243. */ 
  1244. public function radio_callback( $args ) { 
  1245.  
  1246. /** get selected value (if any) */ 
  1247. $selected = $this->get_form_value( $args ); 
  1248. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1249. $args['id'] = esc_attr( $args['id'] ); 
  1250. $html = ''; 
  1251.  
  1252. foreach ( $args['options'] as $key => $option ) { 
  1253.  
  1254. $checked = false; 
  1255. if ( $selected == $key ) { 
  1256. $checked = true; 
  1257.  
  1258. $html .= '<label for="gfpdf_settings[' . $args['id'] . '][' . esc_attr( $key ) . ']"><input name="gfpdf_settings[' . $args['id'] . ']" class="gfpdf_settings_' . $args['id'] . '" id="gfpdf_settings[' . $args['id'] . '][' . esc_attr( $key ) . ']" type="radio" value="' . esc_attr( $key ) . '" ' . checked( true, $checked, false ) . ' ' . $required . ' />'; 
  1259. $html .= $option . '</label>   '; 
  1260.  
  1261. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1262.  
  1263. if ( isset( $args['tooltip'] ) ) { 
  1264. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1265.  
  1266. echo $html; 
  1267.  
  1268. /** 
  1269. * Text Callback 
  1270. * Renders text fields. 
  1271. * @since 4.0 
  1272. * @param array $args Arguments passed by the setting 
  1273. * @return void 
  1274. */ 
  1275. public function text_callback( $args ) { 
  1276.  
  1277. /** get selected value (if any) */ 
  1278. $value = $this->get_form_value( $args ); 
  1279. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1280. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1281. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1282. $args['id'] = esc_attr( $args['id'] ); 
  1283.  
  1284. $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? esc_attr( $args['size'] ) : 'regular'; 
  1285. $html = '<input type="text" class="' . $size . '-text ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" class="gfpdf_settings_' . $args['id'] . '" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" ' . $required; 
  1286.  
  1287. foreach ( $input_data as $data_id => $data_value ) { 
  1288. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1289.  
  1290. $html .= ' />'; 
  1291. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1292.  
  1293. if ( isset( $args['tooltip'] ) ) { 
  1294. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1295.  
  1296. echo $html; 
  1297.  
  1298. /** 
  1299. * Number Callback 
  1300. * Renders number fields. 
  1301. * @since 4.0 
  1302. * @param array $args Arguments passed by the setting 
  1303. * @return void 
  1304. */ 
  1305. public function number_callback( $args ) { 
  1306.  
  1307. /** get selected value (if any) */ 
  1308. $value = $this->get_form_value( $args ); 
  1309.  
  1310. /** ensure value is not an array */ 
  1311. if ( is_array( $value ) ) { 
  1312. $value = implode( ' ', $value ); 
  1313.  
  1314. /** check if required */ 
  1315. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1316. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1317. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1318. $args['id'] = esc_attr( $args['id'] ); 
  1319.  
  1320. $max = isset( $args['max'] ) ? (int) $args['max'] : 999999; 
  1321. $min = isset( $args['min'] ) ? (int) $args['min'] : 0; 
  1322. $step = isset( $args['step'] ) ? (int) $args['step'] : 1; 
  1323.  
  1324. $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? esc_attr( $args['size'] ) : 'regular'; 
  1325. $html = '<input type="number" step="' . esc_attr( $step ) . '" max="' . esc_attr( $max ) . '" min="' . esc_attr( $min ) . '" class="' . $size . '-text gfpdf_settings_' . $args['id'] . ' ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" ' . $required; 
  1326.  
  1327. foreach ( $input_data as $data_id => $data_value ) { 
  1328. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1329.  
  1330. $html .= ' /> ' . $args['desc2']; 
  1331. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1332.  
  1333. if ( isset( $args['tooltip'] ) ) { 
  1334. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1335.  
  1336. echo $html; 
  1337.  
  1338. /** 
  1339. * Textarea Callback 
  1340. * Renders textarea fields. 
  1341. * @since 4.0 
  1342. * @param array $args Arguments passed by the setting 
  1343. * @return void 
  1344. */ 
  1345. public function textarea_callback( $args ) { 
  1346.  
  1347. /** get selected value (if any) */ 
  1348. $value = $this->get_form_value( $args ); 
  1349. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1350. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1351. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1352. $args['id'] = esc_attr( $args['id'] ); 
  1353.  
  1354. $html = '<textarea cols="50" rows="5" id="gfpdf_settings[' . $args['id'] . ']" class="large-text gfpdf_settings_' . $args['id'] . ' ' . $class . '" name="gfpdf_settings[' . $args['id'] . ']" ' . $required; 
  1355.  
  1356. foreach ( $input_data as $data_id => $data_value ) { 
  1357. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1358.  
  1359. $html .= '>' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; 
  1360. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1361.  
  1362. if ( isset( $args['tooltip'] ) ) { 
  1363. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1364.  
  1365. /** Check if the field should include a toggle option */ 
  1366. $toggle = ( ! empty( $args['toggle'] ) ) ? $args['toggle'] : false; 
  1367.  
  1368. if ( $toggle !== false ) { 
  1369. $html = $this->create_toggle_input( $toggle, $html, $value ); 
  1370.  
  1371. echo $html; 
  1372.  
  1373. /** 
  1374. * Password Callback 
  1375. * Renders password fields. 
  1376. * @since 4.0 
  1377. * @param array $args Arguments passed by the setting 
  1378. * @return void 
  1379. */ 
  1380. public function password_callback( $args ) { 
  1381.  
  1382. /** get selected value (if any) */ 
  1383. $value = $this->get_form_value( $args ); 
  1384. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1385. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1386. $args['id'] = esc_attr( $args['id'] ); 
  1387.  
  1388. $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? esc_attr( $args['size'] ) : 'regular'; 
  1389. $html = '<input type="password" class="' . $size . '-text ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" class="gfpdf_settings_' . $args['id'] . '" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( $value ) . '" ' . $required . ' />'; 
  1390. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1391.  
  1392. if ( isset( $args['tooltip'] ) ) { 
  1393. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1394.  
  1395. echo $html; 
  1396.  
  1397. /** 
  1398. * Select Callback 
  1399. * Renders select fields. 
  1400. * @since 4.0 
  1401. * @param array $args Arguments passed by the setting 
  1402. * @return void 
  1403. */ 
  1404. public function select_callback( $args ) { 
  1405.  
  1406. /** get selected value (if any) */ 
  1407. $value = $this->get_form_value( $args ); 
  1408. $placeholder = ( isset( $args['placeholder'] ) ) ? esc_attr( $args['placeholder'] ) : ''; 
  1409. $chosen = ( isset( $args['chosen'] ) ) ? 'gfpdf-chosen' : ''; 
  1410. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1411. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1412. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1413. $args['id'] = esc_attr( $args['id'] ); 
  1414.  
  1415. $multiple = $multipleExt = ''; 
  1416. if ( isset( $args['multiple'] ) ) { 
  1417. $multiple = 'multiple'; 
  1418. $multipleExt = '[]'; 
  1419.  
  1420. $html = '<select id="gfpdf_settings[' . $args['id'] . ']" class="gfpdf_settings_' . $args['id'] . ' ' . $class . ' ' . $chosen . '" name="gfpdf_settings[' . $args['id'] . ']' . $multipleExt . '" data-placeholder="' . $placeholder . '" ' . $multiple . ' ' . $required; 
  1421.  
  1422. foreach ( $input_data as $data_id => $data_value ) { 
  1423. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1424.  
  1425. $html .= '>'; 
  1426. $html .= $this->build_options_for_select( $args['options'], $value ); 
  1427. $html .= '</select>'; 
  1428. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1429.  
  1430. if ( isset( $args['tooltip'] ) ) { 
  1431. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1432.  
  1433. echo $html; 
  1434.  
  1435. /** 
  1436. * Build our option groups for the select box 
  1437. * @param array $options The list of options that should be displayed 
  1438. * @param array|string $value The selected option 
  1439. * @return string 
  1440. * @since 4.1 
  1441. */ 
  1442. public function build_options_for_select( $options, $value ) { 
  1443. $html = ''; 
  1444. foreach ( $options as $option => $name ) { 
  1445. if ( ! is_array( $name ) ) { 
  1446. if ( is_array( $value ) ) { 
  1447. foreach ( $value as $v ) { 
  1448. $selected = selected( $option, $v, false ); 
  1449. if ( $selected != '' ) { 
  1450. break; 
  1451. } else { 
  1452. $selected = selected( $option, $value, false ); 
  1453.  
  1454. $html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>'; 
  1455. } else { 
  1456. $html .= '<optgroup label="' . esc_attr( $option ) . '">'; 
  1457. foreach ( $name as $op_value => $op_label ) { 
  1458. $selected = ''; 
  1459. if ( is_array( $value ) ) { 
  1460. foreach ( $value as $v ) { 
  1461. $selected = selected( $op_value, $v, false ); 
  1462. if ( $selected != '' ) { 
  1463. break; 
  1464. } else { 
  1465. $selected = selected( $op_value, $value, false ); 
  1466.  
  1467. $html .= '<option value="' . esc_attr( $op_value ) . '" ' . $selected . '>' . esc_html( $op_label ) . '</option>'; 
  1468. $html .= '</optgroup>'; 
  1469.  
  1470. return $html; 
  1471.  
  1472. /** 
  1473. * Rich Editor Callback 
  1474. * Renders rich editor fields. 
  1475. * @since 4.0 
  1476. * @param array $args Arguments passed by the setting 
  1477. * @global float $wp_version The WordPress Version 
  1478. */ 
  1479. public function rich_editor_callback( $args ) { 
  1480. /** get selected value (if any) */ 
  1481. $value = $this->get_form_value( $args ); 
  1482.  
  1483. $rows = isset( $args['size'] ) ? esc_attr( $args['size'] ) : 20; 
  1484. $args['id'] = esc_attr( $args['id'] ); 
  1485. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1486.  
  1487. if ( function_exists( 'wp_editor' ) ) { 
  1488. ob_start(); 
  1489. echo '<span class="mt-gfpdf_settings_' . $args['id'] . '" style="float:right; position:relative; right: 10px; top: 90px;"></span>'; 
  1490. wp_editor( stripslashes( $value ), 'gfpdf_settings_' . $args['id'], apply_filters( 'gfpdf_rich_editor_settings', [ 
  1491. 'textarea_name' => 'gfpdf_settings[' . $args['id'] . ']',  
  1492. 'textarea_rows' => $rows,  
  1493. 'editor_height' => $rows * 10, /** estimate row height at 10px */ 
  1494. 'editor_class' => 'gfpdf_settings_' . $args['id'] . ' ' . $class,  
  1495. 'autop' => false,  
  1496. ] ) ); 
  1497. $html = ob_get_clean(); 
  1498. } else { 
  1499. $html = '<textarea class="large-text" rows="'. $rows . '" class="gfpdf_settings_' . $args['id'] . ' ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" name="gfpdf_settings[' . $args['id'] . ']">' . esc_textarea( stripslashes( $value ) ) . '</textarea>'; 
  1500.  
  1501. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1502.  
  1503. if ( isset( $args['tooltip'] ) ) { 
  1504. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1505.  
  1506. /** Check if the field should include a toggle option */ 
  1507. $toggle = ( ! empty( $args['toggle'] ) ) ? $args['toggle'] : false; 
  1508.  
  1509. if ( $toggle !== false ) { 
  1510. $html = $this->create_toggle_input( $toggle, $html, $value ); 
  1511.  
  1512. echo $html; 
  1513.  
  1514. /** 
  1515. * Upload Callback 
  1516. * Renders upload fields. 
  1517. * @since 4.0 
  1518. * @param array $args Arguments passed by the setting 
  1519. * @return void 
  1520. */ 
  1521. public function upload_callback( $args ) { 
  1522.  
  1523. /** get selected value (if any) */ 
  1524. $value = $this->get_form_value( $args ); 
  1525. $uploader_title = ( isset( $args['uploaderTitle'] ) ) ? esc_attr( $args['uploaderTitle'] ) : esc_attr__( 'Select Media', 'gravity-forms-pdf-extended' ); 
  1526. $uploader_button_text = ( isset( $args['uploaderButtonText'] ) ) ? esc_attr( $args['uploaderButtonText'] ) : esc_attr__( 'Select Media', 'gravity-forms-pdf-extended' ); 
  1527. $button_text = ( isset( $args['buttonText'] ) ) ? esc_attr( $args['buttonText'] ) : esc_attr__( 'Upload File', 'gravity-forms-pdf-extended' ); 
  1528. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1529. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1530. $args['id'] = esc_attr( $args['id'] ); 
  1531. $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? esc_attr( $args['size'] ) : 'regular'; 
  1532.  
  1533. $html = '<input type="text" class="' . $size . '-text gfpdf_settings_' . $args['id'] . ' ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" ' . $required . ' />'; 
  1534. $html .= '<span> <input type="button" class="gfpdf_settings_upload_button button-secondary" value="' . $button_text . '" data-uploader-title="' . $uploader_title . '" data-uploader-button-text="' . $uploader_button_text . '" /></span>'; 
  1535. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1536.  
  1537. if ( isset( $args['tooltip'] ) ) { 
  1538. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1539.  
  1540. echo $html; 
  1541.  
  1542.  
  1543. /** 
  1544. * Color picker Callback 
  1545. * Renders color picker fields. 
  1546. * @since 4.0 
  1547. * @param array $args Arguments passed by the setting 
  1548. * @return void 
  1549. */ 
  1550. public function color_callback( $args ) { 
  1551.  
  1552. /** get selected value (if any) */ 
  1553. $value = $this->get_form_value( $args ); 
  1554. $default = isset( $args['std'] ) ? esc_attr( $args['std'] ) : ''; 
  1555. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1556. $required = ( isset( $args['required'] ) && $args['required'] === true ) ? 'required' : ''; 
  1557. $args['id'] = esc_attr( $args['id'] ); 
  1558.  
  1559. $html = '<input type="text" class="gfpdf-color-picker gfpdf_settings_' . $args['id'] . ' ' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( $value ) . '" data-default-color="' . esc_attr( $default ) . '" ' . $required . ' />'; 
  1560. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . $args['id'] . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1561.  
  1562. if ( isset( $args['tooltip'] ) ) { 
  1563. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1564.  
  1565. echo $html; 
  1566.  
  1567. /** 
  1568. * Add a button callback. 
  1569. * Renders a button onto the settings field. 
  1570. * @since 4.0 
  1571. * @param array $args Arguments passed by the setting 
  1572. * @return void 
  1573. */ 
  1574. public function button_callback( $args ) { 
  1575.  
  1576. $nonce = wp_create_nonce( 'gfpdf_settings[' . $args['id'] . ']' ); 
  1577. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1578. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1579.  
  1580. $html = '<button id="gfpdf_settings[' . $args['id'] . ']" name="gfpdf_settings[' . $args['id'] . '][name]" value="' . $args['id'] . '" class="button gfpdf-button ' . $class . '" type="submit"'; 
  1581.  
  1582. foreach ( $input_data as $data_id => $data_value ) { 
  1583. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1584.  
  1585. $html .= '>' . esc_html( $args['std'] ) . '</button>'; 
  1586. $html .= '<span class="gf_settings_description">' . wp_kses_post( $args['desc'] ) . '</span>'; 
  1587. $html .= '<input type="hidden" name="gfpdf_settings[' . $args['id'] . '][nonce]" value="' . $nonce . '" />'; 
  1588.  
  1589. if ( isset( $args['tooltip'] ) ) { 
  1590. $html .= '<span class="gf_hidden_tooltip" style="display: none;">' . wp_kses_post( $args['tooltip'] ) . '</span>'; 
  1591.  
  1592. echo $html; 
  1593.  
  1594. /** 
  1595. * Gravity Forms Conditional Logic Callback 
  1596. * Renders the GF Conditional logic container 
  1597. * @since 4.0 
  1598. * @param array $args Arguments passed by the setting 
  1599. * @return void 
  1600. */ 
  1601. public function conditional_logic_callback( $args ) { 
  1602. $args['idOverride'] = 'gfpdf_conditional_logic'; 
  1603. $args['type'] = 'checkbox'; 
  1604.  
  1605. $this->checkbox_callback( $args ); 
  1606.  
  1607. $html = '<div id="gfpdf_conditional_logic_container" class="gfpdf_conditional_logic"> 
  1608. <!-- content dynamically created from form_admin.js --> 
  1609. </div>'; 
  1610.  
  1611. echo $html; 
  1612.  
  1613. /** 
  1614. * Render a hidden field 
  1615. * @since 4.0 
  1616. * @param array $args Arguments passed by the setting 
  1617. * @return void 
  1618. */ 
  1619. public function hidden_callback( $args ) { 
  1620.  
  1621. /** get selected value (if any) */ 
  1622. $value = $this->get_form_value( $args ); 
  1623. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1624. $input_data = ( isset( $args['data'] ) && is_array( $args['data'] ) ) ? $args['data'] : []; 
  1625. $args['id'] = esc_attr( $args['id'] ); 
  1626.  
  1627. $html = '<input type="hidden" class="' . $class . '" id="gfpdf_settings[' . $args['id'] . ']" class="gfpdf_settings_' . $args['id'] . '" name="gfpdf_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '"'; 
  1628.  
  1629. foreach ( $input_data as $data_id => $data_value ) { 
  1630. $html .= ' data-' . $data_id . '="' . esc_html( $data_value ) . '" '; 
  1631.  
  1632. $html .= '/>'; 
  1633.  
  1634. echo $html; 
  1635.  
  1636. /** 
  1637. * Render the custom paper size functionality 
  1638. * @since 4.0 
  1639. * @param array $args Arguments passed by the setting 
  1640. * @return void 
  1641. */ 
  1642. public function paper_size_callback( $args ) { 
  1643.  
  1644. /** get selected value (if any) */ 
  1645. $value = $this->get_form_value( $args ); 
  1646.  
  1647. if ( empty( $value ) ) { 
  1648. $value = [ '', '', 'mm' ]; 
  1649.  
  1650. $placeholder = ( isset( $args['placeholder'] ) ) ? esc_attr( $args['placeholder'] ) : ''; 
  1651. $chosen = ( isset( $args['chosen'] ) ) ? 'gfpdf-chosen' : ''; 
  1652. $class = ( isset( $args['inputClass'] ) ) ? esc_attr( $args['inputClass'] ) : ''; 
  1653. $size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? esc_attr( $args['size'] ) : 'regular'; 
  1654.  
  1655. $html = '<input type="number" class="' . $size . '-text gfpdf_settings_' . $args['id'] . '" id="gfpdf_settings[' . $args['id'] . ']_width" min="1" name="gfpdf_settings[' . $args['id'] . '][]" value="' . esc_attr( stripslashes( $value[0] ) ) . '" required /> ' . esc_html__( 'Width', 'gravity-forms-pdf-extended' ); 
  1656. $html .= ' <input type="number" class="' . $size . '-text gfpdf_settings_' . $args['id'] . '" id="gfpdf_settings[' . $args['id'] . ']_height" min="1" name="gfpdf_settings[' . $args['id'] . '][]" value="' . esc_attr( stripslashes( $value[1] ) ) . '" required /> ' . esc_html__( 'Height', 'gravity-forms-pdf-extended' ); 
  1657.  
  1658. $measurement = apply_filters( 'gfpdf_paper_size_dimensions', [ 
  1659. 'millimeters' => esc_html__( 'mm', 'gravity-forms-pdf-extended' ),  
  1660. 'inches' => esc_html__( 'inches', 'gravity-forms-pdf-extended' ),  
  1661. ] ); 
  1662.  
  1663. $html .= '  *   <select id="gfpdf_settings[' . $args['id'] . ']_measurement" style="width: 75px" class="gfpdf_settings_' . $args['id'] . ' ' . $class . ' ' . $chosen . '" name="gfpdf_settings[' . $args['id'] . '][]" data-placeholder="' . $placeholder . '">'; 
  1664.  
  1665. $measure_value = esc_attr( stripslashes( $value[2] ) ); 
  1666. foreach ( $measurement as $key => $val ) { 
  1667. $selected = ( $measure_value === $key ) ? 'selected="selected"' : ''; 
  1668. $html .= '<option value="' . $key . '" ' . $selected . '>' . $val . '</option>'; 
  1669.  
  1670. $html .= '</select> '; 
  1671.  
  1672. $html .= '<span class="gf_settings_description"><label for="gfpdf_settings[' . esc_attr( $args['id'] ) . ']"> ' . wp_kses_post( $args['desc'] ) . '</label></span>'; 
  1673.  
  1674. echo $html; 
  1675.  
  1676. /** 
  1677. * Descriptive text callback. 
  1678. * Renders descriptive text onto the settings field. 
  1679. * @since 4.0 
  1680. * @param array $args Arguments passed by the setting 
  1681. * @return void 
  1682. */ 
  1683. public function descriptive_text_callback( $args ) { 
  1684. echo wp_kses_post( $args['desc'] ); 
  1685.  
  1686. /** 
  1687. * Hook Callback 
  1688. * Adds a do_action() hook in place of the field 
  1689. * @since 4.0 
  1690. * @param array $args Arguments passed by the setting 
  1691. * @return void 
  1692. */ 
  1693. public function hook_callback( $args ) { 
  1694. do_action( 'gfpdf_' . $args['id'], $args ); 
  1695.  
  1696. /** 
  1697. * Missing Callback 
  1698. * If a public function is missing for settings callbacks alert the user. 
  1699. * @since 4.0 
  1700. * @param array $args Arguments passed by the setting 
  1701. * @return void 
  1702. */ 
  1703. public function missing_callback( $args ) { 
  1704. printf( esc_html__( 'The callback used for the %s setting is missing.', 'gravity-forms-pdf-extended' ), "<strong>{$args['id']}</strong>" ); 
  1705.  
  1706. /** 
  1707. * Creates jQuery toggle functionality for the current fiel 
  1708. * @param String $toggle The text to be used in the toggle 
  1709. * @param String $html The field HTML 
  1710. * @param String $value Whether the field currently has a value 
  1711. * @return String The modified HTML 
  1712. */ 
  1713. public function create_toggle_input( $toggle, $html, $value ) { 
  1714.  
  1715. $has_value = ( strlen( $value ) > 0 ) ? 1 : 0; 
  1716. $current_display = ( ! $has_value ) ? 'style="display: none;"' : ''; 
  1717.  
  1718. $toggle_elm = '<label><input class="gfpdf-input-toggle" type="checkbox" value="1" ' . checked( $has_value, 1, false ) . ' /> ' . esc_attr( $toggle ) . '</label>'; 
  1719.  
  1720. $html = '<div class="gfpdf-toggle-wrapper" ' . $current_display . '>' . 
  1721. $html . 
  1722. '</div>'; 
  1723.  
  1724. $html = $toggle_elm . $html; 
  1725.  
  1726. return $html;