gravityview /
GravityView
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * GravityView Frontend functions |
||
| 4 | * |
||
| 5 | * @package GravityView |
||
| 6 | * @license GPL2+ |
||
| 7 | * @author Katz Web Services, Inc. |
||
| 8 | * @link http://gravityview.co |
||
| 9 | * @copyright Copyright 2014, Katz Web Services, Inc. |
||
| 10 | * |
||
| 11 | * @since 1.0.0 |
||
| 12 | */ |
||
| 13 | |||
| 14 | |||
| 15 | class GravityView_frontend { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Regex strings that are used to determine whether the current request is a GravityView search or not. |
||
| 19 | * @see GravityView_frontend::is_searching() |
||
| 20 | * @since 1.7.4.1 |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private static $search_parameters = array( 'gv_search', 'gv_start', 'gv_end', 'gv_id', 'gv_by', 'filter_*' ); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Is the currently viewed post a `gravityview` post type? |
||
| 27 | * @var boolean |
||
| 28 | */ |
||
| 29 | var $is_gravityview_post_type = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Does the current post have a `[gravityview]` shortcode? |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | var $post_has_shortcode = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The Post ID of the currently viewed post. Not necessarily GV |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | var $post_id = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Are we currently viewing a single entry? |
||
| 45 | * If so, the int value of the entry ID. Otherwise, false. |
||
| 46 | * @var int|boolean |
||
| 47 | */ |
||
| 48 | var $single_entry = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * If we are viewing a single entry, the entry data |
||
| 52 | * @var array|false |
||
| 53 | */ |
||
| 54 | var $entry = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * When displaying the single entry we should always know to which View it belongs (the context is everything!) |
||
| 58 | * @var null |
||
| 59 | */ |
||
| 60 | var $context_view_id = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The View is showing search results |
||
| 64 | * @since 1.5.4 |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | var $is_search = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The view data parsed from the $post |
||
| 71 | * |
||
| 72 | * @see GravityView_View_Data::__construct() |
||
| 73 | * @var GravityView_View_Data |
||
| 74 | */ |
||
| 75 | var $gv_output_data = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var GravityView_frontend |
||
| 79 | */ |
||
| 80 | static $instance; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Class constructor, enforce Singleton pattern |
||
| 84 | */ |
||
| 85 | private function __construct() {} |
||
| 86 | |||
| 87 | 3 | private function initialize() { |
|
| 88 | 3 | add_action( 'wp', array( $this, 'parse_content'), 11 ); |
|
| 89 | 3 | add_filter( 'parse_query', array( $this, 'parse_query_fix_frontpage' ), 10 ); |
|
| 90 | 3 | add_action( 'template_redirect', array( $this, 'set_entry_data'), 1 ); |
|
| 91 | |||
| 92 | // Enqueue scripts and styles after GravityView_Template::register_styles() |
||
| 93 | 3 | add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts_and_styles' ), 20 ); |
|
| 94 | |||
| 95 | // Enqueue and print styles in the footer. Added 1 priorty so stuff gets printed at 10 priority. |
||
| 96 | 3 | add_action( 'wp_print_footer_scripts', array( $this, 'add_scripts_and_styles' ), 1 ); |
|
| 97 | |||
| 98 | 3 | add_filter( 'the_title', array( $this, 'single_entry_title' ), 1, 2 ); |
|
| 99 | 3 | add_filter( 'the_content', array( $this, 'insert_view_in_content' ) ); |
|
| 100 | 3 | add_filter( 'comments_open', array( $this, 'comments_open' ), 10, 2 ); |
|
| 101 | |||
| 102 | 3 | add_action( 'gravityview_after', array( $this, 'context_not_configured_warning' ) ); |
|
| 103 | 3 | } |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Get the one true instantiated self |
||
| 107 | * @return GravityView_frontend |
||
| 108 | */ |
||
| 109 | 3 | public static function getInstance() { |
|
| 110 | |||
| 111 | 3 | if ( empty( self::$instance ) ) { |
|
| 112 | 3 | self::$instance = new self; |
|
| 113 | 3 | self::$instance->initialize(); |
|
| 114 | } |
||
| 115 | |||
| 116 | 3 | return self::$instance; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return GravityView_View_Data |
||
| 121 | */ |
||
| 122 | public function getGvOutputData() { |
||
| 123 | return $this->gv_output_data; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param GravityView_View_Data $gv_output_data |
||
| 128 | */ |
||
| 129 | public function setGvOutputData( $gv_output_data ) { |
||
| 130 | $this->gv_output_data = $gv_output_data; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | public function isSearch() { |
||
| 137 | return $this->is_search; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param boolean $is_search |
||
| 142 | */ |
||
| 143 | public function setIsSearch( $is_search ) { |
||
| 144 | $this->is_search = $is_search; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return bool|int |
||
| 149 | */ |
||
| 150 | 1 | public function getSingleEntry() { |
|
| 151 | 1 | return $this->single_entry; |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the single entry ID and also the entry |
||
| 156 | * @param bool|int|string $single_entry |
||
| 157 | */ |
||
| 158 | public function setSingleEntry( $single_entry ) { |
||
| 159 | |||
| 160 | $this->single_entry = $single_entry; |
||
| 161 | |||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | public function getEntry() { |
||
| 168 | return $this->entry; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Set the current entry |
||
| 173 | * @param array|int $entry Entry array or entry slug or ID |
||
| 174 | */ |
||
| 175 | public function setEntry( $entry ) { |
||
| 176 | |||
| 177 | if ( ! is_array( $entry ) ) { |
||
| 178 | $entry = GVCommon::get_entry( $entry ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $this->entry = $entry; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | public function getPostId() { |
||
| 188 | return $this->post_id; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param int $post_id |
||
| 193 | */ |
||
| 194 | public function setPostId( $post_id ) { |
||
| 195 | $this->post_id = $post_id; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return boolean |
||
| 200 | */ |
||
| 201 | 3 | public function isPostHasShortcode() { |
|
| 202 | 3 | return $this->post_has_shortcode; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param boolean $post_has_shortcode |
||
| 207 | */ |
||
| 208 | public function setPostHasShortcode( $post_has_shortcode ) { |
||
| 209 | $this->post_has_shortcode = $post_has_shortcode; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | 3 | public function isGravityviewPostType() { |
|
| 216 | 3 | return $this->is_gravityview_post_type; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param boolean $is_gravityview_post_type |
||
| 221 | */ |
||
| 222 | public function setIsGravityviewPostType( $is_gravityview_post_type ) { |
||
| 223 | $this->is_gravityview_post_type = $is_gravityview_post_type; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set the context view ID used when page contains multiple embedded views or displaying the single entry view |
||
| 228 | * |
||
| 229 | * |
||
| 230 | * |
||
| 231 | * @param null $view_id |
||
| 232 | */ |
||
| 233 | 2 | public function set_context_view_id( $view_id = null ) { |
|
| 234 | 2 | $multiple_views = defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? gravityview()->views->count() > 1 : ( $this->getGvOutputData() && $this->getGvOutputData()->has_multiple_views() ); |
|
| 235 | |||
| 236 | 2 | if ( ! empty( $view_id ) ) { |
|
| 237 | |||
| 238 | 2 | $this->context_view_id = $view_id; |
|
| 239 | |||
| 240 | 2 | } elseif ( isset( $_GET['gvid'] ) && $multiple_views ) { |
|
| 241 | /** |
||
| 242 | * used on a has_multiple_views context |
||
| 243 | * @see GravityView_API::entry_link |
||
| 244 | */ |
||
| 245 | 1 | $this->context_view_id = $_GET['gvid']; |
|
| 246 | |||
| 247 | 2 | } elseif ( ! $multiple_views ) { |
|
| 248 | 2 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 249 | 2 | $view = gravityview()->views->last(); |
|
| 250 | 2 | $this->context_view_id = $view ? $view->ID : null; |
|
| 251 | } else { |
||
| 252 | /** GravityView_View_Data::get_views is deprecated. */ |
||
| 253 | $array_keys = array_keys( $this->getGvOutputData()->get_views() ); |
||
| 254 | $this->context_view_id = array_pop( $array_keys ); |
||
| 255 | unset( $array_keys ); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | 2 | } |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the the view_id context when page contains multiple embedded views or displaying single entry view |
||
| 263 | * |
||
| 264 | * @since 1.5.4 |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function get_context_view_id() { |
||
| 269 | return $this->context_view_id; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Allow GravityView entry endpoints on the front page of a site |
||
| 274 | * |
||
| 275 | * @link https://core.trac.wordpress.org/ticket/23867 Fixes this core issue |
||
| 276 | * @link https://wordpress.org/plugins/cpt-on-front-page/ Code is based on this |
||
| 277 | * |
||
| 278 | * @since 1.17.3 |
||
| 279 | * |
||
| 280 | * @param WP_Query &$query (passed by reference) |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | 5 | public function parse_query_fix_frontpage( &$query ) { |
|
| 285 | 5 | global $wp_rewrite; |
|
| 286 | |||
| 287 | 5 | $is_front_page = ( $query->is_home || $query->is_page ); |
|
| 288 | 5 | $show_on_front = ( 'page' === get_option('show_on_front') ); |
|
| 289 | 5 | $front_page_id = get_option('page_on_front'); |
|
| 290 | |||
| 291 | 5 | if ( $is_front_page && $show_on_front && $front_page_id ) { |
|
| 292 | |||
| 293 | // Force to be an array, potentially a query string ( entry=16 ) |
||
| 294 | $_query = wp_parse_args( $query->query ); |
||
| 295 | |||
| 296 | // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename. |
||
| 297 | if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) { |
||
| 298 | unset( $_query['pagename'] ); |
||
| 299 | } |
||
| 300 | |||
| 301 | // this is where will break from core wordpress |
||
| 302 | $ignore = array( 'preview', 'page', 'paged', 'cpage' ); |
||
| 303 | $endpoints = rgobj( $wp_rewrite, 'endpoints' ); |
||
| 304 | foreach ( (array) $endpoints as $endpoint ) { |
||
| 305 | $ignore[] = $endpoint[1]; |
||
| 306 | } |
||
| 307 | unset( $endpoints ); |
||
| 308 | |||
| 309 | // Modify the query if: |
||
| 310 | // - We're on the "Page on front" page (which we are), and: |
||
| 311 | // - The query is empty OR |
||
| 312 | // - The query includes keys that are associated with registered endpoints. `entry`, for example. |
||
| 313 | if ( empty( $_query ) || ! array_diff( array_keys( $_query ), $ignore ) ) { |
||
| 314 | |||
| 315 | $qv =& $query->query_vars; |
||
| 316 | |||
| 317 | // Prevent redirect when on the single entry endpoint |
||
| 318 | if( self::is_single_entry() ) { |
||
| 319 | add_filter( 'redirect_canonical', '__return_false' ); |
||
| 320 | } |
||
| 321 | |||
| 322 | $query->is_page = true; |
||
| 323 | $query->is_home = false; |
||
| 324 | $qv['page_id'] = $front_page_id; |
||
| 325 | |||
| 326 | // Correct <!--nextpage--> for page_on_front |
||
| 327 | if ( ! empty( $qv['paged'] ) ) { |
||
| 328 | $qv['page'] = $qv['paged']; |
||
| 329 | unset( $qv['paged'] ); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | // reset the is_singular flag after our updated code above |
||
| 334 | $query->is_singular = $query->is_single || $query->is_page || $query->is_attachment; |
||
| 335 | } |
||
| 336 | 5 | } |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Read the $post and process the View data inside |
||
| 340 | * @param array $wp Passed in the `wp` hook. Not used. |
||
| 341 | * @return void |
||
| 342 | */ |
||
| 343 | public function parse_content( $wp = array() ) { |
||
| 344 | global $post; |
||
| 345 | |||
| 346 | // If in admin and NOT AJAX request, get outta here. |
||
| 347 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) && gravityview()->request->is_admin() ) { |
||
| 348 | return; |
||
| 349 | /** Deprecated in favor of gravityview()->request->is_admin(). */ |
||
| 350 | } else if ( GravityView_Plugin::is_admin() ) { |
||
| 351 | return; |
||
| 352 | } |
||
| 353 | |||
| 354 | // Calculate requested Views |
||
| 355 | $this->setGvOutputData( GravityView_View_Data::getInstance( $post ) ); |
||
| 356 | |||
| 357 | // !important: we need to run this before getting single entry (to kick the advanced filter) |
||
| 358 | $this->set_context_view_id(); |
||
| 359 | |||
| 360 | $this->setIsGravityviewPostType( get_post_type( $post ) === 'gravityview' ); |
||
| 361 | |||
| 362 | $post_id = $this->getPostId() ? $this->getPostId() : (isset( $post ) ? $post->ID : null ); |
||
| 363 | $this->setPostId( $post_id ); |
||
| 364 | $post_has_shortcode = ! empty( $post->post_content ) ? gravityview_has_shortcode_r( $post->post_content, 'gravityview' ) : false; |
||
| 365 | $this->setPostHasShortcode( $this->isGravityviewPostType() ? null : ! empty( $post_has_shortcode ) ); |
||
| 366 | |||
| 367 | // check if the View is showing search results (only for multiple entries View) |
||
| 368 | $this->setIsSearch( $this->is_searching() ); |
||
| 369 | |||
| 370 | unset( $entry, $post_id, $post_has_shortcode ); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the entry |
||
| 375 | */ |
||
| 376 | function set_entry_data() { |
||
| 377 | $entry_id = self::is_single_entry(); |
||
| 378 | $this->setSingleEntry( $entry_id ); |
||
| 379 | $this->setEntry( $entry_id ); |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Checks if the current View is presenting search results |
||
| 384 | * |
||
| 385 | * @since 1.5.4 |
||
| 386 | * |
||
| 387 | * @return boolean True: Yes, it's a search; False: No, not a search. |
||
| 388 | */ |
||
| 389 | function is_searching() { |
||
| 390 | |||
| 391 | // It's a single entry, not search |
||
| 392 | if ( $this->getSingleEntry() ) { |
||
| 393 | return false; |
||
| 394 | } |
||
| 395 | |||
| 396 | $search_method = GravityView_Widget_Search::getInstance()->get_search_method(); |
||
| 397 | |||
| 398 | if( 'post' === $search_method ) { |
||
| 399 | $get = $_POST; |
||
| 400 | } else { |
||
| 401 | $get = $_GET; |
||
| 402 | } |
||
| 403 | |||
| 404 | // No $_GET parameters |
||
| 405 | if ( empty( $get ) || ! is_array( $get ) ) { |
||
| 406 | return false; |
||
| 407 | } |
||
| 408 | |||
| 409 | // Remove empty values |
||
| 410 | $get = array_filter( $get ); |
||
| 411 | |||
| 412 | // If the $_GET parameters are empty, it's no search. |
||
| 413 | if ( empty( $get ) ) { |
||
| 414 | return false; |
||
| 415 | } |
||
| 416 | |||
| 417 | $search_keys = array_keys( $get ); |
||
| 418 | |||
| 419 | $search_match = implode( '|', self::$search_parameters ); |
||
| 420 | |||
| 421 | foreach ( $search_keys as $search_key ) { |
||
| 422 | |||
| 423 | // Analyze the search key $_GET parameter and see if it matches known GV args |
||
| 424 | if ( preg_match( '/(' . $search_match . ')/i', $search_key ) ) { |
||
| 425 | return true; |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | return false; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Filter the title for the single entry view |
||
| 434 | * |
||
| 435 | * @param string $title current title |
||
| 436 | * @param int $passed_post_id Post ID |
||
| 437 | * @return string (modified) title |
||
| 438 | */ |
||
| 439 | 1 | public function single_entry_title( $title, $passed_post_id = null ) { |
|
| 440 | 1 | global $post; |
|
| 441 | |||
| 442 | // If this is the directory view, return. |
||
| 443 | 1 | if ( ! $this->getSingleEntry() ) { |
|
| 444 | return $title; |
||
| 445 | } |
||
| 446 | |||
| 447 | 1 | $entry = $this->getEntry(); |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @filter `gravityview/single/title/out_loop` Apply the Single Entry Title filter outside the WordPress loop? |
||
| 451 | * @param boolean $in_the_loop Whether to apply the filter to the menu title and the meta tag <title> - outside the loop |
||
| 452 | * @param array $entry Current entry |
||
| 453 | */ |
||
| 454 | 1 | $apply_outside_loop = apply_filters( 'gravityview/single/title/out_loop' , in_the_loop(), $entry ); |
|
| 455 | |||
| 456 | 1 | if ( ! $apply_outside_loop ) { |
|
| 457 | return $title; |
||
| 458 | } |
||
| 459 | |||
| 460 | // User reported WooCommerce doesn't pass two args. |
||
| 461 | 1 | if ( empty( $passed_post_id ) ) { |
|
| 462 | return $title; |
||
| 463 | } |
||
| 464 | |||
| 465 | // Don't modify the title for anything other than the current view/post. |
||
| 466 | // This is true for embedded shortcodes and Views. |
||
| 467 | 1 | if ( is_object( $post ) && (int) $post->ID !== (int) $passed_post_id ) { |
|
| 468 | return $title; |
||
| 469 | } |
||
| 470 | |||
| 471 | 1 | $context_view_id = $this->get_context_view_id(); |
|
| 472 | |||
| 473 | 1 | $multiple_views = defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? gravityview()->views->count() > 1 : $this->getGvOutputData()->has_multiple_views(); |
|
| 474 | |||
| 475 | 1 | if ( $multiple_views && ! empty( $context_view_id ) ) { |
|
| 476 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 477 | 1 | $view = gravityview()->views->get( $context_view_id ); |
|
| 478 | 1 | if ( ! $view ) { |
|
| 479 | /** Emulate the weird behavior of \GravityView_View_Data::get_view adding a view which wasn't there to begin with. */ |
||
| 480 | gravityview()->views->add( \GV\View::by_id( $context_view_id ) ); |
||
| 481 | 1 | $view = gravityview()->views->get( $context_view_id ); |
|
| 482 | } |
||
| 483 | } else { |
||
| 484 | /** Deprecated. Use gravityview()->views->get() or gravityview()->request->get() */ |
||
| 485 | 1 | $view_meta = $this->getGvOutputData()->get_view( $context_view_id ); |
|
| 486 | } |
||
| 487 | } else { |
||
| 488 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 489 | 1 | foreach ( gravityview()->views->all() as $_view ) { |
|
| 490 | 1 | if ( intval( $_view->form->ID ) === intval( $entry['form_id'] ) ) { |
|
| 491 | 1 | $view = $_view; |
|
| 492 | 1 | break; |
|
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | /** No matching form sources were found, happens when requesting an entry from a different form . */ |
||
| 497 | 1 | if ( ! isset( $view ) ) |
|
| 498 | 1 | return $title; |
|
| 499 | } else { |
||
| 500 | /** Deprecated. Use gravityview()->views->all() or gravityview()->request->all() */ |
||
| 501 | foreach ( $this->getGvOutputData()->get_views() as $view_id => $view_data ) { |
||
| 502 | if ( intval( $view_data['form_id'] ) === intval( $entry['form_id'] ) ) { |
||
| 503 | $view_meta = $view_data; |
||
| 504 | break; |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 511 | 1 | if ( $title = $view->settings->get( 'single_title' ) ) { |
|
| 512 | 1 | $title = GravityView_API::replace_variables( $title, $view->form->form, $entry ); |
|
| 513 | 1 | $title = do_shortcode( $title ); |
|
| 514 | } |
||
| 515 | } else { |
||
| 516 | /** Deprecated stuff in the future. See the branch above. */ |
||
| 517 | if ( ! empty( $view_meta['atts']['single_title'] ) ) { |
||
| 518 | |||
| 519 | $title = $view_meta['atts']['single_title']; |
||
| 520 | |||
| 521 | // We are allowing HTML in the fields, so no escaping the output |
||
| 522 | $title = GravityView_API::replace_variables( $title, $view_meta['form'], $entry ); |
||
| 523 | |||
| 524 | $title = do_shortcode( $title ); |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 528 | |||
| 529 | 1 | return $title; |
|
| 530 | } |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * In case View post is called directly, insert the view in the post content |
||
| 535 | * |
||
| 536 | * @access public |
||
| 537 | * @static |
||
| 538 | * @param mixed $content |
||
| 539 | * @return string Add the View output into View CPT content |
||
| 540 | */ |
||
| 541 | 1 | public function insert_view_in_content( $content ) { |
|
| 542 | |||
| 543 | // Plugins may run through the content in the header. WP SEO does this for its OpenGraph functionality. |
||
| 544 | 1 | if ( ! did_action( 'loop_start' ) ) { |
|
| 545 | |||
| 546 | do_action( 'gravityview_log_debug', '[insert_view_in_content] Not processing yet: loop_start hasn\'t run yet. Current action:', current_filter() ); |
||
| 547 | |||
| 548 | return $content; |
||
| 549 | } |
||
| 550 | |||
| 551 | // We don't want this filter to run infinite loop on any post content fields |
||
| 552 | 1 | remove_filter( 'the_content', array( $this, 'insert_view_in_content' ) ); |
|
| 553 | |||
| 554 | // Otherwise, this is called on the Views page when in Excerpt mode. |
||
| 555 | 1 | if ( is_admin() ) { |
|
| 556 | return $content; |
||
| 557 | } |
||
| 558 | |||
| 559 | // Only render in the loop. Fixes issues with the_content filter being applied in places like the sidebar |
||
| 560 | 1 | if( ! in_the_loop() ) { |
|
| 561 | return $content; |
||
| 562 | } |
||
| 563 | |||
| 564 | 1 | if ( $this->isGravityviewPostType() ) { |
|
| 565 | |||
| 566 | /** @since 1.7.4 */ |
||
| 567 | 1 | if ( is_preview() && ! gravityview_get_form_id( $this->post_id ) ) { |
|
| 568 | $content .= __( 'When using a preset template, you must save the View before a Preview is available.', 'gravityview' ); |
||
| 569 | } else { |
||
| 570 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 571 | 1 | foreach ( gravityview()->views->all() as $view ) { |
|
| 572 | 1 | $content .= $this->render_view( array( 'id' => $view->ID ) ); |
|
| 573 | } |
||
| 574 | } else { |
||
| 575 | /** The \GravityView_View_Data::get_views method is depreacted. */ |
||
| 576 | foreach ( $this->getGvOutputData()->get_views() as $view_id => $data ) { |
||
| 577 | $content .= $this->render_view( array( 'id' => $view_id ) ); |
||
| 578 | } |
||
| 579 | } |
||
| 580 | } |
||
| 581 | } |
||
| 582 | |||
| 583 | // Add the filter back in |
||
| 584 | 1 | add_filter( 'the_content', array( $this, 'insert_view_in_content' ) ); |
|
| 585 | |||
| 586 | 1 | return $content; |
|
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Disable comments on GravityView post types |
||
| 591 | * @param boolean $open existing status |
||
| 592 | * @param int $post_id Post ID |
||
| 593 | * @return boolean |
||
| 594 | */ |
||
| 595 | public function comments_open( $open, $post_id ) { |
||
| 596 | |||
| 597 | if ( $this->isGravityviewPostType() ) { |
||
| 598 | $open = false; |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @filter `gravityview/comments_open` Whether to set comments to open or closed. |
||
| 603 | * @since 1.5.4 |
||
| 604 | * @param boolean $open Open or closed status |
||
| 605 | * @param int $post_id Post ID to set comment status for |
||
| 606 | */ |
||
| 607 | $open = apply_filters( 'gravityview/comments_open', $open, $post_id ); |
||
| 608 | |||
| 609 | return $open; |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Display a warning when a View has not been configured |
||
| 614 | * |
||
| 615 | * @since 1.19.2 |
||
| 616 | * |
||
| 617 | * @param int $view_id The ID of the View currently being displayed |
||
| 618 | * |
||
| 619 | * @return void |
||
| 620 | */ |
||
| 621 | public function context_not_configured_warning( $view_id = 0 ) { |
||
| 622 | |||
| 623 | if ( ! class_exists( 'GravityView_View' ) ) { |
||
| 624 | return; |
||
| 625 | } |
||
| 626 | |||
| 627 | $fields = GravityView_View::getInstance()->getContextFields(); |
||
| 628 | |||
| 629 | if ( ! empty( $fields ) ) { |
||
| 630 | return; |
||
| 631 | } |
||
| 632 | |||
| 633 | $context = GravityView_View::getInstance()->getContext(); |
||
| 634 | |||
| 635 | switch( $context ) { |
||
| 636 | case 'directory': |
||
| 637 | $tab = __( 'Multiple Entries', 'gravityview' ); |
||
| 638 | break; |
||
| 639 | case 'edit': |
||
| 640 | $tab = __( 'Edit Entry', 'gravityview' ); |
||
| 641 | break; |
||
| 642 | case 'single': |
||
| 643 | default: |
||
| 644 | $tab = __( 'Single Entry', 'gravityview' ); |
||
| 645 | break; |
||
| 646 | } |
||
| 647 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 648 | |||
| 649 | $title = sprintf( esc_html_x('The %s layout has not been configured.', 'Displayed when a View is not configured. %s is replaced by the tab label', 'gravityview' ), $tab ); |
||
| 650 | $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $view_id, $context ) ); |
||
| 651 | $action_text = sprintf( esc_html__('Add fields to %s', 'gravityview' ), $tab ); |
||
| 652 | $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' ); |
||
| 653 | |||
| 654 | $image = sprintf( '<img alt="%s" src="%s" style="margin-top: 10px;" />', $tab, esc_url(plugins_url( sprintf( 'assets/images/tab-%s.png', $context ), GRAVITYVIEW_FILE ) ) ); |
||
| 655 | $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message ); |
||
| 656 | |||
| 657 | echo GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $view_id ); |
||
| 658 | } |
||
| 659 | |||
| 660 | |||
| 661 | /** |
||
| 662 | * Core function to render a View based on a set of arguments |
||
| 663 | * |
||
| 664 | * @access public |
||
| 665 | * @static |
||
| 666 | * @param array $passed_args { |
||
| 667 | * |
||
| 668 | * Settings for rendering the View |
||
| 669 | * |
||
| 670 | * @type int $id View id |
||
| 671 | * @type int $page_size Number of entries to show per page |
||
| 672 | * @type string $sort_field Form field id to sort |
||
| 673 | * @type string $sort_direction Sorting direction ('ASC' or 'DESC') |
||
| 674 | * @type string $start_date - Ymd |
||
| 675 | * @type string $end_date - Ymd |
||
| 676 | * @type string $class - assign a html class to the view |
||
| 677 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 678 | * } |
||
| 679 | * |
||
| 680 | * @return string|null HTML output of a View, NULL if View isn't found |
||
| 681 | */ |
||
| 682 | 1 | public function render_view( $passed_args ) { |
|
| 683 | |||
| 684 | // validate attributes |
||
| 685 | 1 | if ( empty( $passed_args['id'] ) ) { |
|
| 686 | do_action( 'gravityview_log_error', '[render_view] Returning; no ID defined.', $passed_args ); |
||
| 687 | return null; |
||
| 688 | } |
||
| 689 | |||
| 690 | // Solve problem when loading content via admin-ajax.php |
||
| 691 | // @hack |
||
| 692 | 1 | if ( ! $this->getGvOutputData() ) { |
|
| 693 | |||
| 694 | 1 | do_action( 'gravityview_log_error', '[render_view] gv_output_data not defined; parsing content.', $passed_args ); |
|
| 695 | |||
| 696 | 1 | $this->parse_content(); |
|
| 697 | } |
||
| 698 | |||
| 699 | // Make 100% sure that we're dealing with a properly called situation |
||
| 700 | 1 | if ( ! is_object( $this->getGvOutputData() ) || ! is_callable( array( $this->getGvOutputData(), 'get_view' ) ) ) { |
|
| 701 | |||
| 702 | do_action( 'gravityview_log_error', '[render_view] gv_output_data not an object or get_view not callable.', $this->getGvOutputData() ); |
||
| 703 | |||
| 704 | return null; |
||
| 705 | } |
||
| 706 | |||
| 707 | 1 | $view_id = $passed_args['id']; |
|
| 708 | |||
| 709 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 710 | 1 | $view = gravityview()->views->get( $view_id ); |
|
| 711 | 1 | if ( ! $view ) { |
|
| 712 | /** Emulate the weird behavior of \GravityView_View_Data::get_view adding a view which wasn't there to begin with. */ |
||
| 713 | 1 | gravityview()->views->add( \GV\View::by_id( $view_id ) ); |
|
| 714 | 1 | $view = gravityview()->views->get( $view_id ); |
|
| 715 | |||
| 716 | 1 | if ( ! $view ) { |
|
| 717 | do_action( 'gravityview_log_debug', sprintf( 'GravityView_View_Data[add_view] Returning; View #%s does not exist.', $view_id ) ); |
||
| 718 | return null; |
||
| 719 | } |
||
| 720 | } |
||
| 721 | |||
| 722 | /** Update the view settings with the requested arguments. */ |
||
| 723 | 1 | $view->settings->update( $passed_args ); |
|
| 724 | } else { |
||
| 725 | /** \GravityView_View_Data::get_view is deprecated. */ |
||
| 726 | $view_data = $this->getGvOutputData()->get_view( $view_id, $passed_args ); |
||
| 727 | |||
| 728 | do_action( 'gravityview_log_debug', '[render_view] View Data: ', $view_data ); |
||
| 729 | |||
| 730 | do_action( 'gravityview_log_debug', '[render_view] Init View. Arguments: ', $passed_args ); |
||
| 731 | |||
| 732 | // The passed args were always winning, even if they were NULL. |
||
| 733 | // This prevents that. Filters NULL, FALSE, and empty strings. |
||
| 734 | $passed_args = array_filter( $passed_args, 'strlen' ); |
||
| 735 | |||
| 736 | //Override shortcode args over View template settings |
||
| 737 | $atts = wp_parse_args( $passed_args, $view_data['atts'] ); |
||
| 738 | |||
| 739 | do_action( 'gravityview_log_debug', '[render_view] Arguments after merging with View settings: ', $atts ); |
||
| 740 | } |
||
| 741 | |||
| 742 | // It's password protected and you need to log in. |
||
| 743 | 1 | if ( post_password_required( $view_id ) ) { |
|
| 744 | |||
| 745 | do_action( 'gravityview_log_error', sprintf( '[render_view] Returning: View %d is password protected.', $view_id ) ); |
||
| 746 | |||
| 747 | // If we're in an embed or on an archive page, show the password form |
||
| 748 | if ( get_the_ID() !== $view_id ) { |
||
| 749 | return get_the_password_form(); |
||
| 750 | } |
||
| 751 | |||
| 752 | // Otherwise, just get outta here |
||
| 753 | return null; |
||
| 754 | } |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Don't render View if user isn't allowed to see it |
||
| 758 | * @since 1.15 |
||
| 759 | * @since 1.17.2 Added check for if a user has no caps but is logged in (member of multisite, but not any site). Treat as if logged-out. |
||
| 760 | */ |
||
| 761 | 1 | if( is_user_logged_in() && ! ( empty( wp_get_current_user()->caps ) && empty( wp_get_current_user()->roles ) ) && false === GVCommon::has_cap( 'read_gravityview', $view_id ) ) { |
|
| 762 | |||
| 763 | do_action( 'gravityview_log_debug', sprintf( '%s Returning: View %d is not visible by current user.', __METHOD__, $view_id ) ); |
||
| 764 | |||
| 765 | return null; |
||
| 766 | } |
||
| 767 | |||
| 768 | 1 | if( $this->isGravityviewPostType() ) { |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode? |
||
| 772 | * @see https://codex.wordpress.org/Function_Reference/register_post_type#public |
||
| 773 | * @see \GV\Entry::get_endpoint_name |
||
| 774 | * @since 1.15.2 |
||
| 775 | * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded via shortcode. Default: `true` |
||
| 776 | * @param int $view_id The ID of the View currently being requested. `0` for general setting |
||
| 777 | */ |
||
| 778 | 1 | $direct_access = apply_filters( 'gravityview_direct_access', true, $view_id ); |
|
| 779 | |||
| 780 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 781 | 1 | $embed_only = $view->settings->get( 'embed_only' ); |
|
| 782 | } else { |
||
| 783 | /** Deprecated. View attributes moved to \GV\View::$settings. */ |
||
| 784 | $embed_only = ! empty( $atts['embed_only'] ); |
||
| 785 | } |
||
| 786 | |||
| 787 | 1 | if( ! $direct_access || ( $embed_only && ! GVCommon::has_cap( 'read_private_gravityviews' ) ) ) { |
|
| 788 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' ); |
|
| 789 | } |
||
| 790 | } |
||
| 791 | |||
| 792 | 1 | ob_start(); |
|
| 793 | |||
| 794 | /** |
||
| 795 | * Set globals for templating |
||
| 796 | * @deprecated 1.6.2 |
||
| 797 | */ |
||
| 798 | 1 | global $gravityview_view; |
|
| 799 | |||
| 800 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 801 | 1 | $view_data = $view->as_data(); |
|
| 802 | 1 | $gravityview_view = new GravityView_View( $view_data ); |
|
| 803 | 1 | $post_id = intval( $view->settings->get( 'post_id' ) ? : get_the_ID() ); |
|
| 804 | 1 | $template_id = $view->template ? $view->template->ID : null; |
|
| 805 | } else { |
||
| 806 | /** These constructs are deprecated. Use the new gravityview() wrapper. */ |
||
| 807 | $gravityview_view = new GravityView_View( $view_data ); |
||
| 808 | $post_id = ! empty( $atts['post_id'] ) ? intval( $atts['post_id'] ) : get_the_ID(); |
||
| 809 | $template_id = $view_data['template_id']; |
||
| 810 | } |
||
| 811 | |||
| 812 | 1 | $gravityview_view->setPostId( $post_id ); |
|
| 813 | |||
| 814 | 1 | if ( ! $this->getSingleEntry() ) { |
|
| 815 | |||
| 816 | // user requested Directory View |
||
| 817 | 1 | do_action( 'gravityview_log_debug', '[render_view] Executing Directory View' ); |
|
| 818 | |||
| 819 | //fetch template and slug |
||
| 820 | 1 | $view_slug = apply_filters( 'gravityview_template_slug_'. $template_id, 'table', 'directory' ); |
|
| 821 | |||
| 822 | 1 | do_action( 'gravityview_log_debug', '[render_view] View template slug: ', $view_slug ); |
|
| 823 | |||
| 824 | /** |
||
| 825 | * Disable fetching initial entries for views that don't need it (DataTables) |
||
| 826 | */ |
||
| 827 | 1 | $get_entries = apply_filters( 'gravityview_get_view_entries_'.$view_slug, true ); |
|
| 828 | |||
| 829 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 830 | 1 | $hide_until_searched = $view->settings->get( 'hide_until_searched' ); |
|
| 831 | } else { |
||
| 832 | /** $atts is deprecated, use \GV\View:$settings */ |
||
| 833 | $hide_until_searched = ! empty( $atts['hide_until_searched'] ); |
||
| 834 | } |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Hide View data until search is performed |
||
| 838 | * @since 1.5.4 |
||
| 839 | */ |
||
| 840 | 1 | if ( $hide_until_searched && ! $this->isSearch() ) { |
|
| 841 | $gravityview_view->setHideUntilSearched( true ); |
||
| 842 | $get_entries = false; |
||
| 843 | } |
||
| 844 | |||
| 845 | 1 | if ( $get_entries ) { |
|
| 846 | |||
| 847 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 848 | 1 | $sort_columns = $view->settings->get( 'sort_columns' ); |
|
| 849 | } else { |
||
| 850 | /** $atts is deprecated, use \GV\View:$settings */ |
||
| 851 | $sort_columns = ! empty( $atts['sort_columns'] ); |
||
| 852 | } |
||
| 853 | |||
| 854 | 1 | if ( $sort_columns ) { |
|
| 855 | // add filter to enable column sorting |
||
| 856 | add_filter( 'gravityview/template/field_label', array( $this, 'add_columns_sort_links' ) , 100, 3 ); |
||
| 857 | } |
||
| 858 | |||
| 859 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 860 | 1 | $view_entries = self::get_view_entries( $view->settings->as_atts(), $view->form->ID ); |
|
| 861 | } else { |
||
| 862 | /** $atts is deprecated, use \GV\View:$settings */ |
||
| 863 | /** $view_data is depreacted, use \GV\View properties */ |
||
| 864 | $view_entries = self::get_view_entries( $atts, $view_data['form_id'] ); |
||
| 865 | } |
||
| 866 | |||
| 867 | 1 | do_action( 'gravityview_log_debug', sprintf( '[render_view] Get Entries. Found %s entries total, showing %d entries', $view_entries['count'], sizeof( $view_entries['entries'] ) ) ); |
|
| 868 | |||
| 869 | } else { |
||
| 870 | |||
| 871 | $view_entries = array( 'count' => null, 'entries' => null, 'paging' => null ); |
||
| 872 | |||
| 873 | do_action( 'gravityview_log_debug', '[render_view] Not fetching entries because `gravityview_get_view_entries_'.$view_slug.'` is false' ); |
||
| 874 | } |
||
| 875 | |||
| 876 | 1 | $gravityview_view->setPaging( $view_entries['paging'] ); |
|
| 877 | 1 | $gravityview_view->setContext( 'directory' ); |
|
| 878 | 1 | $sections = array( 'header', 'body', 'footer' ); |
|
| 879 | |||
| 880 | } else { |
||
| 881 | |||
| 882 | // user requested Single Entry View |
||
| 883 | 1 | do_action( 'gravityview_log_debug', '[render_view] Executing Single View' ); |
|
| 884 | |||
| 885 | /** |
||
| 886 | * @action `gravityview_render_entry_{View ID}` Before rendering a single entry for a specific View ID |
||
| 887 | * @since 1.17 |
||
| 888 | */ |
||
| 889 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 890 | 1 | do_action( 'gravityview_render_entry_' . $view->ID ); |
|
| 891 | } else { |
||
| 892 | /** $view_data is depreacted, use \GV\View properties */ |
||
| 893 | do_action( 'gravityview_render_entry_'.$view_data['id'] ); |
||
| 894 | } |
||
| 895 | |||
| 896 | 1 | $entry = $this->getEntry(); |
|
| 897 | |||
| 898 | // You are not permitted to view this entry. |
||
| 899 | 1 | if ( empty( $entry ) || ! self::is_entry_approved( $entry, defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? $view->settings->as_atts() : $atts ) ) { |
|
| 900 | |||
| 901 | do_action( 'gravityview_log_debug', '[render_view] Entry does not exist. This may be because of View filters limiting access.' ); |
||
| 902 | |||
| 903 | // Only display warning once when multiple Views are embedded |
||
| 904 | if( $view_id !== (int) GravityView_frontend::get_context_view_id() ) { |
||
| 905 | ob_end_clean(); |
||
| 906 | return null; |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * @filter `gravityview/render/entry/not_visible` Modify the message shown to users when the entry doesn't exist or they aren't allowed to view it. |
||
| 911 | * @since 1.6 |
||
| 912 | * @param string $message Default: "You have attempted to view an entry that is not visible or may not exist." |
||
| 913 | */ |
||
| 914 | $message = apply_filters( 'gravityview/render/entry/not_visible', __( 'You have attempted to view an entry that is not visible or may not exist.', 'gravityview' ) ); |
||
| 915 | |||
| 916 | /** |
||
| 917 | * @since 1.6 |
||
| 918 | */ |
||
| 919 | echo esc_attr( $message ); |
||
| 920 | |||
| 921 | ob_end_clean(); |
||
| 922 | return null; |
||
| 923 | } |
||
| 924 | |||
| 925 | // We're in single view, but the view being processed is not the same view the single entry belongs to. |
||
| 926 | // important: do not remove this as it prevents fake attempts of displaying entries from other views/forms |
||
| 927 | 1 | $multiple_views = defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ? gravityview()->views->count() > 1 : $this->getGvOutputData()->has_multiple_views(); |
|
| 928 | 1 | if ( $multiple_views && $view_id != $this->get_context_view_id() ) { |
|
| 929 | do_action( 'gravityview_log_debug', '[render_view] In single entry view, but the entry does not belong to this View. Perhaps there are multiple views on the page. View ID: '. $view_id ); |
||
| 930 | ob_end_clean(); |
||
| 931 | return null; |
||
| 932 | } |
||
| 933 | |||
| 934 | //fetch template and slug |
||
| 935 | 1 | $view_slug = apply_filters( 'gravityview_template_slug_' . $template_id, 'table', 'single' ); |
|
| 936 | 1 | do_action( 'gravityview_log_debug', '[render_view] View single template slug: ', $view_slug ); |
|
| 937 | |||
| 938 | //fetch entry detail |
||
| 939 | 1 | $view_entries['count'] = 1; |
|
| 940 | 1 | $view_entries['entries'][] = $entry; |
|
| 941 | 1 | do_action( 'gravityview_log_debug', '[render_view] Get single entry: ', $view_entries['entries'] ); |
|
| 942 | |||
| 943 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 944 | 1 | $back_link_label = $view->settings->get( 'back_link_label', null ); |
|
| 945 | } else { |
||
| 946 | $back_link_label = isset( $atts['back_link_label'] ) ? $atts['back_link_label'] : null; |
||
| 947 | } |
||
| 948 | |||
| 949 | // set back link label |
||
| 950 | 1 | $gravityview_view->setBackLinkLabel( $back_link_label ); |
|
| 951 | 1 | $gravityview_view->setContext( 'single' ); |
|
| 952 | 1 | $sections = array( 'single' ); |
|
| 953 | |||
| 954 | } |
||
| 955 | |||
| 956 | // add template style |
||
| 957 | 1 | self::add_style( $template_id ); |
|
| 958 | |||
| 959 | // Prepare to render view and set vars |
||
| 960 | 1 | $gravityview_view->setEntries( $view_entries['entries'] ); |
|
| 961 | 1 | $gravityview_view->setTotalEntries( $view_entries['count'] ); |
|
| 962 | |||
| 963 | // If Edit |
||
| 964 | 1 | if ( 'edit' === gravityview_get_context() ) { |
|
| 965 | |||
| 966 | do_action( 'gravityview_log_debug', '[render_view] Edit Entry ' ); |
||
| 967 | |||
| 968 | do_action( 'gravityview_edit_entry', $this->getGvOutputData() ); |
||
| 969 | |||
| 970 | return ob_get_clean(); |
||
| 971 | |||
| 972 | } else { |
||
| 973 | // finaly we'll render some html |
||
| 974 | 1 | $sections = apply_filters( 'gravityview_render_view_sections', $sections, $template_id ); |
|
| 975 | |||
| 976 | 1 | do_action( 'gravityview_log_debug', '[render_view] Sections to render: ', $sections ); |
|
| 977 | 1 | foreach ( $sections as $section ) { |
|
| 978 | 1 | do_action( 'gravityview_log_debug', '[render_view] Rendering '. $section . ' section.' ); |
|
| 979 | 1 | $gravityview_view->render( $view_slug, $section, false ); |
|
| 980 | } |
||
| 981 | } |
||
| 982 | |||
| 983 | //@todo: check why we need the IF statement vs. print the view id always. |
||
| 984 | 1 | if ( $this->isGravityviewPostType() || $this->isPostHasShortcode() ) { |
|
| 985 | // Print the View ID to enable proper cookie pagination |
||
| 986 | 1 | echo '<input type="hidden" class="gravityview-view-id" value="' . esc_attr( $view_id ) . '">'; |
|
| 987 | } |
||
| 988 | 1 | $output = ob_get_clean(); |
|
| 989 | |||
| 990 | 1 | return $output; |
|
| 991 | } |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Process the start and end dates for a view - overrides values defined in shortcode (if needed) |
||
| 995 | * |
||
| 996 | * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), |
||
| 997 | * which uses \DateTime() format. |
||
| 998 | * |
||
| 999 | * You can set the `start_date` or `end_date` to any value allowed by {@link http://www.php.net//manual/en/function.strtotime.php strtotime()}, |
||
| 1000 | * including strings like "now" or "-1 year" or "-3 days". |
||
| 1001 | * |
||
| 1002 | * @see GFFormsModel::get_date_range_where |
||
| 1003 | * |
||
| 1004 | * @param array $args View settings |
||
| 1005 | * @param array $search_criteria Search being performed, if any |
||
| 1006 | * @return array Modified `$search_criteria` array |
||
| 1007 | */ |
||
| 1008 | 1 | public static function process_search_dates( $args, $search_criteria = array() ) { |
|
| 1009 | |||
| 1010 | 1 | $return_search_criteria = $search_criteria; |
|
| 1011 | |||
| 1012 | 1 | foreach ( array( 'start_date', 'end_date' ) as $key ) { |
|
| 1013 | |||
| 1014 | |||
| 1015 | // Is the start date or end date set in the view or shortcode? |
||
| 1016 | // If so, we want to make sure that the search doesn't go outside the bounds defined. |
||
| 1017 | 1 | if ( ! empty( $args[ $key ] ) ) { |
|
| 1018 | |||
| 1019 | // Get a timestamp and see if it's a valid date format |
||
| 1020 | 1 | $date = strtotime( $args[ $key ] ); |
|
| 1021 | |||
| 1022 | // The date was invalid |
||
| 1023 | 1 | if ( empty( $date ) ) { |
|
| 1024 | do_action( 'gravityview_log_error', __METHOD__ . ' Invalid ' . $key . ' date format: ' . $args[ $key ] ); |
||
| 1025 | continue; |
||
| 1026 | } |
||
| 1027 | |||
| 1028 | // The format that Gravity Forms expects for start_date and day-specific (not hour/second-specific) end_date |
||
| 1029 | 1 | $datetime_format = 'Y-m-d H:i:s'; |
|
| 1030 | 1 | $search_is_outside_view_bounds = false; |
|
| 1031 | |||
| 1032 | 1 | if( ! empty( $search_criteria[ $key ] ) ) { |
|
| 1033 | |||
| 1034 | 1 | $search_date = strtotime( $search_criteria[ $key ] ); |
|
| 1035 | |||
| 1036 | // The search is for entries before the start date defined by the settings |
||
| 1037 | switch ( $key ) { |
||
| 1038 | 1 | case 'end_date': |
|
| 1039 | /** |
||
| 1040 | * If the end date is formatted as 'Y-m-d', it should be formatted without hours and seconds |
||
| 1041 | * so that Gravity Forms can convert the day to 23:59:59 the previous day. |
||
| 1042 | * |
||
| 1043 | * If it's a relative date ("now" or "-1 day"), then it should use the precise date format |
||
| 1044 | * |
||
| 1045 | * @see GFFormsModel::get_date_range_where |
||
| 1046 | */ |
||
| 1047 | 1 | $datetime_format = gravityview_is_valid_datetime( $args[ $key ] ) ? 'Y-m-d' : 'Y-m-d H:i:s'; |
|
| 1048 | 1 | $search_is_outside_view_bounds = ( $search_date > $date ); |
|
| 1049 | 1 | break; |
|
| 1050 | 1 | case 'start_date': |
|
| 1051 | 1 | $search_is_outside_view_bounds = ( $search_date < $date ); |
|
| 1052 | 1 | break; |
|
| 1053 | } |
||
| 1054 | } |
||
| 1055 | |||
| 1056 | // If there is no search being performed, or if there is a search being performed that's outside the bounds |
||
| 1057 | 1 | if ( empty( $search_criteria[ $key ] ) || $search_is_outside_view_bounds ) { |
|
| 1058 | |||
| 1059 | // Then we override the search and re-set the start date |
||
| 1060 | 1 | $return_search_criteria[ $key ] = date_i18n( $datetime_format , $date, true ); |
|
| 1061 | } |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | 1 | if( isset( $return_search_criteria['start_date'] ) && isset( $return_search_criteria['end_date'] ) ) { |
|
| 1066 | // The start date is AFTER the end date. This will result in no results, but let's not force the issue. |
||
| 1067 | 1 | if ( strtotime( $return_search_criteria['start_date'] ) > strtotime( $return_search_criteria['end_date'] ) ) { |
|
| 1068 | 1 | do_action( 'gravityview_log_error', __METHOD__ . ' Invalid search: the start date is after the end date.', $return_search_criteria ); |
|
| 1069 | } |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | 1 | return $return_search_criteria; |
|
| 1073 | } |
||
| 1074 | |||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Process the approved only search criteria according to the View settings |
||
| 1078 | * |
||
| 1079 | * @param array $args View settings |
||
| 1080 | * @param array $search_criteria Search being performed, if any |
||
| 1081 | * @return array Modified `$search_criteria` array |
||
| 1082 | */ |
||
| 1083 | public static function process_search_only_approved( $args, $search_criteria ) { |
||
| 1084 | |||
| 1085 | /** @since 1.19 */ |
||
| 1086 | if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) { |
||
| 1087 | do_action( 'gravityview_log_debug', __METHOD__ . ': User can moderate entries; showing all approval statuses' ); |
||
| 1088 | return $search_criteria; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | if ( ! empty( $args['show_only_approved'] ) ) { |
||
| 1092 | |||
| 1093 | $search_criteria['field_filters'][] = array( |
||
| 1094 | 'key' => GravityView_Entry_Approval::meta_key, |
||
| 1095 | 'value' => GravityView_Entry_Approval_Status::APPROVED |
||
| 1096 | ); |
||
| 1097 | |||
| 1098 | $search_criteria['field_filters']['mode'] = 'all'; // force all the criterias to be met |
||
| 1099 | |||
| 1100 | do_action( 'gravityview_log_debug', '[process_search_only_approved] Search Criteria if show only approved: ', $search_criteria ); |
||
| 1101 | } |
||
| 1102 | |||
| 1103 | return $search_criteria; |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * Check if a certain entry is approved. |
||
| 1109 | * |
||
| 1110 | * If we pass the View settings ($args) it will check the 'show_only_approved' setting before |
||
| 1111 | * checking the entry approved field, returning true if show_only_approved = false. |
||
| 1112 | * |
||
| 1113 | * @since 1.7 |
||
| 1114 | * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved |
||
| 1115 | * |
||
| 1116 | * @uses GravityView_Entry_Approval_Status::is_approved |
||
| 1117 | * |
||
| 1118 | * @param array $entry Entry object |
||
| 1119 | * @param array $args View settings (optional) |
||
| 1120 | * |
||
| 1121 | * @return bool |
||
| 1122 | */ |
||
| 1123 | public static function is_entry_approved( $entry, $args = array() ) { |
||
| 1124 | |||
| 1125 | if ( empty( $entry['id'] ) || ( array_key_exists( 'show_only_approved', $args ) && ! $args['show_only_approved'] ) ) { |
||
| 1126 | // is implicitly approved if entry is null or View settings doesn't require to check for approval |
||
| 1127 | return true; |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | /** @since 1.19 */ |
||
| 1131 | if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) { |
||
| 1132 | do_action( 'gravityview_log_debug', __METHOD__ . ': User can moderate entries, so entry is approved for viewing' ); |
||
| 1133 | return true; |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | $is_approved = gform_get_meta( $entry['id'], GravityView_Entry_Approval::meta_key ); |
||
| 1137 | |||
| 1138 | return GravityView_Entry_Approval_Status::is_approved( $is_approved ); |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Parse search criteria for a entries search. |
||
| 1143 | * |
||
| 1144 | * array( |
||
| 1145 | * 'search_field' => 1, // ID of the field |
||
| 1146 | * 'search_value' => '', // Value of the field to search |
||
| 1147 | * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains' |
||
| 1148 | * 'show_only_approved' => 0 or 1 // Boolean |
||
| 1149 | * ) |
||
| 1150 | * |
||
| 1151 | * @param array $args Array of args |
||
| 1152 | * @param int $form_id Gravity Forms form ID |
||
| 1153 | * @return array Array of search parameters, formatted in Gravity Forms mode, using `status` key set to "active" by default, `field_filters` array with `key`, `value` and `operator` keys. |
||
| 1154 | */ |
||
| 1155 | 1 | public static function get_search_criteria( $args, $form_id ) { |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * @filter `gravityview_fe_search_criteria` Modify the search criteria |
||
| 1159 | * @see GravityView_Widget_Search::filter_entries Adds the default search criteria |
||
| 1160 | * @param array $search_criteria Empty `field_filters` key |
||
| 1161 | * @param int $form_id ID of the Gravity Forms form that is being searched |
||
| 1162 | */ |
||
| 1163 | 1 | $search_criteria = apply_filters( 'gravityview_fe_search_criteria', array( 'field_filters' => array() ), $form_id ); |
|
| 1164 | |||
| 1165 | 1 | $original_search_criteria = $search_criteria; |
|
| 1166 | |||
| 1167 | 1 | do_action( 'gravityview_log_debug', '[get_search_criteria] Search Criteria after hook gravityview_fe_search_criteria: ', $search_criteria ); |
|
| 1168 | |||
| 1169 | // implicity search |
||
| 1170 | 1 | if ( ! empty( $args['search_value'] ) ) { |
|
| 1171 | |||
| 1172 | // Search operator options. Options: `is` or `contains` |
||
| 1173 | 1 | $operator = ! empty( $args['search_operator'] ) && in_array( $args['search_operator'], array( 'is', 'isnot', '>', '<', 'contains' ) ) ? $args['search_operator'] : 'contains'; |
|
| 1174 | |||
| 1175 | 1 | $search_criteria['field_filters'][] = array( |
|
| 1176 | 1 | 'key' => rgget( 'search_field', $args ), // The field ID to search |
|
| 1177 | 1 | 'value' => _wp_specialchars( $args['search_value'] ), // The value to search. Encode ampersands but not quotes. |
|
| 1178 | 1 | 'operator' => $operator, |
|
| 1179 | ); |
||
| 1180 | |||
| 1181 | // Lock search mode to "all" with implicit presearch filter. |
||
| 1182 | 1 | $search_criteria['field_filters']['mode'] = 'all'; |
|
| 1183 | } |
||
| 1184 | |||
| 1185 | 1 | if( $search_criteria !== $original_search_criteria ) { |
|
| 1186 | 1 | do_action( 'gravityview_log_debug', '[get_search_criteria] Search Criteria after implicity search: ', $search_criteria ); |
|
| 1187 | } |
||
| 1188 | |||
| 1189 | // Handle setting date range |
||
| 1190 | 1 | $search_criteria = self::process_search_dates( $args, $search_criteria ); |
|
| 1191 | |||
| 1192 | 1 | if( $search_criteria !== $original_search_criteria ) { |
|
| 1193 | 1 | do_action( 'gravityview_log_debug', '[get_search_criteria] Search Criteria after date params: ', $search_criteria ); |
|
| 1194 | } |
||
| 1195 | |||
| 1196 | // remove not approved entries |
||
| 1197 | 1 | $search_criteria = self::process_search_only_approved( $args, $search_criteria ); |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * @filter `gravityview_status` Modify entry status requirements to be included in search results. |
||
| 1201 | * @param string $status Default: `active`. Accepts all Gravity Forms entry statuses, including `spam` and `trash` |
||
| 1202 | */ |
||
| 1203 | 1 | $search_criteria['status'] = apply_filters( 'gravityview_status', 'active', $args ); |
|
| 1204 | |||
| 1205 | 1 | return $search_criteria; |
|
| 1206 | } |
||
| 1207 | |||
| 1208 | |||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Core function to calculate View multi entries (directory) based on a set of arguments ($args): |
||
| 1212 | * $id - View id |
||
| 1213 | * $page_size - Page |
||
| 1214 | * $sort_field - form field id to sort |
||
| 1215 | * $sort_direction - ASC / DESC |
||
| 1216 | * $start_date - Ymd |
||
| 1217 | * $end_date - Ymd |
||
| 1218 | * $class - assign a html class to the view |
||
| 1219 | * $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 1220 | * |
||
| 1221 | * |
||
| 1222 | * |
||
| 1223 | * @uses gravityview_get_entries() |
||
| 1224 | * @access public |
||
| 1225 | * @param array $args\n |
||
| 1226 | * - $id - View id |
||
| 1227 | * - $page_size - Page |
||
| 1228 | * - $sort_field - form field id to sort |
||
| 1229 | * - $sort_direction - ASC / DESC |
||
| 1230 | * - $start_date - Ymd |
||
| 1231 | * - $end_date - Ymd |
||
| 1232 | * - $class - assign a html class to the view |
||
| 1233 | * - $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 1234 | * @param int $form_id Gravity Forms Form ID |
||
| 1235 | * @return array Associative array with `count`, `entries`, and `paging` keys. `count` has the total entries count, `entries` is an array with Gravity Forms full entry data, `paging` is an array with `offset` and `page_size` keys |
||
| 1236 | */ |
||
| 1237 | 1 | public static function get_view_entries( $args, $form_id ) { |
|
| 1238 | |||
| 1239 | 1 | do_action( 'gravityview_log_debug', '[get_view_entries] init' ); |
|
| 1240 | // start filters and sorting |
||
| 1241 | |||
| 1242 | 1 | $parameters = self::get_view_entries_parameters( $args, $form_id ); |
|
| 1243 | |||
| 1244 | 1 | $count = 0; // Must be defined so that gravityview_get_entries can use by reference |
|
| 1245 | |||
| 1246 | // fetch entries |
||
| 1247 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 1248 | list( $entries, $paging, $count ) = |
||
| 1249 | 1 | \GV\Mocks\GravityView_frontend_get_view_entries( $args, $form_id, $parameters, $count ); |
|
| 1250 | } else { |
||
| 1251 | /** Deprecated, use $form->entries instead. */ |
||
| 1252 | $entries = gravityview_get_entries( $form_id, $parameters, $count ); |
||
| 1253 | |||
| 1254 | /** Set paging. */ |
||
| 1255 | $paging = rgar( $parameters, 'paging' ); |
||
| 1256 | |||
| 1257 | /** Adjust count by defined offset. */ |
||
| 1258 | $count = max( 0, ( $count - rgar( $args, 'offset', 0 ) ) ); |
||
| 1259 | } |
||
| 1260 | |||
| 1261 | 1 | do_action( 'gravityview_log_debug', sprintf( '%s: Get Entries. Found: %s entries', __METHOD__, $count ), $entries ); |
|
| 1262 | |||
| 1263 | /** |
||
| 1264 | * @filter `gravityview_view_entries` Filter the entries output to the View |
||
| 1265 | * @deprecated since 1.5.2 |
||
| 1266 | * @param array $args View settings associative array |
||
| 1267 | * @var array |
||
| 1268 | */ |
||
| 1269 | 1 | $entries = apply_filters( 'gravityview_view_entries', $entries, $args ); |
|
| 1270 | |||
| 1271 | $return = array( |
||
| 1272 | 1 | 'count' => $count, |
|
| 1273 | 1 | 'entries' => $entries, |
|
| 1274 | 1 | 'paging' => $paging, |
|
| 1275 | ); |
||
| 1276 | |||
| 1277 | /** |
||
| 1278 | * @filter `gravityview/view/entries` Filter the entries output to the View |
||
| 1279 | * @param array $criteria associative array containing count, entries & paging |
||
| 1280 | * @param array $args View settings associative array |
||
| 1281 | * @since 1.5.2 |
||
| 1282 | */ |
||
| 1283 | 1 | return apply_filters( 'gravityview/view/entries', $return, $args ); |
|
| 1284 | } |
||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Get an array of search parameters formatted as Gravity Forms requires |
||
| 1288 | * |
||
| 1289 | * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters |
||
| 1290 | * |
||
| 1291 | * @uses GravityView_frontend::get_search_criteria |
||
| 1292 | * @uses GravityView_frontend::get_search_criteria_paging |
||
| 1293 | * |
||
| 1294 | * @since 1.20 |
||
| 1295 | * |
||
| 1296 | * @see \GV\View_Settings::defaults For $args options |
||
| 1297 | * |
||
| 1298 | * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults |
||
| 1299 | * @param int $form_id Gravity Forms form ID to search |
||
| 1300 | * |
||
| 1301 | * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys |
||
| 1302 | */ |
||
| 1303 | public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) { |
||
| 1304 | |||
| 1305 | |||
| 1306 | if ( ! is_array( $args ) || ! is_numeric( $form_id ) ) { |
||
| 1307 | |||
| 1308 | do_action( 'gravityview_log_error', __METHOD__ . ': Passed args are not an array or the form ID is not numeric' ); |
||
| 1309 | |||
| 1310 | return array(); |
||
| 1311 | } |
||
| 1312 | |||
| 1313 | $form_id = intval( $form_id ); |
||
| 1314 | |||
| 1315 | /** |
||
| 1316 | * Process search parameters |
||
| 1317 | * @var array |
||
| 1318 | */ |
||
| 1319 | $search_criteria = self::get_search_criteria( $args, $form_id ); |
||
| 1320 | |||
| 1321 | $paging = self::get_search_criteria_paging( $args ); |
||
| 1322 | |||
| 1323 | $parameters = array( |
||
| 1324 | 'search_criteria' => $search_criteria, |
||
| 1325 | 'sorting' => self::updateViewSorting( $args, $form_id ), |
||
| 1326 | 'paging' => $paging, |
||
| 1327 | 'cache' => isset( $args['cache'] ) ? $args['cache'] : true, |
||
| 1328 | ); |
||
| 1329 | |||
| 1330 | /** |
||
| 1331 | * @filter `gravityview_get_entries` Filter get entries criteria |
||
| 1332 | * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys. |
||
| 1333 | * @param array $args View configuration args. { |
||
| 1334 | * @type int $id View id |
||
| 1335 | * @type int $page_size Number of entries to show per page |
||
| 1336 | * @type string $sort_field Form field id to sort |
||
| 1337 | * @type string $sort_direction Sorting direction ('ASC' or 'DESC') |
||
| 1338 | * @type string $start_date - Ymd |
||
| 1339 | * @type string $end_date - Ymd |
||
| 1340 | * @type string $class - assign a html class to the view |
||
| 1341 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 1342 | * } |
||
| 1343 | * @param int $form_id ID of Gravity Forms form |
||
| 1344 | */ |
||
| 1345 | $parameters = apply_filters( 'gravityview_get_entries', $parameters, $args, $form_id ); |
||
| 1346 | |||
| 1347 | /** |
||
| 1348 | * @filter `gravityview_get_entries_{View ID}` Filter get entries criteria |
||
| 1349 | * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys. |
||
| 1350 | * @param array $args View configuration args. |
||
| 1351 | */ |
||
| 1352 | $parameters = apply_filters( 'gravityview_get_entries_'.$args['id'], $parameters, $args, $form_id ); |
||
| 1353 | |||
| 1354 | do_action( 'gravityview_log_debug', __METHOD__ . ': $parameters passed to gravityview_get_entries(): ', $parameters ); |
||
| 1355 | |||
| 1356 | return $parameters; |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | /** |
||
| 1360 | * Get the paging array for the View |
||
| 1361 | * |
||
| 1362 | * @since 1.19.5 |
||
| 1363 | * |
||
| 1364 | * @param $args |
||
| 1365 | * @param int $form_id |
||
| 1366 | */ |
||
| 1367 | public static function get_search_criteria_paging( $args ) { |
||
| 1368 | |||
| 1369 | /** |
||
| 1370 | * @filter `gravityview_default_page_size` The default number of entries displayed in a View |
||
| 1371 | * @since 1.1.6 |
||
| 1372 | * @param int $default_page_size Default: 25 |
||
| 1373 | */ |
||
| 1374 | $default_page_size = apply_filters( 'gravityview_default_page_size', 25 ); |
||
| 1375 | |||
| 1376 | // Paging & offset |
||
| 1377 | $page_size = ! empty( $args['page_size'] ) ? intval( $args['page_size'] ) : $default_page_size; |
||
| 1378 | |||
| 1379 | if ( -1 === $page_size ) { |
||
| 1380 | $page_size = PHP_INT_MAX; |
||
| 1381 | } |
||
| 1382 | |||
| 1383 | $curr_page = empty( $_GET['pagenum'] ) ? 1 : intval( $_GET['pagenum'] ); |
||
| 1384 | $offset = ( $curr_page - 1 ) * $page_size; |
||
| 1385 | |||
| 1386 | if ( ! empty( $args['offset'] ) ) { |
||
| 1387 | $offset += intval( $args['offset'] ); |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | $paging = array( |
||
| 1391 | 'offset' => $offset, |
||
| 1392 | 'page_size' => $page_size, |
||
| 1393 | ); |
||
| 1394 | |||
| 1395 | do_action( 'gravityview_log_debug', __METHOD__ . ': Paging: ', $paging ); |
||
| 1396 | |||
| 1397 | return $paging; |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Updates the View sorting criteria |
||
| 1402 | * |
||
| 1403 | * @since 1.7 |
||
| 1404 | * |
||
| 1405 | * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys |
||
| 1406 | * @param int $form_id The ID of the form used to sort |
||
| 1407 | * @return array $sorting Array with `key`, `direction` and `is_numeric` keys |
||
| 1408 | */ |
||
| 1409 | public static function updateViewSorting( $args, $form_id ) { |
||
| 1410 | $sorting = array(); |
||
| 1411 | $sort_field_id = isset( $_GET['sort'] ) ? $_GET['sort'] : rgar( $args, 'sort_field' ); |
||
| 1412 | $sort_direction = isset( $_GET['dir'] ) ? $_GET['dir'] : rgar( $args, 'sort_direction' ); |
||
| 1413 | |||
| 1414 | $sort_field_id = self::_override_sorting_id_by_field_type( $sort_field_id, $form_id ); |
||
| 1415 | |||
| 1416 | if ( ! empty( $sort_field_id ) ) { |
||
| 1417 | $sorting = array( |
||
| 1418 | 'key' => $sort_field_id, |
||
| 1419 | 'direction' => strtolower( $sort_direction ), |
||
| 1420 | 'is_numeric' => GVCommon::is_field_numeric( $form_id, $sort_field_id ) |
||
| 1421 | ); |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | GravityView_View::getInstance()->setSorting( $sorting ); |
||
| 1425 | |||
| 1426 | do_action( 'gravityview_log_debug', '[updateViewSorting] Sort Criteria : ', $sorting ); |
||
| 1427 | |||
| 1428 | return $sorting; |
||
| 1429 | |||
| 1430 | } |
||
| 1431 | |||
| 1432 | /** |
||
| 1433 | * Override sorting per field |
||
| 1434 | * |
||
| 1435 | * Currently only modifies sorting ID when sorting by the full name. Sorts by first name. |
||
| 1436 | * Use the `gravityview/sorting/full-name` filter to override. |
||
| 1437 | * |
||
| 1438 | * @todo Filter from GravityView_Field |
||
| 1439 | * @since 1.7.4 |
||
| 1440 | * |
||
| 1441 | * @param int|string $sort_field_id Field used for sorting (`id` or `1.2`) |
||
| 1442 | * @param int $form_id GF Form ID |
||
| 1443 | * |
||
| 1444 | * @return string Possibly modified sorting ID |
||
| 1445 | */ |
||
| 1446 | private static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) { |
||
| 1447 | |||
| 1448 | $form = gravityview_get_form( $form_id ); |
||
| 1449 | |||
| 1450 | $sort_field = GFFormsModel::get_field( $form, $sort_field_id ); |
||
| 1451 | |||
| 1452 | if( ! $sort_field ) { |
||
| 1453 | return $sort_field_id; |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | switch ( $sort_field['type'] ) { |
||
| 1457 | |||
| 1458 | case 'address': |
||
| 1459 | // Sorting by full address |
||
| 1460 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Override how to sort when sorting address |
||
| 1464 | * |
||
| 1465 | * @since 1.8 |
||
| 1466 | * |
||
| 1467 | * @param string $address_part `street`, `street2`, `city`, `state`, `zip`, or `country` (default: `city`) |
||
| 1468 | * @param string $sort_field_id Field used for sorting |
||
| 1469 | * @param int $form_id GF Form ID |
||
| 1470 | */ |
||
| 1471 | $address_part = apply_filters( 'gravityview/sorting/address', 'city', $sort_field_id, $form_id ); |
||
| 1472 | |||
| 1473 | switch( strtolower( $address_part ) ){ |
||
| 1474 | case 'street': |
||
| 1475 | $sort_field_id .= '.1'; |
||
| 1476 | break; |
||
| 1477 | case 'street2': |
||
| 1478 | $sort_field_id .= '.2'; |
||
| 1479 | break; |
||
| 1480 | default: |
||
| 1481 | case 'city': |
||
| 1482 | $sort_field_id .= '.3'; |
||
| 1483 | break; |
||
| 1484 | case 'state': |
||
| 1485 | $sort_field_id .= '.4'; |
||
| 1486 | break; |
||
| 1487 | case 'zip': |
||
| 1488 | $sort_field_id .= '.5'; |
||
| 1489 | break; |
||
| 1490 | case 'country': |
||
| 1491 | $sort_field_id .= '.6'; |
||
| 1492 | break; |
||
| 1493 | } |
||
| 1494 | |||
| 1495 | } |
||
| 1496 | break; |
||
| 1497 | case 'name': |
||
| 1498 | // Sorting by full name, not first, last, etc. |
||
| 1499 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1500 | /** |
||
| 1501 | * @filter `gravityview/sorting/full-name` Override how to sort when sorting full name. |
||
| 1502 | * @since 1.7.4 |
||
| 1503 | * @param[in,out] string $name_part Sort by `first` or `last` (default: `first`) |
||
| 1504 | * @param[in] string $sort_field_id Field used for sorting |
||
| 1505 | * @param[in] int $form_id GF Form ID |
||
| 1506 | */ |
||
| 1507 | $name_part = apply_filters( 'gravityview/sorting/full-name', 'first', $sort_field_id, $form_id ); |
||
| 1508 | |||
| 1509 | if ( 'last' === strtolower( $name_part ) ) { |
||
| 1510 | $sort_field_id .= '.6'; |
||
| 1511 | } else { |
||
| 1512 | $sort_field_id .= '.3'; |
||
| 1513 | } |
||
| 1514 | } |
||
| 1515 | break; |
||
| 1516 | case 'list': |
||
| 1517 | $sort_field_id = false; |
||
| 1518 | break; |
||
| 1519 | case 'time': |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * @filter `gravityview/sorting/time` Override how to sort when sorting time |
||
| 1523 | * @see GravityView_Field_Time |
||
| 1524 | * @since 1.14 |
||
| 1525 | * @param[in,out] string $name_part Field used for sorting |
||
| 1526 | * @param[in] int $form_id GF Form ID |
||
| 1527 | */ |
||
| 1528 | $sort_field_id = apply_filters( 'gravityview/sorting/time', $sort_field_id, $form_id ); |
||
| 1529 | break; |
||
| 1530 | } |
||
| 1531 | |||
| 1532 | return $sort_field_id; |
||
| 1533 | } |
||
| 1534 | |||
| 1535 | /** |
||
| 1536 | * Verify if user requested a single entry view |
||
| 1537 | * @return boolean|string false if not, single entry slug if true |
||
| 1538 | */ |
||
| 1539 | 3 | public static function is_single_entry() { |
|
| 1540 | |||
| 1541 | 3 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 1542 | 3 | $var_name = \GV\Entry::get_endpoint_name(); |
|
| 1543 | } else { |
||
| 1544 | /** Deprecated. Use \GV\Entry::get_endpoint_name instead. */ |
||
| 1545 | $var_name = GravityView_Post_Types::get_entry_var_name(); |
||
| 1546 | } |
||
| 1547 | |||
| 1548 | 3 | $single_entry = get_query_var( $var_name ); |
|
| 1549 | |||
| 1550 | /** |
||
| 1551 | * Modify the entry that is being displayed. |
||
| 1552 | * |
||
| 1553 | * @internal Should only be used by things like the oEmbed functionality. |
||
| 1554 | * @since 1.6 |
||
| 1555 | */ |
||
| 1556 | 3 | $single_entry = apply_filters( 'gravityview/is_single_entry', $single_entry ); |
|
| 1557 | |||
| 1558 | 3 | if ( empty( $single_entry ) ){ |
|
| 1559 | return false; |
||
| 1560 | } else { |
||
| 1561 | 3 | return $single_entry; |
|
| 1562 | } |
||
| 1563 | } |
||
| 1564 | |||
| 1565 | |||
| 1566 | /** |
||
| 1567 | * Register styles and scripts |
||
| 1568 | * |
||
| 1569 | * @access public |
||
| 1570 | * @return void |
||
| 1571 | */ |
||
| 1572 | 1 | public function add_scripts_and_styles() { |
|
| 1573 | 1 | global $post, $posts; |
|
| 1574 | // enqueue template specific styles |
||
| 1575 | 1 | if ( $this->getGvOutputData() ) { |
|
| 1576 | |||
| 1577 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 1578 | 1 | $views = gravityview()->views->all(); |
|
| 1579 | } else { |
||
| 1580 | /** \GravityView_View_Data::get_view is no more... */ |
||
| 1581 | $views = $this->getGvOutputData()->get_views(); |
||
| 1582 | } |
||
| 1583 | |||
| 1584 | 1 | foreach ( $views as $view_id => $data ) { |
|
| 1585 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 1586 | 1 | $view = $data; |
|
| 1587 | 1 | $view_id = $view->ID; |
|
| 1588 | 1 | $template_id = $view->template ? $view->template->ID : null; |
|
| 1589 | 1 | $data = $view->as_data(); |
|
| 1590 | } else { |
||
| 1591 | $template_id = $data['template_id']; |
||
| 1592 | } |
||
| 1593 | |||
| 1594 | /** |
||
| 1595 | * Don't enqueue the scripts or styles if it's not going to be displayed. |
||
| 1596 | * @since 1.15 |
||
| 1597 | */ |
||
| 1598 | 1 | if( is_user_logged_in() && false === GVCommon::has_cap( 'read_gravityview', $view_id ) ) { |
|
| 1599 | continue; |
||
| 1600 | } |
||
| 1601 | |||
| 1602 | // By default, no thickbox |
||
| 1603 | 1 | $js_dependencies = array( 'jquery', 'gravityview-jquery-cookie' ); |
|
| 1604 | 1 | $css_dependencies = array(); |
|
| 1605 | |||
| 1606 | 1 | if ( defined( 'GRAVITYVIEW_FUTURE_CORE_LOADED' ) ) { |
|
| 1607 | 1 | $lightbox = $view->settings->get( 'lightbox' ); |
|
| 1608 | } else { |
||
| 1609 | /** View data attributes are now stored in \GV\View::$settings */ |
||
| 1610 | $lightbox = ! empty( $data['atts']['lightbox'] ); |
||
| 1611 | } |
||
| 1612 | |||
| 1613 | // If the thickbox is enqueued, add dependencies |
||
| 1614 | 1 | if ( $lightbox ) { |
|
| 1615 | |||
| 1616 | /** |
||
| 1617 | * @filter `gravity_view_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox` |
||
| 1618 | * @param string $script_slug If you want to use a different lightbox script, return the name of it here. |
||
| 1619 | */ |
||
| 1620 | 1 | $js_dependencies[] = apply_filters( 'gravity_view_lightbox_script', 'thickbox' ); |
|
| 1621 | |||
| 1622 | /** |
||
| 1623 | * @filter `gravity_view_lightbox_style` Modify the lightbox CSS slug. Default: `thickbox` |
||
| 1624 | * @param string $script_slug If you want to use a different lightbox script, return the name of its CSS file here. |
||
| 1625 | */ |
||
| 1626 | 1 | $css_dependencies[] = apply_filters( 'gravity_view_lightbox_style', 'thickbox' ); |
|
| 1627 | } |
||
| 1628 | |||
| 1629 | /** |
||
| 1630 | * If the form has checkbox fields, enqueue dashicons |
||
| 1631 | * @see https://github.com/katzwebservices/GravityView/issues/536 |
||
| 1632 | * @since 1.15 |
||
| 1633 | */ |
||
| 1634 | 1 | if( gravityview_view_has_single_checkbox_or_radio( $data['form'], $data['fields'] ) ) { |
|
| 1635 | $css_dependencies[] = 'dashicons'; |
||
| 1636 | } |
||
| 1637 | |||
| 1638 | 1 | wp_register_script( 'gravityview-jquery-cookie', plugins_url( 'assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE ), array( 'jquery' ), GravityView_Plugin::version, true ); |
|
| 1639 | |||
| 1640 | 1 | $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
|
| 1641 | |||
| 1642 | 1 | wp_register_script( 'gravityview-fe-view', plugins_url( 'assets/js/fe-views' . $script_debug . '.js', GRAVITYVIEW_FILE ), apply_filters( 'gravityview_js_dependencies', $js_dependencies ) , GravityView_Plugin::version, true ); |
|
| 1643 | |||
| 1644 | 1 | wp_enqueue_script( 'gravityview-fe-view' ); |
|
| 1645 | |||
| 1646 | 1 | if ( ! empty( $data['atts']['sort_columns'] ) ) { |
|
| 1647 | wp_enqueue_style( 'gravityview_font', plugins_url( 'assets/css/font.css', GRAVITYVIEW_FILE ), $css_dependencies, GravityView_Plugin::version, 'all' ); |
||
| 1648 | } |
||
| 1649 | |||
| 1650 | 1 | $this->enqueue_default_style( $css_dependencies ); |
|
| 1651 | |||
| 1652 | 1 | self::add_style( $template_id ); |
|
| 1653 | } |
||
| 1654 | |||
| 1655 | 1 | if ( 'wp_print_footer_scripts' === current_filter() ) { |
|
| 1656 | |||
| 1657 | $js_localization = array( |
||
| 1658 | 'cookiepath' => COOKIEPATH, |
||
| 1659 | 'clear' => _x( 'Clear', 'Clear all data from the form', 'gravityview' ), |
||
| 1660 | 'reset' => _x( 'Reset', 'Reset the search form to the state that existed on page load', 'gravityview' ), |
||
| 1661 | ); |
||
| 1662 | |||
| 1663 | /** |
||
| 1664 | * @filter `gravityview_js_localization` Modify the array passed to wp_localize_script() |
||
| 1665 | * @param array $js_localization The data padded to the Javascript file |
||
| 1666 | * @param array $views Array of View data arrays with View settings |
||
| 1667 | */ |
||
| 1668 | $js_localization = apply_filters( 'gravityview_js_localization', $js_localization, $views ); |
||
| 1669 | |||
| 1670 | wp_localize_script( 'gravityview-fe-view', 'gvGlobals', $js_localization ); |
||
| 1671 | } |
||
| 1672 | } |
||
| 1673 | 1 | } |
|
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Handle enqueuing the `gravityview_default_style` stylesheet |
||
| 1677 | * |
||
| 1678 | * @since 1.17 |
||
| 1679 | * |
||
| 1680 | * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet |
||
| 1681 | * |
||
| 1682 | * @return void |
||
| 1683 | */ |
||
| 1684 | private function enqueue_default_style( $css_dependencies = array() ) { |
||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * @filter `gravityview_use_legacy_search_css` Should GravityView use the legacy Search Bar stylesheet (from before Version 1.17)? |
||
| 1688 | * @since 1.17 |
||
| 1689 | * @param bool $use_legacy_search_style If true, loads `gv-legacy-search(-rtl).css`. If false, loads `gv-default-styles(-rtl).css`. `-rtl` is added on RTL websites. Default: `false` |
||
| 1690 | */ |
||
| 1691 | $use_legacy_search_style = apply_filters( 'gravityview_use_legacy_search_style', false ); |
||
| 1692 | |||
| 1693 | $rtl = is_rtl() ? '-rtl' : ''; |
||
| 1694 | |||
| 1695 | $css_file_base = $use_legacy_search_style ? 'gv-legacy-search' : 'gv-default-styles'; |
||
| 1696 | |||
| 1697 | $path = gravityview_css_url( $css_file_base . $rtl . '.css' ); |
||
| 1698 | |||
| 1699 | wp_enqueue_style( 'gravityview_default_style', $path, $css_dependencies, GravityView_Plugin::version, 'all' ); |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Add template extra style if exists |
||
| 1704 | * @param string $template_id |
||
| 1705 | */ |
||
| 1706 | public static function add_style( $template_id ) { |
||
| 1707 | |||
| 1708 | if ( ! empty( $template_id ) && wp_style_is( 'gravityview_style_' . $template_id, 'registered' ) ) { |
||
| 1709 | do_action( 'gravityview_log_debug', sprintf( '[add_style] Adding extra template style for %s', $template_id ) ); |
||
| 1710 | wp_enqueue_style( 'gravityview_style_' . $template_id ); |
||
| 1711 | } elseif ( empty( $template_id ) ) { |
||
| 1712 | do_action( 'gravityview_log_error', '[add_style] Cannot add template style; template_id is empty' ); |
||
| 1713 | } else { |
||
| 1714 | do_action( 'gravityview_log_error', sprintf( '[add_style] Cannot add template style; %s is not registered', 'gravityview_style_'.$template_id ) ); |
||
| 1715 | } |
||
| 1716 | |||
| 1717 | } |
||
| 1718 | |||
| 1719 | |||
| 1720 | /** |
||
| 1721 | * Inject the sorting links on the table columns |
||
| 1722 | * |
||
| 1723 | * Callback function for hook 'gravityview/template/field_label' |
||
| 1724 | * @see GravityView_API::field_label() (in includes/class-api.php) |
||
| 1725 | * |
||
| 1726 | * @since 1.7 |
||
| 1727 | * |
||
| 1728 | * @param string $label Field label |
||
| 1729 | * @param array $field Field settings |
||
| 1730 | * |
||
| 1731 | * @return string Field Label |
||
| 1732 | */ |
||
| 1733 | public function add_columns_sort_links( $label = '', $field, $form ) { |
||
| 1734 | |||
| 1735 | /** |
||
| 1736 | * Not a table-based template; don't add sort icons |
||
| 1737 | * @since 1.12 |
||
| 1738 | */ |
||
| 1739 | if( ! preg_match( '/table/ism', GravityView_View::getInstance()->getTemplatePartSlug() ) ) { |
||
| 1740 | return $label; |
||
| 1741 | } |
||
| 1742 | |||
| 1743 | if ( ! $this->is_field_sortable( $field['id'], $form ) ) { |
||
| 1744 | return $label; |
||
| 1745 | } |
||
| 1746 | |||
| 1747 | $sorting = GravityView_View::getInstance()->getSorting(); |
||
| 1748 | |||
| 1749 | $class = 'gv-sort'; |
||
| 1750 | |||
| 1751 | $sort_field_id = self::_override_sorting_id_by_field_type( $field['id'], $form['id'] ); |
||
| 1752 | |||
| 1753 | $sort_args = array( |
||
| 1754 | 'sort' => $field['id'], |
||
| 1755 | 'dir' => 'asc', |
||
| 1756 | ); |
||
| 1757 | |||
| 1758 | if ( ! empty( $sorting['key'] ) && (string) $sort_field_id === (string) $sorting['key'] ) { |
||
| 1759 | //toggle sorting direction. |
||
| 1760 | if ( 'asc' === $sorting['direction'] ) { |
||
| 1761 | $sort_args['dir'] = 'desc'; |
||
| 1762 | $class .= ' gv-icon-sort-desc'; |
||
| 1763 | } else { |
||
| 1764 | $sort_args['dir'] = 'asc'; |
||
| 1765 | $class .= ' gv-icon-sort-asc'; |
||
| 1766 | } |
||
| 1767 | } else { |
||
| 1768 | $class .= ' gv-icon-caret-up-down'; |
||
| 1769 | } |
||
| 1770 | |||
| 1771 | $url = add_query_arg( $sort_args, remove_query_arg( array('pagenum') ) ); |
||
| 1772 | |||
| 1773 | return '<a href="'. esc_url_raw( $url ) .'" class="'. $class .'" ></a> '. $label; |
||
| 1774 | |||
| 1775 | } |
||
| 1776 | |||
| 1777 | /** |
||
| 1778 | * Checks if field (column) is sortable |
||
| 1779 | * |
||
| 1780 | * @param string $field Field settings |
||
| 1781 | * @param array $form Gravity Forms form array |
||
| 1782 | * |
||
| 1783 | * @since 1.7 |
||
| 1784 | * |
||
| 1785 | * @return bool True: Yes, field is sortable; False: not sortable |
||
| 1786 | */ |
||
| 1787 | public function is_field_sortable( $field_id = '', $form = array() ) { |
||
| 1788 | |||
| 1789 | $field_type = $field_id; |
||
| 1790 | |||
| 1791 | if( is_numeric( $field_id ) ) { |
||
| 1792 | $field = GFFormsModel::get_field( $form, $field_id ); |
||
| 1793 | $field_type = $field->type; |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | $not_sortable = array( |
||
| 1797 | 'edit_link', |
||
| 1798 | 'delete_link', |
||
| 1799 | ); |
||
| 1800 | |||
| 1801 | /** |
||
| 1802 | * @filter `gravityview/sortable/field_blacklist` Modify what fields should never be sortable. |
||
| 1803 | * @since 1.7 |
||
| 1804 | * @param[in,out] array $not_sortable Array of field types that aren't sortable |
||
| 1805 | * @param string $field_type Field type to check whether the field is sortable |
||
| 1806 | * @param array $form Gravity Forms form |
||
| 1807 | */ |
||
| 1808 | $not_sortable = apply_filters( 'gravityview/sortable/field_blacklist', $not_sortable, $field_type, $form ); |
||
| 1809 | |||
| 1810 | if ( in_array( $field_type, $not_sortable ) ) { |
||
| 1811 | return false; |
||
| 1812 | } |
||
| 1813 | |||
| 1814 | return apply_filters( "gravityview/sortable/formfield_{$form['id']}_{$field_id}", apply_filters( "gravityview/sortable/field_{$field_id}", true, $form ) ); |
||
| 1815 | |||
| 1816 | } |
||
| 1817 | |||
| 1818 | } |
||
| 1819 | |||
| 1820 | GravityView_frontend::getInstance(); |
||
| 1821 | |||
| 1822 | |||
| 1823 | |||
| 1824 |