GPDFAPI

An easy-to-use API developers can use to work with Gravity PDF.

Defined (1)

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

/api.php  
  1. final class GPDFAPI { 
  2.  
  3. /** 
  4. * Returns our public logger class which uses Monolog (a PSR-3 compatible logging interface - https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) 
  5. * Log messages can be added with any of the following: 
  6. * $gfpdf->log->addDebug( $message, [$parameters = array()] ) 
  7. * $gfpdf->log->addInfo( $message, [$parameters = array()] ) 
  8. * $gfpdf->log->addNotice( $message, [$parameters = array()] ) 
  9. * $gfpdf->log->addWarning( $message, [$parameters = array()] ) 
  10. * $gfpdf->log->addError( $message, [$parameters = array()] ) 
  11. * $gfpdf->log->addCritical( $message, [$parameters = array()] ) 
  12. * $gfpdf->log->addAlert( $message, [$parameters = array()] ) 
  13. * $gfpdf->log->addEmergency( $message, [$parameters = array()] ) 
  14. * When in production Gravity PDF will only log to a file when the Gravity Forms Logging plugin is enabled and Gravity PDF is set to "Log errors only" ($log->addError() or higher) or "Log all messages" ($log->addNotice() or higher) 
  15. * See https://gravitypdf.com/documentation/v4/api_get_log_class/ for more information about this method 
  16. * @return \Monolog\Logger 
  17. * @since 4.0 
  18. */ 
  19. public static function get_log_class() { 
  20. global $gfpdf; 
  21.  
  22. return $gfpdf->log; 
  23.  
  24. /** 
  25. * Returns our public notice queue system to make it easy to display errors and messages to the user. 
  26. * Usage: 
  27. * $notices->add_notice( String $message ); 
  28. * $notices->add_error( String $error ); 
  29. * This taps into the 'admin_notices' or 'network_admin_notices' WordPress hooks so you need to add your notices before then. 
  30. * See https://gravitypdf.com/documentation/v4/api_get_notice_class/ for more information about this method 
  31. * @return \GFPDF\Helper\Helper_Notices 
  32. * @since 4.0 
  33. */ 
  34. public static function get_notice_class() { 
  35. global $gfpdf; 
  36.  
  37. return $gfpdf->notices; 
  38.  
  39. /** 
  40. * Returns our public data class which we use to store important global information related to Gravity PDF 
  41. * This uses PHP magic methods __set() and __get() to access and store information. 
  42. * Usage: 
  43. * $data->title; //returns "Gravity PDF" 
  44. * $data->title = 'Gravity PDF 4.0'; //sets $data->title to "Gravity PDF 4.0" 
  45. * Note: Our __get() magic method returns variables by reference 
  46. * See https://gravitypdf.com/documentation/v4/get_data_class/ for more information about this method 
  47. * @return \GFPDF\Helper\Helper_Data 
  48. * @since 4.0 
  49. */ 
  50. public static function get_data_class() { 
  51. global $gfpdf; 
  52.  
  53. return $gfpdf->data; 
  54.  
  55. /** 
  56. * Returns our access layer class for all Gravity PDF Settings (both global and form specific) 
  57. * Note: Most relevant methods have been broken our and are avaiable through the GPDFAPI directly (GPDFAPI::get_pdf, GPDFAPI::get_plugin_settings ect) 
  58. * See https://gravitypdf.com/documentation/v4/api_get_options_class/ for more information about this method 
  59. * @return \GFPDF\Helper\Helper_Options_Fields 
  60. * @since 4.0 
  61. */ 
  62. public static function get_options_class() { 
  63. global $gfpdf; 
  64.  
  65. return $gfpdf->options; 
  66.  
  67. /** 
  68. * Returns our miscellaneous methods (or common methods) used throughout the plugin. 
  69. * Usage: 
  70. * $misc->is_gfpdf_page(); 
  71. * See https://gravitypdf.com/documentation/v4/api_get_misc_class/ for more information about this method 
  72. * @return \GFPDF\Helper\Helper_Misc 
  73. * @since 4.0 
  74. */ 
  75. public static function get_misc_class() { 
  76. global $gfpdf; 
  77.  
  78. return $gfpdf->misc; 
  79.  
  80. /** 
  81. * Returns our templates methods used throughout the plugin. 
  82. * Usage: 
  83. * $templates->get_all_templates(); 
  84. * See @TODO https://gravitypdf.com/documentation/v4/api_get_templates_class/ for more information about this method 
  85. * @return \GFPDF\Helper\Helper_Templates 
  86. * @since 4.1 
  87. */ 
  88. public static function get_templates_class() { 
  89. global $gfpdf; 
  90.  
  91. return $gfpdf->templates; 
  92.  
  93. /** 
  94. * Returns our abstracted Gravity Forms API class we use throughout the plugin 
  95. * While you could just use the GFAPI directly, some methods in this class have been cache-optimised and are specifically tuned for Gravity PDF. 
  96. * Note: not all the methods in the GFAPI are implimented. 
  97. * Usage: 
  98. * $gform->get_form( $form_id ); 
  99. * See https://gravitypdf.com/documentation/v4/api_get_form_class/ for more information about this method 
  100. * @return \GFPDF\Helper\Helper_Form 
  101. * @since 4.0 
  102. */ 
  103. public static function get_form_class() { 
  104. global $gfpdf; 
  105.  
  106. return $gfpdf->gform; 
  107.  
  108. /** 
  109. * Returns the original Model/View/Controller class we initialised in our /src/bootstrap.php file 
  110. * This method acts like a faux singleton provider (but none of our classes are static or singletons themselves - hence the 'faux') as you get the originally initialised class back 
  111. * This is very useful when you want to remove any filters / actions we set in a controller's add_filters() or add_actions() methods 
  112. * You can also use to to easily access any public methods in our classes 
  113. * Note: This method only returns Controller_ / Model_ / View_ classes. Use the other methods above to access our Helper_ classes 
  114. * Usage: 
  115. * $class = GPDFAPI::get_mcv_class( 'Controller_PDF' ); 
  116. * //if we have a class returned 
  117. * if( $class !== false ) { 
  118. * //remove a middleware filter 
  119. * remove_filter( 'gfpdf_pdf_middleware', array( $class, 'middle_active' ), 10 ); 
  120. * } 
  121. * See https://gravitypdf.com/documentation/v4/api_get_mvc_class/ for more information about this method 
  122. * @param string $class_name The name of one of our MVC classes (no namespace) 
  123. * @return object|bool Will return your object if found, otherwise false 
  124. * @since 4.0 
  125. */ 
  126. public static function get_mvc_class( $class_name ) { 
  127. global $gfpdf; 
  128.  
  129. return $gfpdf->singleton->get_class( $class_name ); 
  130.  
  131. /** 
  132. * Returns a new instance of one of our PDF generating code (model or view) 
  133. * @param string $type Type of class to return. Valid options include 'view' or 'model' 
  134. * @return object|WP_Error 
  135. * @since 4.0 
  136. */ 
  137. public static function get_pdf_class( $type = 'view' ) { 
  138.  
  139. if ( $type === 'view' ) { 
  140. return static::get_mvc_class( 'View_PDF' ); 
  141.  
  142. if ( $type === 'model' ) { 
  143. return static::get_mvc_class( 'Model_PDF' ); 
  144.  
  145. return new WP_Error( 'invalid_type', esc_html__( 'The $type parameter is invalid. Only "view" and "model" are accepted', 'gravity-forms-pdf-extended' ) ); 
  146.  
  147. /** 
  148. * Gets a list of current PDFs setup for a particular Gravity Form 
  149. * See https://gravitypdf.com/documentation/v4/api_get_form_pdfs/ for more information about this method 
  150. * @param integer $form_id The Gravity Form ID 
  151. * @return array|WP_Error Array of PDF settings or WP_Error 
  152. * @since 4.0 
  153. */ 
  154. public static function get_form_pdfs( $form_id ) { 
  155. $options = static::get_options_class(); 
  156.  
  157. return $options->get_form_pdfs( $form_id ); 
  158.  
  159. /** 
  160. * Gets a specific Gravity Form PDF configuration 
  161. * See https://gravitypdf.com/documentation/v4/api_get_pdf/ for more information about this method 
  162. * @param integer $form_id The Gravity Form ID 
  163. * @param string $pdf_id The PDF ID 
  164. * @return array|WP_Error Array of PDF settings or WP_Error 
  165. * @since 4.0 
  166. */ 
  167. public static function get_pdf( $form_id, $pdf_id ) { 
  168. $options = static::get_options_class(); 
  169.  
  170. return $options->get_pdf( $form_id, $pdf_id ); 
  171.  
  172. /** 
  173. * Add a new PDF to a Gravity Form 
  174. * See https://gravitypdf.com/documentation/v4/api_add_pdf/ for more information about this method 
  175. * @param integer $form_id The Gravity Form ID 
  176. * @param array $settings The settings for the PDF 
  177. * @return boolean / String The PDF ID on success, false on failure 
  178. * @since 4.0 
  179. */ 
  180. public static function add_pdf( $form_id, $settings = [] ) { 
  181. $options = static::get_options_class(); 
  182.  
  183. return $options->add_pdf( $form_id, $settings ); 
  184.  
  185. /** 
  186. * Updates an existing Gravity Form PDF. Passing an empty $settings array will delete the PDF 
  187. * See https://gravitypdf.com/documentation/v4/api_update_pdf/ for more information about this method 
  188. * @param integer $form_id The Gravity Form ID 
  189. * @param string $pdf_id The PDF ID 
  190. * @param array $settings The settings for the PDF 
  191. * @return boolean True on success, false on failure 
  192. * @since 4.0 
  193. */ 
  194. public static function update_pdf( $form_id, $pdf_id, $settings = [] ) { 
  195. $options = static::get_options_class(); 
  196.  
  197. return $options->update_pdf( $form_id, $pdf_id, $settings ); 
  198.  
  199. /** 
  200. * Deletes a specific Gravity Form PDF configuration 
  201. * See https://gravitypdf.com/documentation/v4/api_delete_pdf/ for more information about this method 
  202. * @param integer $form_id The Gravity Form ID 
  203. * @param string $pdf_id The PDF ID 
  204. * @return boolean True on success, false on failure 
  205. * @since 4.0 
  206. */ 
  207. public static function delete_pdf( $form_id, $pdf_id ) { 
  208. $options = static::get_options_class(); 
  209.  
  210. return $options->delete_pdf( $form_id, $pdf_id ); 
  211.  
  212. /** 
  213. * Retrieve an array of the global Gravity PDF settings (this doesn't include individual form configuration details - see GPDFAPI::get_form_pdfs) 
  214. * See https://gravitypdf.com/documentation/v4/api_get_plugin_settings/ for more information about this method 
  215. * @return array 
  216. * @since 4.0 
  217. */ 
  218. public static function get_plugin_settings() { 
  219. $options = static::get_options_class(); 
  220.  
  221. return $options->get_settings(); 
  222.  
  223. /** 
  224. * Get an option from the global Gravity PDF settings. If it doesn't exist the $default value will be returned 
  225. * See https://gravitypdf.com/documentation/v4/api_get_plugin_option/ for more information about this method 
  226. * @param string $key The Gravity PDF option key 
  227. * @param mixed $default What's returned if the option doesn't exist 
  228. * @return mixed 
  229. * @since 4.0 
  230. */ 
  231. public static function get_plugin_option( $key, $default = '' ) { 
  232. $options = static::get_options_class(); 
  233.  
  234. return $options->get_option( $key, $default ); 
  235.  
  236. /** 
  237. * Add a new Global option to Gravity PDF 
  238. * If option already exists a WP_Error is returned 
  239. * In most cases you'll want to use GPDFAPI::update_plugin_option() instead 
  240. * See https://gravitypdf.com/documentation/v4/api_add_plugin_option/ for more information about this method 
  241. * @param string $key The option key to add 
  242. * @param mixed $value 
  243. * @return boolean|WP_Error 
  244. * @since 4.0 
  245. */ 
  246. public static function add_plugin_option( $key, $value ) { 
  247. $options = static::get_options_class(); 
  248.  
  249. /** Check the option doesn't already exist */ 
  250. if ( null !== $options->get_option( $key, null ) ) { 
  251. return new WP_Error( 'option_exists', esc_html__( 'The option key %s already exists. Use GPDFAPI::update_plugin_option instead', 'gravity-forms-pdf-extended' ) ); 
  252.  
  253. return static::update_plugin_option( $key, $value ); 
  254.  
  255. /** 
  256. * Updates a Gravity PDF global option. Will create option if it doesn't exist. 
  257. * If $value is falsy (determined by empty() ) the option is deleted. 
  258. * See https://gravitypdf.com/documentation/v4/api_update_plugin_option/ for more information about this method 
  259. * @param string $key The option key to update 
  260. * @param mixed $value 
  261. * @return boolean|WP_Error 
  262. * @since 4.0 
  263. */ 
  264. public static function update_plugin_option( $key, $value ) { 
  265. $options = static::get_options_class(); 
  266.  
  267. return $options->update_option( $key, $value ); 
  268.  
  269. /** 
  270. * Delete's a Gravity PDF global option. 
  271. * See https://gravitypdf.com/documentation/v4/api_delete_plugin_option/ for more information about this method 
  272. * @param string $key The option key to delete 
  273. * @return boolean 
  274. * @since 4.0 
  275. */ 
  276. public static function delete_plugin_option( $key ) { 
  277. $options = static::get_options_class(); 
  278.  
  279. return $options->delete_option( $key ); 
  280.  
  281. /** 
  282. * When provided the Gravity Form entry ID and PDF ID, this method will correctly generate the PDF, save it to disk,  
  283. * trigger appropriate actions and return the absolute path to the PDF. 
  284. * See https://gravitypdf.com/documentation/v4/api_create_pdf/ for more information about this method 
  285. * @param integer $entry_id The Gravity Form entry ID 
  286. * @param string $pdf_id The Gravity PDF ID number (the pid number in the URL when viewing a setting in the admin area) 
  287. * @return mixed Return the full path to the PDF, or a WP_Error on failure 
  288. * @since 4.0 
  289. */ 
  290. public static function create_pdf( $entry_id, $pdf_id ) { 
  291.  
  292. $form_class = static::get_form_class(); 
  293.  
  294. /** Get our entry */ 
  295. $entry = $form_class->get_entry( $entry_id ); 
  296.  
  297. if ( is_wp_error( $entry ) ) { 
  298. return new WP_Error( 'invalid_entry', 'Make sure to pass in a valid Gravity Forms Entry ID' ); 
  299.  
  300. /** Get our settings */ 
  301. $setting = static::get_pdf( $entry['form_id'], $pdf_id ); 
  302.  
  303. if ( is_wp_error( $setting ) ) { 
  304. return new WP_Error( 'invalid_pdf_setting', 'Could not located the PDF Settings. Ensure you pass in a valid PDF ID.' ); 
  305.  
  306. $pdf = static::get_mvc_class( 'Model_PDF' ); 
  307.  
  308. return $pdf->generate_and_save_pdf( $entry, $setting ); 
  309.  
  310. /** 
  311. * Generates the current entry's HTML product table 
  312. * See https://gravitypdf.com/documentation/v4/api_product_table/ for more information about this method 
  313. * @param array $entry The Gravity Form entry 
  314. * @param boolean $return Whether to output or return the HTML 
  315. * @return string|void The product table or null 
  316. * @since 4.0 
  317. */ 
  318. public static function product_table( $entry, $return = false ) { 
  319. global $gfpdf; 
  320.  
  321. $products = new GFPDF\Helper\Fields\Field_Products( new GF_Field(), $entry, $gfpdf->gform, $gfpdf->misc ); 
  322.  
  323. if ( ! $products->is_empty() ) { 
  324.  
  325. if ( $return ) { 
  326. return $products->html(); 
  327.  
  328. echo $products->html(); 
  329.  
  330. return null; 
  331.  
  332. /** 
  333. * Generates a likert table 
  334. * See https://gravitypdf.com/documentation/v4/likert_table/ for more information about this method 
  335. * @param array $entry The Gravity Form entry 
  336. * @param integer $field_id The likert field ID 
  337. * @param boolean $return Whether to output or return the HTML 
  338. * @return Mixed The likert table or null 
  339. * @since 4.0 
  340. */ 
  341. public static function likert_table( $entry, $field_id, $return = false ) { 
  342. global $gfpdf; 
  343.  
  344. /** Get our form */ 
  345. $form = $gfpdf->gform->get_form( $entry['form_id'] ); 
  346.  
  347. /** Check for errors */ 
  348. if ( is_wp_error( $form ) ) { 
  349. return null; 
  350.  
  351. /** Find our field ID, if any */ 
  352. foreach ( $form['fields'] as $field ) { 
  353.  
  354. if ( $field->id == $field_id && $field->inputType == 'likert' ) { 
  355.  
  356. /** Output our likert */ 
  357. $likert = new GFPDF\Helper\Fields\Field_Likert( $field, $entry, $gfpdf->gform, $gfpdf->misc ); 
  358.  
  359. if ( $return ) { 
  360. return $likert->html(); 
  361.  
  362. echo $likert->html(); 
  363. break; 
  364.  
  365. return null; 
  366.  
  367. /** 
  368. * Installs a PDF font on the file system 
  369. * See https://gravitypdf.com/documentation/v4/api_add_pdf_font/ for more information about this method 
  370. * @param array $font The font information to add. 
  371. * This array needs to be in the following format: 
  372. * Array ( 
  373. * 'font_name' => 'Lato',  
  374. * 'regular' => '/full/path/to/font/Lato-Regular.ttf',  
  375. * 'italics' => '/full/path/to/font/Lato-Italic.ttf',  
  376. * 'bold' => '/full/path/to/font/Lato-Bold.ttf',  
  377. * 'bolditalics' => '/full/path/to/font/Lato-BoldItalic.ttf',  
  378. * ) 
  379. * Only the 'font_name' and 'regular' keys are required. 
  380. * All fonts should be referenced with the full server path. 
  381. * Currently, only .ttf fonts are supported. 
  382. * The font name can only contain alphanumeric characters, or a space 
  383. * @return bool|WP_Error 
  384. * @since 4.1 
  385. */ 
  386. public static function add_pdf_font( $font ) { 
  387. $settings = GPDFAPI::get_mvc_class( 'Model_Settings' ); 
  388.  
  389. if ( ! isset( $font['font_name'] ) || ! $settings->is_font_name_valid( $font['font_name'] ) ) { 
  390. return new WP_Error( 'invalid_font_name', 'Font name is not valid. Alphanumeric characters and spaces only.' ); 
  391.  
  392. if ( ! $settings->is_font_name_unique( $font['font_name'] ) ) { 
  393. return new WP_Error( 'font_name_not_unique', 'A font with the same name already exists.' ); 
  394.  
  395. $results = $settings->install_fonts( $font ); 
  396.  
  397. if ( isset( $results['errors'] ) ) { 
  398. return new WP_Error( 'font_installation_error', implode( "\n\n", $results['errors'] ) ); 
  399.  
  400. return true; 
  401.  
  402. /** 
  403. * Deletes one of the v4 fonts that is installed 
  404. * See https://gravitypdf.com/documentation/v4/delete_pdf_font/ for more information about this method 
  405. * @param string $font_name The font that should be deleted 
  406. * @return bool|WP_Error 
  407. * @since 4.1 
  408. */ 
  409. public static function delete_pdf_font( $font_name ) { 
  410. $settings = GPDFAPI::get_mvc_class( 'Model_Settings' ); 
  411. $options = GPDFAPI::get_options_class(); 
  412. $misc = GPDFAPI::get_misc_class(); 
  413. $data = GPDFAPI::get_data_class(); 
  414.  
  415. $fonts = $options->get_option( 'custom_fonts' ); 
  416. $font_id = $settings->get_font_id_by_name( $font_name ); 
  417.  
  418. if ( ! isset( $fonts[ $font_id ] ) ) { 
  419. return new WP_Error( 'font_not_installed', 'Font not installed.' ); 
  420.  
  421. /** Remove the font files */ 
  422. if ( ! $settings->remove_font_file( $fonts[ $font_id ] ) ) { 
  423. return new WP_Error( 'font_delete_failure', 'There was a problem deleting the font files.' ); 
  424.  
  425. /** Cleanup our fontdata directory to prevent caching issues with mPDF */ 
  426. $misc->cleanup_dir( $data->template_fontdata_location ); 
  427.  
  428. /** Update the database */ 
  429. unset( $fonts[ $font_id ] ); 
  430. if ( ! $options->update_option( 'custom_fonts', $fonts ) ) { 
  431. return new WP_Error( 'font_delete_db_failure', 'There was a problem deleting the font from the database.' ); 
  432.  
  433. return true;