GFPDFHelperHelper_Abstract_View

A simple abstract class view can extent to share similar variables.

Defined (1)

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

/src/helper/abstract/Helper_Abstract_View.php  
  1. abstract class Helper_Abstract_View extends Helper_Abstract_Model { 
  2.  
  3. /** 
  4. * Each object should have a view name 
  5. * @var string 
  6. * @since 4.0 
  7. */ 
  8. protected $view_type = null; 
  9.  
  10. /** 
  11. * Enable a private data cache we can set and retrive information from 
  12. * @var array 
  13. * @since 4.0 
  14. */ 
  15. protected $data_cache = []; 
  16.  
  17. /** 
  18. * Automatically define our constructor which will set our data cache 
  19. * @param array $data An array of data to pass to the view 
  20. * @since 4.0 
  21. */ 
  22. public function __construct( $data = [] ) { 
  23. $this->data_cache = $data; 
  24.  
  25. /** 
  26. * Triggered when invoking inaccessible methods in an object context 
  27. * Use it to load in our view 
  28. * @param string $name Template name to load 
  29. * @param array $arguments Pass in additional parameters to the template view if needed 
  30. * @return string 
  31. * @since 4.0 
  32. */ 
  33. final public function __call( $name, $arguments ) { 
  34. /** check if we have any arguments */ 
  35. $vars = $this->data_cache; 
  36. if ( isset( $arguments[0] ) && is_array( $arguments[0] ) ) { 
  37. $vars = array_merge( $arguments[0], $vars ); 
  38.  
  39. /** load the about page view */ 
  40.  
  41. return $this->load( $name, $vars ); 
  42.  
  43. /** 
  44. * Get the full path to the current view 
  45. * @return string The path 
  46. * @since 4.0.1 
  47. */ 
  48. final public function get_view_dir_path() { 
  49. return PDF_PLUGIN_DIR . 'src/view/html/' . $this->view_type . '/'; 
  50.  
  51. /** 
  52. * Load a view file based on the filename and type 
  53. * @param string $filename The filename to load 
  54. * @param array $args Variables to pass to the included file 
  55. * @param boolean $output Whether to automatically display the included file or return it's output as a String 
  56. * @return string|WP_Error The loaded file, or WP_ERROR 
  57. * @since 4.0 
  58. */ 
  59. final protected function load( $filename, $args = [], $output = true ) { 
  60. $path = $this->get_view_dir_path() . $filename . '.php'; 
  61. $args = array_merge( $this->data_cache, $args ); 
  62.  
  63. if ( is_readable( $path ) ) { 
  64.  
  65. if ( $output ) { 
  66.  
  67. /** Include our $gfpdf object automatically */ 
  68. global $gfpdf; 
  69.  
  70. /** for backwards compatibility extract the $args variable */ 
  71. extract( $args, EXTR_SKIP ); /** skip any arguments that would clash - i.e filename, args, output, path, this */ 
  72.  
  73. include $path; 
  74.  
  75. return true; 
  76. } else { 
  77. return $this->buffer( $path, $args ); 
  78.  
  79. return new WP_Error( 'invalid_path', sprintf( esc_html__( 'Cannot find file %s', 'gravity-forms-pdf-extended' ), $filename ) ); 
  80.  
  81. /** 
  82. * Store output of included file in a buffer and return 
  83. * @param string $path File path to include 
  84. * @param array $args Variables to pass to the included file 
  85. * @return string The contents of the included file 
  86. * @since 4.0 
  87. */ 
  88. final private function buffer( $path, $args = [] ) { 
  89. /** for backwards compatibility extract the $args variable */ 
  90. extract( $args, EXTR_SKIP ); /** skip any arguments that would clash - i.e filename, args, output, path, this */ 
  91.  
  92. /** Include our $gfpdf object automatically */ 
  93. global $gfpdf; 
  94.  
  95. ob_start(); 
  96. include $path; 
  97.  
  98. return ob_get_clean();