Complex classes like GravityView_frontend often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GravityView_frontend, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 | 39 | private function initialize() { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get the one true instantiated self |
||
| 106 | * @return GravityView_frontend |
||
| 107 | */ |
||
| 108 | 42 | public static function getInstance() { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return GravityView_View_Data |
||
| 120 | */ |
||
| 121 | 40 | public function getGvOutputData() { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param \GravityView_View_Data $gv_output_data |
||
| 127 | */ |
||
| 128 | 40 | public function setGvOutputData( $gv_output_data ) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @return boolean |
||
| 134 | */ |
||
| 135 | 39 | public function isSearch() { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param boolean $is_search |
||
| 141 | */ |
||
| 142 | 40 | public function setIsSearch( $is_search ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return bool|int |
||
| 148 | */ |
||
| 149 | 41 | public function getSingleEntry() { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Sets the single entry ID and also the entry |
||
| 155 | * @param bool|int|string $single_entry |
||
| 156 | */ |
||
| 157 | 39 | public function setSingleEntry( $single_entry ) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | 39 | public function getEntry() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set the current entry |
||
| 172 | * @param array|int $entry Entry array or entry slug or ID |
||
| 173 | */ |
||
| 174 | 39 | public function setEntry( $entry ) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | 40 | public function getPostId() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param int $post_id |
||
| 192 | */ |
||
| 193 | 40 | public function setPostId( $post_id ) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | 41 | public function isPostHasShortcode() { |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param boolean $post_has_shortcode |
||
| 206 | */ |
||
| 207 | 40 | public function setPostHasShortcode( $post_has_shortcode ) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | 42 | public function isGravityviewPostType() { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param boolean $is_gravityview_post_type |
||
| 220 | */ |
||
| 221 | 40 | public function setIsGravityviewPostType( $is_gravityview_post_type ) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Set the context view ID used when page contains multiple embedded views or displaying the single entry view |
||
| 227 | * |
||
| 228 | * |
||
| 229 | * |
||
| 230 | * @param null $view_id |
||
| 231 | */ |
||
| 232 | 3 | public function set_context_view_id( $view_id = null ) { |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Returns the the view_id context when page contains multiple embedded views or displaying single entry view |
||
| 256 | * |
||
| 257 | * @since 1.5.4 |
||
| 258 | * |
||
| 259 | * @return int|null |
||
| 260 | */ |
||
| 261 | 39 | public function get_context_view_id() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Allow GravityView entry endpoints on the front page of a site |
||
| 267 | * |
||
| 268 | * @link https://core.trac.wordpress.org/ticket/23867 Fixes this core issue |
||
| 269 | * @link https://wordpress.org/plugins/cpt-on-front-page/ Code is based on this |
||
| 270 | * |
||
| 271 | * @since 1.17.3 |
||
| 272 | * |
||
| 273 | * @param WP_Query &$query (passed by reference) |
||
| 274 | * |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | 188 | public function parse_query_fix_frontpage( &$query ) { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Read the $post and process the View data inside |
||
| 334 | * @param array $wp Passed in the `wp` hook. Not used. |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | 2 | public function parse_content( $wp = array() ) { |
|
| 338 | 2 | global $post; |
|
| 339 | |||
| 340 | // If in admin and NOT AJAX request, get outta here. |
||
| 341 | 2 | if ( gravityview()->request->is_admin() ) { |
|
| 342 | return; |
||
| 343 | } |
||
| 344 | |||
| 345 | // Calculate requested Views |
||
| 346 | 2 | $this->setGvOutputData( GravityView_View_Data::getInstance( $post ) ); |
|
| 347 | |||
| 348 | // !important: we need to run this before getting single entry (to kick the advanced filter) |
||
| 349 | 2 | $this->set_context_view_id(); |
|
| 350 | |||
| 351 | 2 | $this->setIsGravityviewPostType( get_post_type( $post ) === 'gravityview' ); |
|
| 352 | |||
| 353 | 2 | $post_id = $this->getPostId() ? $this->getPostId() : (isset( $post ) ? $post->ID : null ); |
|
| 354 | 2 | $this->setPostId( $post_id ); |
|
| 355 | 2 | $post_has_shortcode = ! empty( $post->post_content ) ? gravityview_has_shortcode_r( $post->post_content, 'gravityview' ) : false; |
|
| 356 | 2 | $this->setPostHasShortcode( $this->isGravityviewPostType() ? null : ! empty( $post_has_shortcode ) ); |
|
| 357 | |||
| 358 | // check if the View is showing search results (only for multiple entries View) |
||
| 359 | 2 | $this->setIsSearch( $this->is_searching() ); |
|
| 360 | |||
| 361 | 2 | unset( $entry, $post_id, $post_has_shortcode ); |
|
| 362 | 2 | } |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Set the entry |
||
| 366 | */ |
||
| 367 | function set_entry_data() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Checks if the current View is presenting search results |
||
| 375 | * |
||
| 376 | * @since 1.5.4 |
||
| 377 | * |
||
| 378 | * @return boolean True: Yes, it's a search; False: No, not a search. |
||
| 379 | */ |
||
| 380 | 2 | function is_searching() { |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Filter the title for the single entry view |
||
| 425 | * |
||
| 426 | * @param string $passed_title Current title |
||
| 427 | * @param int $passed_post_id Post ID |
||
| 428 | * @return string (modified) title |
||
| 429 | */ |
||
| 430 | 9 | public function single_entry_title( $passed_title, $passed_post_id = null ) { |
|
| 431 | 9 | global $post; |
|
| 432 | |||
| 433 | // Since this is a public method, it can be called outside of the plugin. Don't assume things have been loaded properly. |
||
| 434 | 9 | if ( ! class_exists( '\GV\Entry' ) ) { |
|
| 435 | return $passed_title; |
||
| 436 | } |
||
| 437 | |||
| 438 | 9 | $gventry = gravityview()->request->is_entry(); |
|
| 439 | |||
| 440 | // If this is the directory view, return. |
||
| 441 | 9 | if( ! $gventry ) { |
|
| 442 | 8 | return $passed_title; |
|
| 443 | } |
||
| 444 | |||
| 445 | 1 | $entry = $gventry->as_entry(); |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @filter `gravityview/single/title/out_loop` Apply the Single Entry Title filter outside the WordPress loop? |
||
| 449 | * @param boolean $in_the_loop Whether to apply the filter to the menu title and the meta tag <title> - outside the loop |
||
| 450 | * @param array $entry Current entry |
||
| 451 | */ |
||
| 452 | 1 | $apply_outside_loop = apply_filters( 'gravityview/single/title/out_loop', in_the_loop(), $entry ); |
|
| 453 | |||
| 454 | 1 | if ( ! $apply_outside_loop ) { |
|
| 455 | 1 | return $passed_title; |
|
| 456 | } |
||
| 457 | |||
| 458 | // WooCommerce doesn't $post_id |
||
| 459 | 1 | if ( empty( $passed_post_id ) ) { |
|
| 460 | 1 | return $passed_title; |
|
| 461 | } |
||
| 462 | |||
| 463 | // Don't modify the title for anything other than the current view/post. |
||
| 464 | // This is true for embedded shortcodes and Views. |
||
| 465 | 1 | if ( is_object( $post ) && (int) $post->ID !== (int) $passed_post_id ) { |
|
| 466 | 1 | return $passed_title; |
|
| 467 | } |
||
| 468 | |||
| 469 | 1 | $view = gravityview()->request->is_view(); |
|
| 470 | |||
| 471 | 1 | if( $view ) { |
|
| 472 | 1 | return $this->_get_single_entry_title( $view, $entry, $passed_title ); |
|
| 473 | } |
||
| 474 | |||
| 475 | 1 | $_gvid = \GV\Utils::_GET( 'gvid', null ); |
|
| 476 | |||
| 477 | // $_GET['gvid'] is set; we know what View to render |
||
| 478 | 1 | if ( $_gvid ) { |
|
| 479 | |||
| 480 | 1 | $view = \GV\View::by_id( $_gvid ); |
|
| 481 | |||
| 482 | 1 | return $this->_get_single_entry_title( $view, $entry, $passed_title ); |
|
| 483 | } |
||
| 484 | |||
| 485 | 1 | global $post; |
|
| 486 | |||
| 487 | 1 | if ( ! $post ) { |
|
| 488 | return $passed_title; |
||
| 489 | } |
||
| 490 | |||
| 491 | 1 | $view_collection = \GV\View_Collection::from_post( $post ); |
|
| 492 | |||
| 493 | // We have multiple Views, but no gvid...this isn't valid security |
||
| 494 | 1 | if( 1 < $view_collection->count() ) { |
|
| 495 | 1 | return $passed_title; |
|
| 496 | } |
||
| 497 | |||
| 498 | return $this->_get_single_entry_title( $view_collection->first(), $entry, $passed_title ); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns the single entry title for a View with variables replaced and shortcodes parsed |
||
| 503 | * |
||
| 504 | * @since 2.7.2 |
||
| 505 | * |
||
| 506 | * @param \GV\View|null $view |
||
| 507 | * @param array $entry |
||
| 508 | * @param string $passed_title |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | private function _get_single_entry_title( $view, $entry = array(), $passed_title = '' ) { |
||
| 513 | |||
| 514 | if ( ! $view ) { |
||
| 515 | return $passed_title; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @filter `gravityview/single/title/check_entry_display` Override whether to check entry display rules against filters |
||
| 520 | * @internal This might change in the future! Don't rely on it. |
||
| 521 | * @since 2.7.2 |
||
| 522 | * @param bool $check_entry_display Check whether the entry is visible for the current View configuration. Default: true. |
||
| 523 | * @param array $entry Gravity Forms entry array |
||
| 524 | * @param \GV\View $view The View |
||
| 525 | */ |
||
| 526 | $check_entry_display = apply_filters( 'gravityview/single/title/check_entry_display', true, $entry, $view ); |
||
| 527 | |||
| 528 | if( $check_entry_display ) { |
||
| 529 | |||
| 530 | $check_display = GVCommon::check_entry_display( $entry, $view ); |
||
| 531 | |||
| 532 | if( is_wp_error( $check_display ) ) { |
||
| 533 | return $passed_title; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | $title = $view->settings->get( 'single_title', $passed_title ); |
||
| 538 | |||
| 539 | $form = GVCommon::get_form( $entry['form_id'] ); |
||
| 540 | |||
| 541 | // We are allowing HTML in the fields, so no escaping the output |
||
| 542 | $title = GravityView_API::replace_variables( $title, $form, $entry ); |
||
| 543 | |||
| 544 | $title = do_shortcode( $title ); |
||
| 545 | |||
| 546 | return $title; |
||
| 547 | } |
||
| 548 | |||
| 549 | |||
| 550 | /** |
||
| 551 | * In case View post is called directly, insert the view in the post content |
||
| 552 | * |
||
| 553 | * @deprecated Use \GV\View::content() instead. |
||
| 554 | * |
||
| 555 | * @static |
||
| 556 | * @param mixed $content |
||
| 557 | * @return string Add the View output into View CPT content |
||
| 558 | */ |
||
| 559 | 4 | public function insert_view_in_content( $content ) { |
|
| 560 | 4 | gravityview()->log->notice( '\GravityView_frontend::insert_view_in_content is deprecated. Use \GV\View::content()' ); |
|
| 561 | 4 | return \GV\View::content( $content ); |
|
| 562 | } |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Disable comments on GravityView post types |
||
| 566 | * @param boolean $open existing status |
||
| 567 | * @param int $post_id Post ID |
||
| 568 | * @return boolean |
||
| 569 | */ |
||
| 570 | public function comments_open( $open, $post_id ) { |
||
| 571 | |||
| 572 | if ( $this->isGravityviewPostType() ) { |
||
| 573 | $open = false; |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @filter `gravityview/comments_open` Whether to set comments to open or closed. |
||
| 578 | * @since 1.5.4 |
||
| 579 | * @param boolean $open Open or closed status |
||
| 580 | * @param int $post_id Post ID to set comment status for |
||
| 581 | */ |
||
| 582 | $open = apply_filters( 'gravityview/comments_open', $open, $post_id ); |
||
| 583 | |||
| 584 | return $open; |
||
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Display a warning when a View has not been configured |
||
| 589 | * |
||
| 590 | * @since 1.19.2 |
||
| 591 | * |
||
| 592 | * @param int $view_id The ID of the View currently being displayed |
||
| 593 | * |
||
| 594 | * @return void |
||
| 595 | */ |
||
| 596 | 2 | public function context_not_configured_warning( $view_id = 0 ) { |
|
| 597 | |||
| 598 | 2 | if ( ! class_exists( 'GravityView_View' ) ) { |
|
| 599 | return; |
||
| 600 | } |
||
| 601 | |||
| 602 | 2 | $fields = GravityView_View::getInstance()->getContextFields(); |
|
| 603 | |||
| 604 | 2 | if ( ! empty( $fields ) ) { |
|
| 605 | 2 | return; |
|
| 606 | } |
||
| 607 | |||
| 608 | $context = GravityView_View::getInstance()->getContext(); |
||
| 609 | |||
| 610 | switch( $context ) { |
||
| 611 | case 'directory': |
||
| 612 | $tab = __( 'Multiple Entries', 'gravityview' ); |
||
| 613 | break; |
||
| 614 | case 'edit': |
||
| 615 | $tab = __( 'Edit Entry', 'gravityview' ); |
||
| 616 | break; |
||
| 617 | case 'single': |
||
| 618 | default: |
||
| 619 | $tab = __( 'Single Entry', 'gravityview' ); |
||
| 620 | break; |
||
| 621 | } |
||
| 622 | |||
| 623 | |||
| 624 | $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 ); |
||
| 625 | $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $view_id, $context ) ); |
||
| 626 | $action_text = sprintf( esc_html__('Add fields to %s', 'gravityview' ), $tab ); |
||
| 627 | $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' ); |
||
| 628 | |||
| 629 | $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 ) ) ); |
||
| 630 | $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message ); |
||
| 631 | |||
| 632 | echo GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $view_id ); |
||
| 633 | } |
||
| 634 | |||
| 635 | |||
| 636 | /** |
||
| 637 | * Core function to render a View based on a set of arguments |
||
| 638 | * |
||
| 639 | * @static |
||
| 640 | * @param array $passed_args { |
||
| 641 | * |
||
| 642 | * Settings for rendering the View |
||
| 643 | * |
||
| 644 | * @type int $id View id |
||
| 645 | * @type int $page_size Number of entries to show per page |
||
| 646 | * @type string $sort_field Form field id to sort |
||
| 647 | * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND') |
||
| 648 | * @type string $start_date - Ymd |
||
| 649 | * @type string $end_date - Ymd |
||
| 650 | * @type string $class - assign a html class to the view |
||
| 651 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 652 | * } |
||
| 653 | * |
||
| 654 | * @deprecated Use \GV\View_Renderer |
||
| 655 | * |
||
| 656 | * @return string|null HTML output of a View, NULL if View isn't found |
||
| 657 | */ |
||
| 658 | 1 | public function render_view( $passed_args ) { |
|
| 659 | 1 | gravityview()->log->notice( '\GravityView_frontend::render_view is deprecated. Use \GV\View_Renderer etc.' ); |
|
| 660 | |||
| 661 | /** |
||
| 662 | * We can use a shortcode here, since it's pretty much the same. |
||
| 663 | * |
||
| 664 | * But we do need to check embed permissions, since shortcodes don't do this. |
||
| 665 | */ |
||
| 666 | |||
| 667 | 1 | if ( ! $view = gravityview()->views->get( $passed_args ) ) { |
|
| 668 | return null; |
||
| 669 | } |
||
| 670 | |||
| 671 | 1 | $view->settings->update( $passed_args ); |
|
| 672 | |||
| 673 | 1 | $direct_access = apply_filters( 'gravityview_direct_access', true, $view->ID ); |
|
| 674 | 1 | $embed_only = $view->settings->get( 'embed_only' ); |
|
| 675 | |||
| 676 | 1 | if( ! $direct_access || ( $embed_only && ! GVCommon::has_cap( 'read_private_gravityviews' ) ) ) { |
|
| 677 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' ); |
|
| 678 | } |
||
| 679 | |||
| 680 | 1 | $shortcode = new \GV\Shortcodes\gravityview(); |
|
| 681 | 1 | return $shortcode->callback( $passed_args ); |
|
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Process the start and end dates for a view - overrides values defined in shortcode (if needed) |
||
| 686 | * |
||
| 687 | * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), |
||
| 688 | * which uses \DateTime() format. |
||
| 689 | * |
||
| 690 | * 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()}, |
||
| 691 | * including strings like "now" or "-1 year" or "-3 days". |
||
| 692 | * |
||
| 693 | * @see GFFormsModel::get_date_range_where |
||
| 694 | * |
||
| 695 | * @param array $args View settings |
||
| 696 | * @param array $search_criteria Search being performed, if any |
||
| 697 | * @return array Modified `$search_criteria` array |
||
| 698 | */ |
||
| 699 | 77 | public static function process_search_dates( $args, $search_criteria = array() ) { |
|
| 700 | |||
| 701 | 77 | $return_search_criteria = $search_criteria; |
|
| 702 | |||
| 703 | 77 | foreach ( array( 'start_date', 'end_date' ) as $key ) { |
|
| 704 | |||
| 705 | |||
| 706 | // Is the start date or end date set in the view or shortcode? |
||
| 707 | // If so, we want to make sure that the search doesn't go outside the bounds defined. |
||
| 708 | 77 | if ( ! empty( $args[ $key ] ) ) { |
|
| 709 | |||
| 710 | // Get a timestamp and see if it's a valid date format |
||
| 711 | 2 | $date = strtotime( $args[ $key ] ); |
|
| 712 | |||
| 713 | // The date was invalid |
||
| 714 | 2 | if ( empty( $date ) ) { |
|
| 715 | gravityview()->log->error( ' Invalid {key} date format: {format}', array( 'key' => $key, 'format' => $args[ $key ] ) ); |
||
| 716 | continue; |
||
| 717 | } |
||
| 718 | |||
| 719 | // The format that Gravity Forms expects for start_date and day-specific (not hour/second-specific) end_date |
||
| 720 | 2 | $datetime_format = 'Y-m-d H:i:s'; |
|
| 721 | 2 | $search_is_outside_view_bounds = false; |
|
| 722 | |||
| 723 | 2 | if( ! empty( $search_criteria[ $key ] ) ) { |
|
| 724 | |||
| 725 | 1 | $search_date = strtotime( $search_criteria[ $key ] ); |
|
| 726 | |||
| 727 | // The search is for entries before the start date defined by the settings |
||
| 728 | 1 | switch ( $key ) { |
|
| 729 | 1 | case 'end_date': |
|
| 730 | /** |
||
| 731 | * If the end date is formatted as 'Y-m-d', it should be formatted without hours and seconds |
||
| 732 | * so that Gravity Forms can convert the day to 23:59:59 the previous day. |
||
| 733 | * |
||
| 734 | * If it's a relative date ("now" or "-1 day"), then it should use the precise date format |
||
| 735 | * |
||
| 736 | * @see GFFormsModel::get_date_range_where |
||
| 737 | */ |
||
| 738 | 1 | $datetime_format = gravityview_is_valid_datetime( $args[ $key ] ) ? 'Y-m-d' : 'Y-m-d H:i:s'; |
|
| 739 | 1 | $search_is_outside_view_bounds = ( $search_date > $date ); |
|
| 740 | 1 | break; |
|
| 741 | 1 | case 'start_date': |
|
| 742 | 1 | $search_is_outside_view_bounds = ( $search_date < $date ); |
|
| 743 | 1 | break; |
|
| 744 | } |
||
| 745 | } |
||
| 746 | |||
| 747 | // If there is no search being performed, or if there is a search being performed that's outside the bounds |
||
| 748 | 2 | if ( empty( $search_criteria[ $key ] ) || $search_is_outside_view_bounds ) { |
|
| 749 | |||
| 750 | // Then we override the search and re-set the start date |
||
| 751 | 2 | $return_search_criteria[ $key ] = date_i18n( $datetime_format , $date, true ); |
|
| 752 | } |
||
| 753 | } |
||
| 754 | } |
||
| 755 | |||
| 756 | 77 | if( isset( $return_search_criteria['start_date'] ) && isset( $return_search_criteria['end_date'] ) ) { |
|
| 757 | // The start date is AFTER the end date. This will result in no results, but let's not force the issue. |
||
| 758 | 2 | if ( strtotime( $return_search_criteria['start_date'] ) > strtotime( $return_search_criteria['end_date'] ) ) { |
|
| 759 | 1 | gravityview()->log->error( 'Invalid search: the start date is after the end date.', array( 'data' => $return_search_criteria ) ); |
|
| 760 | } |
||
| 761 | } |
||
| 762 | |||
| 763 | 77 | return $return_search_criteria; |
|
| 764 | } |
||
| 765 | |||
| 766 | |||
| 767 | /** |
||
| 768 | * Process the approved only search criteria according to the View settings |
||
| 769 | * |
||
| 770 | * @param array $args View settings |
||
| 771 | * @param array $search_criteria Search being performed, if any |
||
| 772 | * @return array Modified `$search_criteria` array |
||
| 773 | */ |
||
| 774 | 76 | public static function process_search_only_approved( $args, $search_criteria ) { |
|
| 775 | |||
| 776 | /** @since 1.19 */ |
||
| 777 | 76 | if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) { |
|
| 778 | gravityview()->log->debug( 'User can moderate entries; showing all approval statuses' ); |
||
| 779 | return $search_criteria; |
||
| 780 | } |
||
| 781 | |||
| 782 | 76 | if ( ! empty( $args['show_only_approved'] ) ) { |
|
| 783 | |||
| 784 | 18 | $search_criteria['field_filters'][] = array( |
|
| 785 | 'key' => GravityView_Entry_Approval::meta_key, |
||
| 786 | 'value' => GravityView_Entry_Approval_Status::APPROVED |
||
| 787 | ); |
||
| 788 | |||
| 789 | 18 | $search_criteria['field_filters']['mode'] = 'all'; // force all the criterias to be met |
|
| 790 | |||
| 791 | 18 | gravityview()->log->debug( '[process_search_only_approved] Search Criteria if show only approved: ', array( 'data' => $search_criteria ) ); |
|
| 792 | } |
||
| 793 | |||
| 794 | 76 | return $search_criteria; |
|
| 795 | } |
||
| 796 | |||
| 797 | |||
| 798 | /** |
||
| 799 | * Check if a certain entry is approved. |
||
| 800 | * |
||
| 801 | * If we pass the View settings ($args) it will check the 'show_only_approved' setting before |
||
| 802 | * checking the entry approved field, returning true if show_only_approved = false. |
||
| 803 | * |
||
| 804 | * @since 1.7 |
||
| 805 | * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved |
||
| 806 | * |
||
| 807 | * @uses GravityView_Entry_Approval_Status::is_approved |
||
| 808 | * |
||
| 809 | * @param array $entry Entry object |
||
| 810 | * @param array $args View settings (optional) |
||
| 811 | * |
||
| 812 | * @return bool |
||
| 813 | */ |
||
| 814 | public static function is_entry_approved( $entry, $args = array() ) { |
||
| 815 | |||
| 816 | if ( empty( $entry['id'] ) || ( array_key_exists( 'show_only_approved', $args ) && ! $args['show_only_approved'] ) ) { |
||
| 817 | // is implicitly approved if entry is null or View settings doesn't require to check for approval |
||
| 818 | return true; |
||
| 819 | } |
||
| 820 | |||
| 821 | /** @since 1.19 */ |
||
| 822 | if( ! empty( $args['admin_show_all_statuses'] ) && GVCommon::has_cap('gravityview_moderate_entries') ) { |
||
| 823 | gravityview()->log->debug( 'User can moderate entries, so entry is approved for viewing' ); |
||
| 824 | return true; |
||
| 825 | } |
||
| 826 | |||
| 827 | $is_approved = gform_get_meta( $entry['id'], GravityView_Entry_Approval::meta_key ); |
||
| 828 | |||
| 829 | return GravityView_Entry_Approval_Status::is_approved( $is_approved ); |
||
| 830 | } |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Parse search criteria for a entries search. |
||
| 834 | * |
||
| 835 | * array( |
||
| 836 | * 'search_field' => 1, // ID of the field |
||
| 837 | * 'search_value' => '', // Value of the field to search |
||
| 838 | * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains' |
||
| 839 | * 'show_only_approved' => 0 or 1 // Boolean |
||
| 840 | * ) |
||
| 841 | * |
||
| 842 | * @param array $args Array of args |
||
| 843 | * @param int $form_id Gravity Forms form ID |
||
| 844 | * @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. |
||
| 845 | */ |
||
| 846 | 77 | public static function get_search_criteria( $args, $form_id ) { |
|
| 847 | /** |
||
| 848 | * Compatibility with filters hooking in `gravityview_search_criteria` instead of `gravityview_fe_search_criteria`. |
||
| 849 | */ |
||
| 850 | 77 | $criteria = apply_filters( 'gravityview_search_criteria', array(), array( $form_id ), \GV\Utils::get( $args, 'id' ) ); |
|
| 851 | 77 | $search_criteria = isset( $criteria['search_criteria'] ) ? $criteria['search_criteria'] : array( 'field_filters' => array() ); |
|
| 852 | |||
| 853 | /** |
||
| 854 | * @filter `gravityview_fe_search_criteria` Modify the search criteria |
||
| 855 | * @see GravityView_Widget_Search::filter_entries Adds the default search criteria |
||
| 856 | * @param array $search_criteria Empty `field_filters` key |
||
| 857 | * @param int $form_id ID of the Gravity Forms form that is being searched |
||
| 858 | * @param array $args The View settings. |
||
| 859 | */ |
||
| 860 | 77 | $search_criteria = apply_filters( 'gravityview_fe_search_criteria', $search_criteria, $form_id, $args ); |
|
| 861 | |||
| 862 | 77 | if ( ! is_array( $search_criteria ) ) { |
|
| 863 | return array(); |
||
| 864 | } |
||
| 865 | |||
| 866 | 77 | $original_search_criteria = $search_criteria; |
|
| 867 | |||
| 868 | 77 | gravityview()->log->debug( '[get_search_criteria] Search Criteria after hook gravityview_fe_search_criteria: ', array( 'data' =>$search_criteria ) ); |
|
| 869 | |||
| 870 | // implicity search |
||
| 871 | 77 | if ( ! empty( $args['search_value'] ) ) { |
|
| 872 | |||
| 873 | // Search operator options. Options: `is` or `contains` |
||
| 874 | 3 | $operator = ! empty( $args['search_operator'] ) && in_array( $args['search_operator'], array( 'is', 'isnot', '>', '<', 'contains' ) ) ? $args['search_operator'] : 'contains'; |
|
| 875 | |||
| 876 | 3 | $search_criteria['field_filters'][] = array( |
|
| 877 | 3 | 'key' => \GV\Utils::_GET( 'search_field', \GV\Utils::get( $args, 'search_field' ) ), // The field ID to search |
|
| 878 | 3 | 'value' => _wp_specialchars( $args['search_value'] ), // The value to search. Encode ampersands but not quotes. |
|
| 879 | 3 | 'operator' => $operator, |
|
| 880 | ); |
||
| 881 | |||
| 882 | // Lock search mode to "all" with implicit presearch filter. |
||
| 883 | 3 | $search_criteria['field_filters']['mode'] = 'all'; |
|
| 884 | } |
||
| 885 | |||
| 886 | 77 | if( $search_criteria !== $original_search_criteria ) { |
|
| 887 | 3 | gravityview()->log->debug( '[get_search_criteria] Search Criteria after implicity search: ', array( 'data' => $search_criteria ) ); |
|
| 888 | } |
||
| 889 | |||
| 890 | // Handle setting date range |
||
| 891 | 77 | $search_criteria = self::process_search_dates( $args, $search_criteria ); |
|
| 892 | |||
| 893 | 77 | if( $search_criteria !== $original_search_criteria ) { |
|
| 894 | 4 | gravityview()->log->debug( '[get_search_criteria] Search Criteria after date params: ', array( 'data' => $search_criteria ) ); |
|
| 895 | } |
||
| 896 | |||
| 897 | // remove not approved entries |
||
| 898 | 77 | $search_criteria = self::process_search_only_approved( $args, $search_criteria ); |
|
| 899 | |||
| 900 | /** |
||
| 901 | * @filter `gravityview_status` Modify entry status requirements to be included in search results. |
||
| 902 | * @param string $status Default: `active`. Accepts all Gravity Forms entry statuses, including `spam` and `trash` |
||
| 903 | */ |
||
| 904 | 77 | $search_criteria['status'] = apply_filters( 'gravityview_status', 'active', $args ); |
|
| 905 | |||
| 906 | 77 | return $search_criteria; |
|
| 907 | } |
||
| 908 | |||
| 909 | |||
| 910 | |||
| 911 | /** |
||
| 912 | * Core function to calculate View multi entries (directory) based on a set of arguments ($args): |
||
| 913 | * $id - View id |
||
| 914 | * $page_size - Page |
||
| 915 | * $sort_field - form field id to sort |
||
| 916 | * $sort_direction - ASC / DESC |
||
| 917 | * $start_date - Ymd |
||
| 918 | * $end_date - Ymd |
||
| 919 | * $class - assign a html class to the view |
||
| 920 | * $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 921 | * |
||
| 922 | * |
||
| 923 | * |
||
| 924 | * @uses gravityview_get_entries() |
||
| 925 | * @param array $args\n |
||
| 926 | * - $id - View id |
||
| 927 | * - $page_size - Page |
||
| 928 | * - $sort_field - form field id to sort |
||
| 929 | * - $sort_direction - ASC / DESC |
||
| 930 | * - $start_date - Ymd |
||
| 931 | * - $end_date - Ymd |
||
| 932 | * - $class - assign a html class to the view |
||
| 933 | * - $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 934 | * @param int $form_id Gravity Forms Form ID |
||
| 935 | * @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 |
||
| 936 | */ |
||
| 937 | 1 | public static function get_view_entries( $args, $form_id ) { |
|
| 938 | |||
| 939 | 1 | gravityview()->log->debug( '[get_view_entries] init' ); |
|
| 940 | // start filters and sorting |
||
| 941 | |||
| 942 | 1 | $parameters = self::get_view_entries_parameters( $args, $form_id ); |
|
| 943 | |||
| 944 | 1 | $count = 0; // Must be defined so that gravityview_get_entries can use by reference |
|
| 945 | |||
| 946 | // fetch entries |
||
| 947 | list( $entries, $paging, $count ) = |
||
| 948 | 1 | \GV\Mocks\GravityView_frontend_get_view_entries( $args, $form_id, $parameters, $count ); |
|
| 949 | |||
| 950 | 1 | gravityview()->log->debug( 'Get Entries. Found: {count} entries', array( 'count' => $count, 'data' => $entries ) ); |
|
| 951 | |||
| 952 | /** |
||
| 953 | * @filter `gravityview_view_entries` Filter the entries output to the View |
||
| 954 | * @deprecated since 1.5.2 |
||
| 955 | * @param array $args View settings associative array |
||
| 956 | * @var array |
||
| 957 | */ |
||
| 958 | 1 | $entries = apply_filters( 'gravityview_view_entries', $entries, $args ); |
|
| 959 | |||
| 960 | $return = array( |
||
| 961 | 1 | 'count' => $count, |
|
| 962 | 1 | 'entries' => $entries, |
|
| 963 | 1 | 'paging' => $paging, |
|
| 964 | ); |
||
| 965 | |||
| 966 | /** |
||
| 967 | * @filter `gravityview/view/entries` Filter the entries output to the View |
||
| 968 | * @param array $criteria associative array containing count, entries & paging |
||
| 969 | * @param array $args View settings associative array |
||
| 970 | * @since 1.5.2 |
||
| 971 | */ |
||
| 972 | 1 | return apply_filters( 'gravityview/view/entries', $return, $args ); |
|
| 973 | } |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Get an array of search parameters formatted as Gravity Forms requires |
||
| 977 | * |
||
| 978 | * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters |
||
| 979 | * |
||
| 980 | * @uses GravityView_frontend::get_search_criteria |
||
| 981 | * @uses GravityView_frontend::get_search_criteria_paging |
||
| 982 | * |
||
| 983 | * @since 1.20 |
||
| 984 | * |
||
| 985 | * @see \GV\View_Settings::defaults For $args options |
||
| 986 | * |
||
| 987 | * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults |
||
| 988 | * @param int $form_id Gravity Forms form ID to search |
||
| 989 | * |
||
| 990 | * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys |
||
| 991 | */ |
||
| 992 | 76 | public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) { |
|
| 993 | |||
| 994 | |||
| 995 | 76 | if ( ! is_array( $args ) || ! is_numeric( $form_id ) ) { |
|
| 996 | |||
| 997 | gravityview()->log->error( 'Passed args are not an array or the form ID is not numeric' ); |
||
| 998 | |||
| 999 | return array(); |
||
| 1000 | } |
||
| 1001 | |||
| 1002 | 76 | $form_id = intval( $form_id ); |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Process search parameters |
||
| 1006 | * @var array |
||
| 1007 | */ |
||
| 1008 | 76 | $search_criteria = self::get_search_criteria( $args, $form_id ); |
|
| 1009 | |||
| 1010 | 76 | $paging = self::get_search_criteria_paging( $args ); |
|
| 1011 | |||
| 1012 | $parameters = array( |
||
| 1013 | 76 | 'search_criteria' => $search_criteria, |
|
| 1014 | 76 | 'sorting' => self::updateViewSorting( $args, $form_id ), |
|
| 1015 | 76 | 'paging' => $paging, |
|
| 1016 | 76 | 'cache' => isset( $args['cache'] ) ? $args['cache'] : true, |
|
| 1017 | ); |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * @filter `gravityview_get_entries` Filter get entries criteria |
||
| 1021 | * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys. |
||
| 1022 | * @param array $args View configuration args. { |
||
| 1023 | * @type int $id View id |
||
| 1024 | * @type int $page_size Number of entries to show per page |
||
| 1025 | * @type string $sort_field Form field id to sort |
||
| 1026 | * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND') |
||
| 1027 | * @type string $start_date - Ymd |
||
| 1028 | * @type string $end_date - Ymd |
||
| 1029 | * @type string $class - assign a html class to the view |
||
| 1030 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 1031 | * } |
||
| 1032 | * @param int $form_id ID of Gravity Forms form |
||
| 1033 | */ |
||
| 1034 | 76 | $parameters = apply_filters( 'gravityview_get_entries', $parameters, $args, $form_id ); |
|
| 1035 | |||
| 1036 | /** |
||
| 1037 | * @filter `gravityview_get_entries_{View ID}` Filter get entries criteria |
||
| 1038 | * @param array $parameters Array with `search_criteria`, `sorting` and `paging` keys. |
||
| 1039 | * @param array $args View configuration args. |
||
| 1040 | */ |
||
| 1041 | 76 | $parameters = apply_filters( 'gravityview_get_entries_'.\GV\Utils::get( $args, 'id' ), $parameters, $args, $form_id ); |
|
| 1042 | |||
| 1043 | 76 | gravityview()->log->debug( '$parameters passed to gravityview_get_entries(): ', array( 'data' => $parameters ) ); |
|
| 1044 | |||
| 1045 | 76 | return $parameters; |
|
| 1046 | } |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * Get the paging array for the View |
||
| 1050 | * |
||
| 1051 | * @since 1.19.5 |
||
| 1052 | * |
||
| 1053 | * @param $args |
||
| 1054 | * @param int $form_id |
||
| 1055 | */ |
||
| 1056 | 76 | public static function get_search_criteria_paging( $args ) { |
|
| 1057 | |||
| 1058 | /** |
||
| 1059 | * @filter `gravityview_default_page_size` The default number of entries displayed in a View |
||
| 1060 | * @since 1.1.6 |
||
| 1061 | * @param int $default_page_size Default: 25 |
||
| 1062 | */ |
||
| 1063 | 76 | $default_page_size = apply_filters( 'gravityview_default_page_size', 25 ); |
|
| 1064 | |||
| 1065 | // Paging & offset |
||
| 1066 | 76 | $page_size = ! empty( $args['page_size'] ) ? intval( $args['page_size'] ) : $default_page_size; |
|
| 1067 | |||
| 1068 | 76 | if ( -1 === $page_size ) { |
|
| 1069 | 1 | $page_size = PHP_INT_MAX; |
|
| 1070 | } |
||
| 1071 | |||
| 1072 | 76 | $curr_page = empty( $_GET['pagenum'] ) ? 1 : intval( $_GET['pagenum'] ); |
|
| 1073 | 76 | $offset = ( $curr_page - 1 ) * $page_size; |
|
| 1074 | |||
| 1075 | 76 | if ( ! empty( $args['offset'] ) ) { |
|
| 1076 | 1 | $offset += intval( $args['offset'] ); |
|
| 1077 | } |
||
| 1078 | |||
| 1079 | $paging = array( |
||
| 1080 | 76 | 'offset' => $offset, |
|
| 1081 | 76 | 'page_size' => $page_size, |
|
| 1082 | ); |
||
| 1083 | |||
| 1084 | 76 | gravityview()->log->debug( 'Paging: ', array( 'data' => $paging ) ); |
|
| 1085 | |||
| 1086 | 76 | return $paging; |
|
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Updates the View sorting criteria |
||
| 1091 | * |
||
| 1092 | * @since 1.7 |
||
| 1093 | * |
||
| 1094 | * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys |
||
| 1095 | * @param int $form_id The ID of the form used to sort |
||
| 1096 | * @return array $sorting Array with `key`, `direction` and `is_numeric` keys |
||
| 1097 | */ |
||
| 1098 | 76 | public static function updateViewSorting( $args, $form_id ) { |
|
| 1099 | 76 | $sorting = array(); |
|
| 1100 | |||
| 1101 | 76 | $has_values = isset( $_GET['sort'] ); |
|
| 1102 | |||
| 1103 | 76 | if ( $has_values && is_array( $_GET['sort'] ) ) { |
|
| 1104 | 3 | $sorts = array_keys( $_GET['sort'] ); |
|
| 1105 | 3 | $dirs = array_values( $_GET['sort'] ); |
|
| 1106 | |||
| 1107 | 3 | if ( $has_values = array_filter( $dirs ) ) { |
|
| 1108 | 3 | $sort_field_id = end( $sorts ); |
|
| 1109 | 3 | $sort_direction = end( $dirs ); |
|
| 1110 | } |
||
| 1111 | } |
||
| 1112 | |||
| 1113 | 76 | if ( ! isset( $sort_field_id ) ) { |
|
| 1114 | 76 | $sort_field_id = isset( $_GET['sort'] ) ? $_GET['sort'] : \GV\Utils::get( $args, 'sort_field' ); |
|
| 1115 | } |
||
| 1116 | |||
| 1117 | 76 | if ( ! isset( $sort_direction ) ) { |
|
| 1118 | 76 | $sort_direction = isset( $_GET['dir'] ) ? $_GET['dir'] : \GV\Utils::get( $args, 'sort_direction' ); |
|
| 1119 | } |
||
| 1120 | |||
| 1121 | 76 | if ( is_array( $sort_field_id ) ) { |
|
| 1122 | 1 | $sort_field_id = array_pop( $sort_field_id ); |
|
| 1123 | } |
||
| 1124 | |||
| 1125 | 76 | if ( is_array( $sort_direction ) ) { |
|
| 1126 | $sort_direction = array_pop( $sort_direction ); |
||
| 1127 | } |
||
| 1128 | |||
| 1129 | 76 | if ( ! empty( $sort_field_id ) ) { |
|
| 1130 | 6 | if ( is_array( $sort_field_id ) ) { |
|
| 1131 | $sort_direction = array_values( $sort_field_id ); |
||
| 1132 | $sort_field_id = array_keys( $sort_field_id ); |
||
| 1133 | |||
| 1134 | $sort_field_id = reset( $sort_field_id ); |
||
| 1135 | $sort_direction = reset( $sort_direction ); |
||
| 1136 | } |
||
| 1137 | |||
| 1138 | 6 | $sort_field_id = self::_override_sorting_id_by_field_type( $sort_field_id, $form_id ); |
|
| 1139 | $sorting = array( |
||
| 1140 | 6 | 'key' => $sort_field_id, |
|
| 1141 | 6 | 'direction' => strtolower( $sort_direction ), |
|
| 1142 | 6 | 'is_numeric' => GVCommon::is_field_numeric( $form_id, $sort_field_id ) |
|
| 1143 | ); |
||
| 1144 | |||
| 1145 | 6 | if ( 'RAND' === $sort_direction ) { |
|
| 1146 | |||
| 1147 | $form = GFAPI::get_form( $form_id ); |
||
| 1148 | |||
| 1149 | // Get the first GF_Field field ID, set as the key for entry randomization |
||
| 1150 | if ( ! empty( $form['fields'] ) ) { |
||
| 1151 | |||
| 1152 | /** @var GF_Field $field */ |
||
| 1153 | foreach ( $form['fields'] as $field ) { |
||
| 1154 | if ( ! is_a( $field, 'GF_Field' ) ) { |
||
| 1155 | continue; |
||
| 1156 | } |
||
| 1157 | |||
| 1158 | $sorting = array( |
||
| 1159 | 'key' => $field->id, |
||
| 1160 | 'is_numeric' => false, |
||
| 1161 | 'direction' => 'RAND', |
||
| 1162 | ); |
||
| 1163 | |||
| 1164 | break; |
||
| 1165 | } |
||
| 1166 | } |
||
| 1167 | } |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | 76 | GravityView_View::getInstance()->setSorting( $sorting ); |
|
| 1171 | |||
| 1172 | 76 | gravityview()->log->debug( '[updateViewSorting] Sort Criteria : ', array( 'data' => $sorting ) ); |
|
| 1173 | |||
| 1174 | 76 | return $sorting; |
|
| 1175 | |||
| 1176 | } |
||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * Override sorting per field |
||
| 1180 | * |
||
| 1181 | * Currently only modifies sorting ID when sorting by the full name. Sorts by first name. |
||
| 1182 | * Use the `gravityview/sorting/full-name` filter to override. |
||
| 1183 | * |
||
| 1184 | * @todo Filter from GravityView_Field |
||
| 1185 | * @since 1.7.4 |
||
| 1186 | * @internal Hi developer! Although this is public, don't call this method; we're going to replace it. |
||
| 1187 | * |
||
| 1188 | * @param int|string|array $sort_field_id Field used for sorting (`id` or `1.2`), or an array for multisorts |
||
| 1189 | * @param int $form_id GF Form ID |
||
| 1190 | * |
||
| 1191 | * @return string Possibly modified sorting ID |
||
| 1192 | */ |
||
| 1193 | 6 | public static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) { |
|
| 1194 | |||
| 1195 | 6 | if ( is_array( $sort_field_id ) ) { |
|
| 1196 | $modified_ids = array(); |
||
| 1197 | foreach ( $sort_field_id as $_sort_field_id ) { |
||
| 1198 | $modified_ids []= self::_override_sorting_id_by_field_type( $_sort_field_id, $form_id ); |
||
| 1199 | } |
||
| 1200 | return $modified_ids; |
||
| 1201 | } |
||
| 1202 | |||
| 1203 | 6 | $form = gravityview_get_form( $form_id ); |
|
| 1204 | |||
| 1205 | 6 | $sort_field = GFFormsModel::get_field( $form, $sort_field_id ); |
|
| 1206 | |||
| 1207 | 6 | if( ! $sort_field ) { |
|
| 1208 | 1 | return $sort_field_id; |
|
| 1209 | } |
||
| 1210 | |||
| 1211 | 5 | switch ( $sort_field['type'] ) { |
|
| 1212 | |||
| 1213 | 5 | case 'address': |
|
| 1214 | // Sorting by full address |
||
| 1215 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1216 | |||
| 1217 | /** |
||
| 1218 | * Override how to sort when sorting address |
||
| 1219 | * |
||
| 1220 | * @since 1.8 |
||
| 1221 | * |
||
| 1222 | * @param string $address_part `street`, `street2`, `city`, `state`, `zip`, or `country` (default: `city`) |
||
| 1223 | * @param string $sort_field_id Field used for sorting |
||
| 1224 | * @param int $form_id GF Form ID |
||
| 1225 | */ |
||
| 1226 | $address_part = apply_filters( 'gravityview/sorting/address', 'city', $sort_field_id, $form_id ); |
||
| 1227 | |||
| 1228 | switch( strtolower( $address_part ) ){ |
||
| 1229 | case 'street': |
||
| 1230 | $sort_field_id .= '.1'; |
||
| 1231 | break; |
||
| 1232 | case 'street2': |
||
| 1233 | $sort_field_id .= '.2'; |
||
| 1234 | break; |
||
| 1235 | default: |
||
| 1236 | case 'city': |
||
| 1237 | $sort_field_id .= '.3'; |
||
| 1238 | break; |
||
| 1239 | case 'state': |
||
| 1240 | $sort_field_id .= '.4'; |
||
| 1241 | break; |
||
| 1242 | case 'zip': |
||
| 1243 | $sort_field_id .= '.5'; |
||
| 1244 | break; |
||
| 1245 | case 'country': |
||
| 1246 | $sort_field_id .= '.6'; |
||
| 1247 | break; |
||
| 1248 | } |
||
| 1249 | |||
| 1250 | } |
||
| 1251 | break; |
||
| 1252 | 5 | case 'name': |
|
| 1253 | // Sorting by full name, not first, last, etc. |
||
| 1254 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1255 | /** |
||
| 1256 | * @filter `gravityview/sorting/full-name` Override how to sort when sorting full name. |
||
| 1257 | * @since 1.7.4 |
||
| 1258 | * @param[in,out] string $name_part Sort by `first` or `last` (default: `first`) |
||
| 1259 | * @param[in] string $sort_field_id Field used for sorting |
||
| 1260 | * @param[in] int $form_id GF Form ID |
||
| 1261 | */ |
||
| 1262 | $name_part = apply_filters( 'gravityview/sorting/full-name', 'first', $sort_field_id, $form_id ); |
||
| 1263 | |||
| 1264 | if ( 'last' === strtolower( $name_part ) ) { |
||
| 1265 | $sort_field_id .= '.6'; |
||
| 1266 | } else { |
||
| 1267 | $sort_field_id .= '.3'; |
||
| 1268 | } |
||
| 1269 | } |
||
| 1270 | break; |
||
| 1271 | 5 | case 'list': |
|
| 1272 | $sort_field_id = false; |
||
| 1273 | break; |
||
| 1274 | 5 | case 'time': |
|
| 1275 | |||
| 1276 | /** |
||
| 1277 | * @filter `gravityview/sorting/time` Override how to sort when sorting time |
||
| 1278 | * @see GravityView_Field_Time |
||
| 1279 | * @since 1.14 |
||
| 1280 | * @param[in,out] string $name_part Field used for sorting |
||
| 1281 | * @param[in] int $form_id GF Form ID |
||
| 1282 | */ |
||
| 1283 | 1 | $sort_field_id = apply_filters( 'gravityview/sorting/time', $sort_field_id, $form_id ); |
|
| 1284 | 1 | break; |
|
| 1285 | } |
||
| 1286 | |||
| 1287 | 5 | return $sort_field_id; |
|
| 1288 | } |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Verify if user requested a single entry view |
||
| 1292 | * @since 2.3.3 Added return null |
||
| 1293 | * @return boolean|string|null false if not, single entry slug if true, null if \GV\Entry doesn't exist yet |
||
| 1294 | */ |
||
| 1295 | 26 | public static function is_single_entry() { |
|
| 1296 | |||
| 1297 | // Since this is a public method, it can be called outside of the plugin. Don't assume things have been loaded properly. |
||
| 1298 | 26 | if ( ! class_exists( '\GV\Entry' ) ) { |
|
| 1299 | |||
| 1300 | // Not using gravityview()->log->error(), since that may not exist yet either! |
||
| 1301 | do_action( 'gravityview_log_error', '\GV\Entry not defined yet. Backtrace: ' . wp_debug_backtrace_summary() ); |
||
| 1302 | |||
| 1303 | return null; |
||
| 1304 | } |
||
| 1305 | |||
| 1306 | 26 | $var_name = \GV\Entry::get_endpoint_name(); |
|
| 1307 | |||
| 1308 | 26 | $single_entry = get_query_var( $var_name ); |
|
| 1309 | |||
| 1310 | /** |
||
| 1311 | * Modify the entry that is being displayed. |
||
| 1312 | * |
||
| 1313 | * @internal Should only be used by things like the oEmbed functionality. |
||
| 1314 | * @since 1.6 |
||
| 1315 | */ |
||
| 1316 | 26 | $single_entry = apply_filters( 'gravityview/is_single_entry', $single_entry ); |
|
| 1317 | |||
| 1318 | 26 | if ( empty( $single_entry ) ){ |
|
| 1319 | 4 | return false; |
|
| 1320 | } else { |
||
| 1321 | 22 | return $single_entry; |
|
| 1322 | } |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Register styles and scripts |
||
| 1328 | * |
||
| 1329 | * @return void |
||
| 1330 | */ |
||
| 1331 | 1 | public function add_scripts_and_styles() { |
|
| 1449 | |||
| 1450 | /** |
||
| 1451 | * Handle enqueuing the `gravityview_default_style` stylesheet |
||
| 1452 | * |
||
| 1453 | * @since 1.17 |
||
| 1454 | * |
||
| 1455 | * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet |
||
| 1456 | * |
||
| 1457 | * @return void |
||
| 1458 | */ |
||
| 1459 | private function enqueue_default_style( $css_dependencies = array() ) { |
||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Add template extra style if exists |
||
| 1479 | * @param string $template_id |
||
| 1480 | */ |
||
| 1481 | public static function add_style( $template_id ) { |
||
| 1493 | |||
| 1494 | |||
| 1495 | /** |
||
| 1496 | * Inject the sorting links on the table columns |
||
| 1497 | * |
||
| 1498 | * Callback function for hook 'gravityview/template/field_label' |
||
| 1499 | * @see GravityView_API::field_label() (in includes/class-api.php) |
||
| 1500 | * |
||
| 1501 | * @since 1.7 |
||
| 1502 | * |
||
| 1503 | * @param string $label Field label |
||
| 1504 | * @param array $field Field settings |
||
| 1505 | * @param array $form Form object |
||
| 1506 | * |
||
| 1507 | * @return string Field Label |
||
| 1508 | */ |
||
| 1509 | public function add_columns_sort_links( $label = '', $field, $form ) { |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * Checks if field (column) is sortable |
||
| 1555 | * |
||
| 1556 | * @param string $field Field settings |
||
| 1557 | * @param array $form Gravity Forms form array |
||
| 1558 | * |
||
| 1559 | * @since 1.7 |
||
| 1560 | * |
||
| 1561 | * @return bool True: Yes, field is sortable; False: not sortable |
||
| 1562 | */ |
||
| 1563 | 1 | public function is_field_sortable( $field_id = '', $form = array() ) { |
|
| 1593 | |||
| 1594 | } |
||
| 1595 | |||
| 1596 | GravityView_frontend::getInstance(); |
||
| 1597 | |||
| 1598 | |||
| 1599 | |||
| 1600 |