GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-plugin-hooks-pageviews.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Add Pageviews.io output to GravityView
4  *
5  * @file class-gravityview-plugin-hooks-gravity-flow.php
6  * @package GravityView
7  * @license GPL2+
8  * @author GravityView <[email protected]>
9  * @link https://gravityview.co
10  * @copyright Copyright 2016, Katz Web Services, Inc.
11  *
12  * @since 1.17
13  */
14 
15 /**
16  * @inheritDoc
17  * @since 1.17
18  */
20 
21  /**
22  * @var string Check for the Gravity Flow constant
23  */
24  protected $class_name = 'Pageviews';
25 
26  /**
27  * @var The next entry ID before the shortcode is gonna be output.
28  */
29  public $next_id = false;
30 
31  /**
32  * @var The shortcode attributes.
33  */
34  public $atts = array();
35 
36  /**
37  * Filter the values shown in GravityView frontend
38  *
39  * @since 1.17
40  */
41  function add_hooks() {
42 
43  parent::add_hooks();
44 
45  add_shortcode( 'gv_pageviews', array( $this, 'pageviews') );
46 
47  add_filter( 'gravityview/fields/custom/decode_shortcodes', array( $this, 'inject_entry_id' ), 10, 3 );
48 
49  add_filter( 'template_redirect', array( $this, 'remove_autocontent' ), 11 );
50 
51  add_action( 'pageviews_before_js', array( $this, 'increment_callback' ) );
52  }
53 
54  /**
55  * Remove the autocontent filter on single entries.
56  * We do not need to be outputting the View counter.
57  */
58  public function remove_autocontent( $r ) {
59  if ( gravityview()->request->is_entry() ) {
60  remove_filter( 'the_content', 'Pageviews::compat_the_content' );
61  }
62  return $r;
63  }
64 
65  /**
66  * Maybe set self::$next_id from the context.
67  *
68  * Used as sort of an action via the gravityview/fields/custom/decode_shortcodes filter.
69  */
70  public function inject_entry_id( $r, $content, $context ) {
71  if ( ! empty( $context->entry['id'] ) ) {
72  $this->next_id = $context->entry['id'];
73  } else {
74  $this->next_id = false; // Nothing to look at, move along
75  }
76 
77  return $r;
78  }
79 
80  /**
81  * Output the Pageviews stuffs
82  *
83  * Shortcode: [gv_pageviews]
84  *
85  * Attributes: `id` Overload the Entry ID. Default: {entry_id} for the custom content field
86  * `preload` The preload text. Default: ...
87  *
88  * @since develop
89  *
90  * @param array $atts The shortcode arguments
91  *
92  * @return string The content
93  */
94  public function pageviews( $atts ) {
95  $this->atts = shortcode_atts( array(
96  'preload' => '...',
97  'id' => $this->next_id,
98  ), $atts );
99 
100  if ( ! $this->atts['id'] ) {
101  return; // The ID was not set
102  }
103 
104  add_filter( 'pageviews_placeholder_preload', array( $this, 'preload_callback' ) );
105 
106  $output = Pageviews::placeholder( 'GV' . $this->atts['id'] ); // Prefix the ID to avoid collissions with default post IDs
107 
108  remove_filter( 'pageviews_placeholder_preload', array( $this, 'preload_callback' ) );
109 
110  return $output;
111  }
112 
113  /**
114  * Set the preload text.
115  */
116  public function preload_callback( $preload ) {
117  return $this->atts['preload'];
118  }
119 
120  /**
121  * Set the increment configuration parameter.
122  */
123  public function increment_callback() {
124  if ( $entry = gravityview()->request->is_entry() ) {
125  $increment = 'GV' . $entry['id'];
126  ?>
127  _pv_config.incr = <?php echo json_encode( $increment ); ?>;
128  <?php
129  }
130  }
131 }
132 
increment_callback()
Set the increment configuration parameter.
inject_entry_id( $r, $content, $context)
Maybe set self::$next_id from the context.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
add_hooks()
Filter the values shown in GravityView frontend.
gravityview()
The main GravityView wrapper function.
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
$entry
Definition: notes.php:27
remove_autocontent( $r)
Remove the autocontent filter on single entries.