GFPDFModelModel_Mergetags

Handles all the PDF Mergetag logic.

Defined (1)

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

/src/model/Model_Mergetags.php  
  1. class Model_Mergetags extends Helper_Abstract_Model { 
  2.  
  3. /** 
  4. * @var \GFPDF\Model\Model_PDF 
  5. * @since 4.1 
  6. */ 
  7. protected $pdf; 
  8.  
  9. /** 
  10. * Holds our Helper_Abstract_Options / Helper_Options_Fields object 
  11. * Makes it easy to access global PDF settings and individual form PDF settings 
  12. * @var \GFPDF\Helper\Helper_Options_Fields 
  13. * @since 4.1 
  14. */ 
  15. protected $options; 
  16.  
  17. /** 
  18. * Holds our log class 
  19. * @var \Monolog\Logger|LoggerInterface 
  20. * @since 4.1 
  21. */ 
  22. protected $log; 
  23.  
  24. /** 
  25. * Holds our Helper_Misc object 
  26. * Makes it easy to access common methods throughout the plugin 
  27. * @var \GFPDF\Helper\Helper_Misc 
  28. * @since 4.1 
  29. */ 
  30. protected $misc; 
  31.  
  32. /** 
  33. * Model_Mergetags constructor. 
  34. * @param Helper_Abstract_Options $options 
  35. * @param \GFPDF\Model\Model_PDF $pdf 
  36. * @param \Monolog\Logger|LoggerInterface $log 
  37. * @since 4.1 
  38. */ 
  39. public function __construct( Helper_Abstract_Options $options, Model_PDF $pdf, LoggerInterface $log, Helper_Misc $misc ) { 
  40.  
  41. /** Assign our internal variables */ 
  42. $this->pdf = $pdf; 
  43. $this->log = $log; 
  44. $this->options = $options; 
  45. $this->misc = $misc; 
  46.  
  47. /** 
  48. * Add our PDF Merge tags to the merge tag selector 
  49. * The PDF Merge tag format is {NAME:pdf:ID} 
  50. * The NAME is purely to help users identify what PDF the merge tag relates to 
  51. * The ID is the PDF PID parameter 
  52. * @param array $tags The current list of custom tags 
  53. * @param int $form_id The Gravity FOrm ID 
  54. * @return array 
  55. * @since 4.1 
  56. */ 
  57. public function add_pdf_mergetags( $tags, $form_id ) { 
  58.  
  59. $pdfs = $this->options->get_form_pdfs( $form_id ); 
  60.  
  61. if ( is_wp_error( $pdfs ) ) { 
  62. return $tags; 
  63.  
  64. /** Loop through the results and add all PDF URLs to the merge tags */ 
  65. foreach ( $pdfs as $id => $pdf ) { 
  66. if ( $pdf['active'] === true ) { 
  67. $tags[] = [ 
  68. 'tag' => sprintf( '{%s:pdf:%s}', $pdf['name'], $id ),  
  69. /** Format "PDF: %s" - we split it up like this so we didn't have to add another translation */ 
  70. 'label' => esc_html__( 'PDF', 'gravity-forms-pdf-extended' ) . 
  71. ': ' . 
  72. esc_html( $pdf['name'] ),  
  73. ]; 
  74.  
  75. return $tags; 
  76.  
  77. /** 
  78. * Replace the Gravity PDF merge tag ({NAME:pdf:ID}) with the associated PDF URL 
  79. * @param string $text The string to convert 
  80. * @param array $form The Gravity Form array 
  81. * @param array $entry The Gravity Forms entry array 
  82. * @param bool $url_encode Whether to encode the URL or not 
  83. * @return string 
  84. * @since 4.1 
  85. */ 
  86. public function process_pdf_mergetags( $text, $form, $entry, $url_encode ) { 
  87.  
  88. /** Check if there are any PDF merge tags to process, otherwise exit early */ 
  89. if ( strpos( $text, ':pdf:' ) === false ) { 
  90. return $text; 
  91.  
  92. /** Match our PDF merge tags */ 
  93. $results = preg_match_all( "/\{(.*?):pdf:(.*?)\}/", $text, $matches, PREG_SET_ORDER ); 
  94.  
  95. /** Verify we have a match */ 
  96. if ( $results ) { 
  97.  
  98. $this->log->addNotice( 'Converting PDF Mergetag', [ 
  99. 'form_id' => $form['id'],  
  100. 'entry_id' => $entry['id'],  
  101.  
  102. 'tags' => $matches,  
  103. 'text' => $text,  
  104. ] ); 
  105.  
  106. foreach ( $matches as $tag ) { 
  107.  
  108. /** Get the PDF configuration */ 
  109. $config = $this->options->get_pdf( $form['id'], $tag[2] ); 
  110.  
  111. /** Strip tag if config not valid, it isn't active or conditional logic is not met */ 
  112. if ( is_wp_error( $config ) 
  113. || $config['active'] !== true 
  114. || ( isset( $config['conditionalLogic'] ) && ! $this->misc->evaluate_conditional_logic( $config['conditionalLogic'], $entry ) ) 
  115. ) { 
  116. $this->log->addError( 'PDF Mergetag is not valid', [ 
  117. 'form_id' => $form['id'],  
  118. 'entry_id' => $entry['id'],  
  119. 'tag' => $tag,  
  120.  
  121. /** include the error, or the actual config array */ 
  122. 'config' => ( is_wp_error( $config ) ) ? $config->get_error_messages() : $config,  
  123. ] ); 
  124.  
  125. /** Remove the tag and the new line if present (prevents any odd spacing issues) */ 
  126. $text = str_replace( [ $tag[0] . '<br>', $tag[0] . '<br />', $tag[0] . "\n", $tag[0] ], '', $text ); 
  127. continue; 
  128.  
  129. /** Everything is valid so get the URL and display */ 
  130. $url = $this->pdf->get_pdf_url( $tag[2], $entry['id'], false, false, $url_encode ); 
  131.  
  132. /** replace the merge tag */ 
  133. $text = str_replace( $tag[0], $url, $text ); 
  134.  
  135. return $text;