GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-field-other-entries.php
Go to the documentation of this file.
1 <?php
2 /**
3  * @file class-gravityview-field-other-entries.php
4  * @package GravityView
5  * @subpackage includes\fields
6  * @since 1.7.2
7  */
8 
9 /**
10  * A field that displays other entries by the entry_creator for the same View in a list format
11  *
12  * @since 1.7.2
13  */
15 
16  var $name = 'other_entries';
17 
18  var $is_searchable = false;
19 
20  var $contexts = array( 'multiple', 'single' );
21 
22  var $group = 'gravityview';
23 
24  var $icon = 'dashicons-admin-page';
25 
26  private $context;
27 
28  public function __construct() {
29  $this->label = esc_html__( 'Other Entries', 'gk-gravityview' );
30  $this->description = esc_html__('Display other entries created by the entry creator.', 'gk-gravityview');
31  parent::__construct();
32  }
33 
34  /**
35  * @inheritDoc
36  * @since 1.7.2
37  */
38  public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
39 
40  if( 'edit' === $context ) {
41  return $field_options;
42  }
43 
44  // No "Link to single entry"; all the items will be links to entries!
45  unset( $field_options['show_as_link'] );
46 
47  $new_options = array();
48 
49  $new_options['link_format'] = array(
50  'type' => 'text',
51  'label' => __( 'Entry link text (required)', 'gk-gravityview' ),
52  'value' => __('Entry #{entry_id}', 'gk-gravityview'),
53  'merge_tags' => 'force',
54  'group' => 'field',
55  );
56 
57  $new_options['after_link'] = array(
58  'type' => 'textarea',
59  'label' => __( 'Text or HTML to display after the link (optional)', 'gk-gravityview' ),
60  'desc' => __('This content will be displayed below each entry link.', 'gk-gravityview'),
61  'value' => '',
62  'merge_tags' => 'force',
63  'class' => 'widefat code',
64  'group' => 'field',
65  );
66 
67  $new_options['page_size'] = array(
68  'type' => 'number',
69  'label' => __( 'Entries to Display', 'gk-gravityview' ),
70  'desc' => __( 'What is the maximum number of entries that should be shown?', 'gk-gravityview' ) . ' ' . sprintf( _x( 'Set to %s for no maximum.', '%s replaced with a formatted 0', 'gk-gravityview' ), '<code>0</code>' ),
71  'value' => '10',
72  'merge_tags' => false,
73  'min' => 0,
74  'group' => 'field',
75  );
76 
77  $new_options['no_entries_hide'] = array(
78  'type' => 'checkbox',
79  'label' => __( 'Hide if no entries', 'gk-gravityview' ),
80  'desc' => __( 'Don\'t display this field if the entry creator has no other entries', 'gk-gravityview' ),
81  'value' => false,
82  'group' => 'visibility',
83  );
84 
85  $new_options['no_entries_text'] = array(
86  'type' => 'text',
87  'label' => __( 'No Entries Text', 'gk-gravityview' ),
88  'desc' => __( 'The text that is shown if the entry creator has no other entries (and "Hide if no entries" is disabled).', 'gk-gravityview' ),
89  'value' => __( 'This user has no other entries.', 'gk-gravityview' ),
90  'class' => 'widefat',
91  'requires' => 'no_entries_hide',
92  'group' => 'visibility',
93  );
94 
95  return $new_options + $field_options;
96  }
97 
98  /**
99  * Retrieve the other entries based on the current View and entry.
100  *
101  * @param \GV\Template_Context $context The context that contains the View and the Entry.
102  *
103  * @return \GV\Entry[] The entries.
104  */
105  public function get_entries( $context ) {
106  add_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ), 10, 3 );
107  add_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ), 10, 3 );
108 
109  // Exclude widiget modifiers altogether
110  global $wp_filter;
111  $filters = array(
112  'gravityview_fe_search_criteria',
113  'gravityview_search_criteria',
114  'gravityview/view/query',
115  );
116  $removed = $remove = array();
117  foreach ( $filters as $filter ) {
118  foreach ( $wp_filter[ $filter ] as $priority => $callbacks ) {
119  foreach ( $callbacks as $id => $callback ) {
120  if ( ! is_array( $callback['function'] ) ) {
121  continue;
122  }
123  if ( $callback['function'][0] instanceof \GV\Widget ) {
124  $remove[] = array( $filter, $priority, $id );
125  }
126  }
127  }
128  }
129 
130  foreach ( $remove as $r ) {
131  list( $filter, $priority, $id ) = $r;
132  $removed[] = array( $filter, $priority, $id, $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
133  unset( $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] );
134  }
135 
136  $this->context = $context;
137 
138  $entries = $context->view->get_entries()->all();
139 
140  foreach ( $removed as $r ) {
141  list( $filter, $priority, $id, $function ) = $r;
142  $wp_filter[ $filter ]->callbacks[ $priority ][ $id ] = $function;
143  }
144 
145  remove_action( 'gravityview/view/query', array( $this, 'gf_query_filter' ) );
146  remove_filter( 'gravityview_fe_search_criteria', array( $this, 'filter_entries' ) );
147 
148  $this->context = null;
149 
150  return $entries;
151  }
152 
153  public function filter_entries( $search_criteria, $form_id = null, $args = array(), $force_search_criteria = false ) {
155 
156  $created_by = $context->entry['created_by'];
157 
158  /** Filter entries by approved and created_by. */
159  $search_criteria['field_filters'][] = array(
160  'key' => 'created_by',
161  'value' => $created_by,
162  'operator' => 'is'
163  );
164 
165  /**
166  * @filter `gravityview/field/other_entries/criteria` Modify the search parameters before the entries are fetched.
167  *
168  * @since 1.11
169  *
170  * @param array $criteria Gravity Forms search criteria array, as used by GVCommon::get_entries()
171  * @param array $view_settings Associative array of settings with plugin defaults used if not set by the View
172  * @param int $form_id The Gravity Forms ID
173  * @since 2.0
174  * @param \GV\Template_Context $gravityview The context
175  */
176  $criteria = apply_filters( 'gravityview/field/other_entries/criteria', $search_criteria, $context->view->settings->as_atts(), $context->view->form->ID, $context );
177 
178  /** Force mode all and filter out our own entry. */
179  $search_criteria['field_filters']['mode'] = 'all';
180  $search_criteria['field_filters'][] = array(
181  'key' => 'id',
182  'value' => $context->entry->ID,
183  'operator' => 'isnot'
184  );
185 
186  $search_criteria['paging']['page_size'] = $context->field->page_size ? : 10;
187 
188  return $search_criteria;
189  }
190 
191  public function gf_query_filter( &$query, $view, $request ) {
192  // @todo One day, we can implement in GF_Query as well...
193  // this would allow us to keep on using nested conditionals and not force 'all'
194  }
195 }
196 
Modify field settings by extending this class.
get_entries( $context)
Retrieve the other entries based on the current View and entry.
$entries
filter_entries( $search_criteria, $form_id=null, $args=array(), $force_search_criteria=false)
$criteria['paging']
Modify the search parameters before the entries are fetched.
scale description p description
A field that displays other entries by the entry_creator for the same View in a list format...
$created_by
if(empty( $created_by)) $form_id
field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id)
if(false !==strpos( $value, '00:00')) $field_id
string $field_id ID of the field being displayed
Definition: time.php:22