GFPDFModelModel_Shortcodes

Handles all the PDF Shortcode logic.

Defined (1)

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

/src/model/Model_Shortcodes.php  
  1. class Model_Shortcodes extends Helper_Abstract_Model { 
  2.  
  3. /** 
  4. * Holds the abstracted Gravity Forms API specific to Gravity PDF 
  5. * @var \GFPDF\Helper\Helper_Form 
  6. * @since 4.0 
  7. */ 
  8. protected $gform; 
  9.  
  10. /** 
  11. * Holds our log class 
  12. * @var \Monolog\Logger|LoggerInterface 
  13. * @since 4.0 
  14. */ 
  15. protected $log; 
  16.  
  17. /** 
  18. * Holds our Helper_Abstract_Options / Helper_Options_Fields object 
  19. * Makes it easy to access global PDF settings and individual form PDF settings 
  20. * @var \GFPDF\Helper\Helper_Options_Fields 
  21. * @since 4.0 
  22. */ 
  23. protected $options; 
  24.  
  25. /** 
  26. * Holds our Helper_Misc object 
  27. * Makes it easy to access common methods throughout the plugin 
  28. * @var \GFPDF\Helper\Helper_Misc 
  29. * @since 4.0 
  30. */ 
  31. protected $misc; 
  32.  
  33. /** 
  34. * Setup our class by injecting all our dependancies 
  35. * @param \GFPDF\Helper\Helper_Abstract_Form|\GFPDF\Helper\Helper_Form $gform Our abstracted Gravity Forms helper functions 
  36. * @param \Monolog\Logger|LoggerInterface $log Our logger class 
  37. * @param \GFPDF\Helper\Helper_Abstract_Options|\GFPDF\Helper\Helper_Options_Fields $options Our options class which allows us to access any settings 
  38. * @since 4.0 
  39. */ 
  40. public function __construct( Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Abstract_Options $options, Helper_Misc $misc ) { 
  41.  
  42. /** Assign our internal variables */ 
  43. $this->gform = $gform; 
  44. $this->log = $log; 
  45. $this->options = $options; 
  46. $this->misc = $misc; 
  47.  
  48. /** 
  49. * Generates a direct link to the PDF that should be generated 
  50. * If placed in a confirmation the appropriate entry will be displayed. 
  51. * A user also has the option to pass in an "entry" parameter to define the entry ID 
  52. * @param array $attributes The shortcode attributes specified 
  53. * @return string 
  54. * @since 4.0 
  55. */ 
  56. public function gravitypdf( $attributes ) { 
  57.  
  58. $this->log->addNotice( 'Generating Shortcode' ); 
  59.  
  60. $controller = $this->getController(); 
  61. $has_view_permissions = $this->gform->has_capability( 'gravityforms_view_entries' ); 
  62.  
  63. /** merge in any missing defaults */ 
  64. $attributes = shortcode_atts( [ 
  65. 'id' => '',  
  66. 'text' => 'Download PDF',  
  67. 'type' => 'download',  
  68. 'class' => 'gravitypdf-download-link',  
  69. 'classes' => '',  
  70. 'entry' => '',  
  71. 'print' => '',  
  72. ], $attributes, 'gravitypdf' ); 
  73.  
  74. /** See https://gravitypdf.com/documentation/v4/gfpdf_gravityforms_shortcode_attributes/ for more information about this filter */ 
  75. $attributes = apply_filters( 'gfpdf_gravityforms_shortcode_attributes', $attributes ); 
  76.  
  77. /** Add Shortcake preview support */ 
  78. if ( defined( 'SHORTCODE_UI_DOING_PREVIEW' ) && SHORTCODE_UI_DOING_PREVIEW === true ) { 
  79. $attributes['url'] = '#'; 
  80.  
  81. return $controller->view->display_gravitypdf_shortcode( $attributes ); 
  82.  
  83. /** Check if we have an entry ID, otherwise check the GET and POST data */ 
  84. if ( empty( $attributes['entry'] ) ) { 
  85. if ( isset( $_GET['lid'] ) || isset( $_GET['entry'] ) ) { 
  86. $attributes['entry'] = ( isset( $_GET['lid'] ) ) ? (int) $_GET['lid'] : (int) $_GET['entry']; 
  87. } else { 
  88.  
  89. /** Only display error to users with appropriate permissions */ 
  90. if ( $has_view_permissions ) { 
  91. return $controller->view->no_entry_id(); 
  92.  
  93. return ''; 
  94.  
  95. /** Check if we have a valid PDF configuration */ 
  96. $entry = $this->gform->get_entry( $attributes['entry'] ); 
  97. $config = ( ! is_wp_error( $entry ) ) ? $this->options->get_pdf( $entry['form_id'], $attributes['id'] ) : $entry; /** if invalid entry a WP_Error will be thrown */ 
  98.  
  99. if ( is_wp_error( $config ) ) { 
  100.  
  101. /** Only display error to users with appropriate permissions */ 
  102. if ( $has_view_permissions ) { 
  103. return $controller->view->invalid_pdf_config(); 
  104.  
  105. return ''; 
  106.  
  107. /** Check if the PDF is enabled AND the conditional logic (if any) has been met */ 
  108. if ( $config['active'] !== true ) { 
  109. /** Only display error to users with appropriate permissions */ 
  110. if ( $has_view_permissions ) { 
  111. return $controller->view->pdf_not_active(); 
  112.  
  113. return ''; 
  114.  
  115. if ( isset( $config['conditionalLogic'] ) && ! $this->misc->evaluate_conditional_logic( $config['conditionalLogic'], $entry ) ) { 
  116. /** Only display error to users with appropriate permissions */ 
  117. if ( $has_view_permissions ) { 
  118. return $controller->view->conditional_logic_not_met(); 
  119.  
  120. return ''; 
  121.  
  122. /** Everything looks valid so let's get the URL */ 
  123. $pdf = new Model_PDF( $this->gform, $this->log, $this->options, GPDFAPI::get_data_class(), GPDFAPI::get_misc_class(), GPDFAPI::get_notice_class(), GPDFAPI::get_templates_class() ); 
  124. $download = ( $attributes['type'] == 'download' ) ? true : false; 
  125. $print = ( ! empty( $attributes['print'] ) ) ? true : false; 
  126. $attributes['url'] = $pdf->get_pdf_url( $attributes['id'], $attributes['entry'], $download, $print ); 
  127.  
  128. /** generate the markup and return */ 
  129. $this->log->addNotice( 'Generating Shortcode Markup', [ 
  130. 'attr' => $attributes,  
  131. ] ); 
  132.  
  133. return $controller->view->display_gravitypdf_shortcode( $attributes ); 
  134.  
  135. /** 
  136. * Update our Gravity Forms "Text" Confirmation Shortcode to include the current entry ID 
  137. * @param string $confirmation The confirmation text 
  138. * @param array $form The Gravity Form array 
  139. * @param array $entry The Gravity Form entry information 
  140. * @return array The confirmation text 
  141. * @since 4.0 
  142. */ 
  143. public function gravitypdf_confirmation( $confirmation, $form, $entry ) { 
  144.  
  145. /** check if confirmation is text-based */ 
  146. if ( ! is_array( $confirmation ) ) { 
  147. $confirmation = $this->add_entry_id_to_shortcode( $confirmation, $entry['id'] ); 
  148.  
  149. return $confirmation; 
  150.  
  151. /** 
  152. * Update our Gravity Forms Notification Shortcode to include the current entry ID 
  153. * @param string $notification The confirmation text 
  154. * @param array $form The Gravity Form array 
  155. * @param array $entry The Gravity Form entry information 
  156. * @return array The confirmation text 
  157. * @since 4.0 
  158. */ 
  159. public function gravitypdf_notification( $notification, $form, $entry ) { 
  160.  
  161. /** check if notification has a 'message' */ 
  162. if ( isset( $notification['message'] ) ) { 
  163. $notification['message'] = $this->add_entry_id_to_shortcode( $notification['message'], $entry['id'] ); 
  164.  
  165. return $notification; 
  166.  
  167. /** 
  168. * Add basic GravityView support and parse the Custom Content field for the [gravitypdf] shortcode 
  169. * This means users can copy and paste our sample shortcode without having to worry about an entry ID being passed. 
  170. * @param string $html 
  171. * @return string 
  172. * @since 4.0 
  173. */ 
  174. public function gravitypdf_gravityview_custom( $html ) { 
  175. $gravityview_view = GravityView_View::getInstance(); 
  176. $entry = $gravityview_view->getCurrentEntry(); 
  177.  
  178. return $this->add_entry_id_to_shortcode( $html, $entry['id'] ); 
  179.  
  180. /** 
  181. * Check for the [gravitypdf] shortcode and add the entry ID to it 
  182. * @param $string The text to search 
  183. * @param $entry_id The entry ID to add to our shortcode 
  184. * @return string 
  185. * @since 4.0 
  186. */ 
  187. private function add_entry_id_to_shortcode( $string, $entry_id ) { 
  188. /** Check if our shortcode exists and add the entry ID if needed */ 
  189. $gravitypdf = $this->get_shortcode_information( 'gravitypdf', $string ); 
  190.  
  191. if ( sizeof( $gravitypdf ) > 0 ) { 
  192. foreach ( $gravitypdf as $shortcode ) { 
  193. /** if the user hasn't explicitely defined an entry to display... */ 
  194. if ( ! isset( $shortcode['attr']['entry'] ) ) { 
  195. /** get the new shortcode information */ 
  196. $new_shortcode = $this->add_shortcode_attr( $shortcode, 'entry', $entry_id ); 
  197.  
  198. /** update our confirmation message */ 
  199. $string = str_replace( $shortcode['shortcode'], $new_shortcode['shortcode'], $string ); 
  200.  
  201. return $string; 
  202.  
  203. /** 
  204. * Update a shortcode attributes 
  205. * @param array $code In individual shortcode array pulled in from the $this->get_shortcode_information() function 
  206. * @param string $attr The attribute to add / replace 
  207. * @param string $value The new attribute value 
  208. * @return array 
  209. * @since 4.0 
  210. */ 
  211. public function add_shortcode_attr( $code, $attr, $value ) { 
  212.  
  213. /** if the attribute doesn't already exist... */ 
  214. if ( ! isset( $code['attr'][ $attr ] ) ) { 
  215.  
  216. $raw_attr = "{$code['attr_raw']} {$attr}=\"{$value}\""; 
  217.  
  218. /** if there are no attributes at all we'll need to fix our str replace */ 
  219. if ( 0 === strlen( $code['attr_raw'] ) ) { 
  220. $pattern = '^\[([a-zA-Z]+)'; 
  221. $code['shortcode'] = preg_replace( "/$pattern/s", "[$1 {$attr}=\"{$value}\"", $code['shortcode'] ); 
  222. } else { 
  223. $code['shortcode'] = str_ireplace( $code['attr_raw'], $raw_attr, $code['shortcode'] ); 
  224.  
  225. $code['attr_raw'] = $raw_attr; 
  226.  
  227. } else { /** replace the current attribute */ 
  228. $pattern = $attr . '="(.+?)"'; 
  229. $code['shortcode'] = preg_replace( "/$pattern/si", $attr . '="' . $value . '"', $code['shortcode'] ); 
  230. $code['attr_raw'] = preg_replace( "/$pattern/si", $attr . '="' . $value . '"', $code['attr_raw'] ); 
  231.  
  232. /** Update the actual attribute */ 
  233. $code['attr'][ $attr ] = $value; 
  234.  
  235. return $code; 
  236.  
  237. /** 
  238. * Check if user is currently submitting a new confirmation redirect URL in the admin area,  
  239. * if so replace any shortcodes with a direct link to the PDF (as Gravity Forms correctly validates the URL) 
  240. * @param array $form Gravity Form Array 
  241. * @return array 
  242. * @since 4.0 
  243. */ 
  244. public function gravitypdf_redirect_confirmation( $form ) { 
  245.  
  246. /** check if the confirmation is currently being saved */ 
  247. if ( isset( $_POST['form_confirmation_url'] ) ) { 
  248.  
  249. $this->log->addNotice( 'Process Redirect Confirmation Save', [ 
  250. 'form_id' => $form['id'],  
  251. 'post' => $_POST,  
  252. ] ); 
  253.  
  254. $url = stripslashes_deep( $_POST['form_confirmation_url'] ); 
  255.  
  256. /** check if our shortcode exists and convert it to a URL */ 
  257. $gravitypdf = $this->get_shortcode_information( 'gravitypdf', $url ); 
  258.  
  259. if ( sizeof( $gravitypdf ) > 0 ) { 
  260.  
  261. foreach ( $gravitypdf as $code ) { 
  262.  
  263. /** get the PDF Settings ID */ 
  264. $pid = ( isset( $code['attr']['id'] ) ) ? $code['attr']['id'] : ''; 
  265.  
  266. if ( ! empty( $pid ) ) { 
  267.  
  268. /** generate the PDF URL */ 
  269. $pdf = new Model_PDF( $this->gform, $this->log, $this->options, GPDFAPI::get_data_class(), GPDFAPI::get_misc_class(), GPDFAPI::get_notice_class(), GPDFAPI::get_templates_class() ); 
  270. $download = ( ! isset( $code['attr']['type'] ) || $code['attr']['type'] == 'download' ) ? true : false; 
  271. $pdf_url = $pdf->get_pdf_url( $pid, '{entry_id}', $download, false, false ); 
  272.  
  273. /** override the confirmation URL submitted */ 
  274. $_POST['form_confirmation_url'] = str_replace( $code['shortcode'], $pdf_url, $url ); 
  275.  
  276. /** it's a filter so return the $form array */ 
  277.  
  278. return $form; 
  279.  
  280. /** 
  281. * Search for any shortcodes in the text and return any matches 
  282. * @param string $shortcode The shortcode to search for 
  283. * @param string $text The text to search in 
  284. * @return array The shortcode information 
  285. * @since 4.0 
  286. */ 
  287. public function get_shortcode_information( $shortcode, $text ) { 
  288. $shortcodes = []; 
  289.  
  290. if ( has_shortcode( $text, $shortcode ) ) { 
  291.  
  292. /** our shortcode exists so parse the shortcode data and return an easy-to-use array */ 
  293. $pattern = get_shortcode_regex(); 
  294. preg_match_all( "/$pattern/s", $text, $matches ); 
  295.  
  296. if ( ! empty( $matches ) && isset( $matches[2] ) ) { 
  297. foreach ( $matches[2] as $key => $code ) { 
  298. if ( $code == $shortcode ) { 
  299. $attr = shortcode_parse_atts( $matches[3][ $key ] ); 
  300.  
  301. $shortcodes[] = [ 
  302. 'shortcode' => $matches[0][ $key ],  
  303. 'attr_raw' => $matches[3][ $key ],  
  304. 'attr' => ( is_array( $attr ) ) ? $attr : [],  
  305. ]; 
  306.  
  307. return $shortcodes;