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 | 37 | private function initialize() { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get the one true instantiated self |
||
| 106 | * @return GravityView_frontend |
||
| 107 | */ |
||
| 108 | 43 | public static function getInstance() { |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return GravityView_View_Data |
||
| 120 | */ |
||
| 121 | 37 | public function getGvOutputData() { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param \GravityView_View_Data $gv_output_data |
||
| 127 | */ |
||
| 128 | 37 | public function setGvOutputData( $gv_output_data ) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @return boolean |
||
| 134 | */ |
||
| 135 | 36 | public function isSearch() { |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param boolean $is_search |
||
| 141 | */ |
||
| 142 | 37 | public function setIsSearch( $is_search ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @return bool|int |
||
| 148 | */ |
||
| 149 | 50 | public function getSingleEntry() { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Sets the single entry ID and also the entry |
||
| 155 | * @param bool|int|string $single_entry |
||
| 156 | */ |
||
| 157 | 36 | public function setSingleEntry( $single_entry ) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | 44 | public function getEntry() { |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Set the current entry |
||
| 172 | * @param array|int $entry Entry array or entry slug or ID |
||
| 173 | */ |
||
| 174 | 36 | public function setEntry( $entry ) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return int |
||
| 185 | */ |
||
| 186 | 37 | public function getPostId() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param int $post_id |
||
| 192 | */ |
||
| 193 | 37 | public function setPostId( $post_id ) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | 38 | public function isPostHasShortcode() { |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param boolean $post_has_shortcode |
||
| 206 | */ |
||
| 207 | 37 | public function setPostHasShortcode( $post_has_shortcode ) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return boolean |
||
| 213 | */ |
||
| 214 | 39 | public function isGravityviewPostType() { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param boolean $is_gravityview_post_type |
||
| 220 | */ |
||
| 221 | 37 | 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 | 2 | 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 string |
||
| 260 | */ |
||
| 261 | 36 | 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 | 168 | 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 | 1 | public function parse_content( $wp = array() ) { |
|
| 338 | 1 | global $post; |
|
| 339 | |||
| 340 | // If in admin and NOT AJAX request, get outta here. |
||
| 341 | 1 | if ( gravityview()->request->is_admin() ) { |
|
| 342 | return; |
||
| 343 | } |
||
| 344 | |||
| 345 | // Calculate requested Views |
||
| 346 | 1 | $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 | 1 | $this->set_context_view_id(); |
|
| 350 | |||
| 351 | 1 | $this->setIsGravityviewPostType( get_post_type( $post ) === 'gravityview' ); |
|
| 352 | |||
| 353 | 1 | $post_id = $this->getPostId() ? $this->getPostId() : (isset( $post ) ? $post->ID : null ); |
|
| 354 | 1 | $this->setPostId( $post_id ); |
|
| 355 | 1 | $post_has_shortcode = ! empty( $post->post_content ) ? gravityview_has_shortcode_r( $post->post_content, 'gravityview' ) : false; |
|
| 356 | 1 | $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 | 1 | $this->setIsSearch( $this->is_searching() ); |
|
| 360 | |||
| 361 | 1 | unset( $entry, $post_id, $post_has_shortcode ); |
|
| 362 | 1 | } |
|
| 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 | 1 | function is_searching() { |
|
| 381 | |||
| 382 | // It's a single entry, not search |
||
| 383 | 1 | if ( $this->getSingleEntry() ) { |
|
| 384 | 1 | return false; |
|
| 385 | } |
||
| 386 | |||
| 387 | 1 | $search_method = GravityView_Widget_Search::getInstance()->get_search_method(); |
|
| 388 | |||
| 389 | 1 | if( 'post' === $search_method ) { |
|
| 390 | $get = $_POST; |
||
| 391 | } else { |
||
| 392 | 1 | $get = $_GET; |
|
| 393 | } |
||
| 394 | |||
| 395 | // No $_GET parameters |
||
| 396 | 1 | if ( empty( $get ) || ! is_array( $get ) ) { |
|
| 397 | 1 | return false; |
|
| 398 | } |
||
| 399 | |||
| 400 | // Remove empty values |
||
| 401 | $get = array_filter( $get ); |
||
| 402 | |||
| 403 | // If the $_GET parameters are empty, it's no search. |
||
| 404 | if ( empty( $get ) ) { |
||
| 405 | return false; |
||
| 406 | } |
||
| 407 | |||
| 408 | $search_keys = array_keys( $get ); |
||
| 409 | |||
| 410 | $search_match = implode( '|', self::$search_parameters ); |
||
| 411 | |||
| 412 | foreach ( $search_keys as $search_key ) { |
||
| 413 | |||
| 414 | // Analyze the search key $_GET parameter and see if it matches known GV args |
||
| 415 | if ( preg_match( '/(' . $search_match . ')/i', $search_key ) ) { |
||
| 416 | return true; |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | return false; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Filter the title for the single entry view |
||
| 425 | * |
||
| 426 | * |
||
| 427 | * @param string $title current title |
||
| 428 | * @param int $passed_post_id Post ID |
||
| 429 | * @return string (modified) title |
||
| 430 | */ |
||
| 431 | 8 | public function single_entry_title( $title, $passed_post_id = null ) { |
|
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * In case View post is called directly, insert the view in the post content |
||
| 496 | * |
||
| 497 | * @deprecated Use \GV\View::content() instead. |
||
| 498 | * |
||
| 499 | * @access public |
||
| 500 | * @static |
||
| 501 | * @param mixed $content |
||
| 502 | * @return string Add the View output into View CPT content |
||
| 503 | */ |
||
| 504 | 4 | public function insert_view_in_content( $content ) { |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Disable comments on GravityView post types |
||
| 511 | * @param boolean $open existing status |
||
| 512 | * @param int $post_id Post ID |
||
| 513 | * @return boolean |
||
| 514 | */ |
||
| 515 | public function comments_open( $open, $post_id ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Display a warning when a View has not been configured |
||
| 534 | * |
||
| 535 | * @since 1.19.2 |
||
| 536 | * |
||
| 537 | * @param int $view_id The ID of the View currently being displayed |
||
| 538 | * |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | 2 | public function context_not_configured_warning( $view_id = 0 ) { |
|
| 579 | |||
| 580 | |||
| 581 | /** |
||
| 582 | * Core function to render a View based on a set of arguments |
||
| 583 | * |
||
| 584 | * @access public |
||
| 585 | * @static |
||
| 586 | * @param array $passed_args { |
||
| 587 | * |
||
| 588 | * Settings for rendering the View |
||
| 589 | * |
||
| 590 | * @type int $id View id |
||
| 591 | * @type int $page_size Number of entries to show per page |
||
| 592 | * @type string $sort_field Form field id to sort |
||
| 593 | * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND') |
||
| 594 | * @type string $start_date - Ymd |
||
| 595 | * @type string $end_date - Ymd |
||
| 596 | * @type string $class - assign a html class to the view |
||
| 597 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 598 | * } |
||
| 599 | * |
||
| 600 | * @deprecated Use \GV\View_Renderer |
||
| 601 | * |
||
| 602 | * @return string|null HTML output of a View, NULL if View isn't found |
||
| 603 | */ |
||
| 604 | 1 | public function render_view( $passed_args ) { |
|
| 629 | |||
| 630 | /** |
||
| 631 | * Process the start and end dates for a view - overrides values defined in shortcode (if needed) |
||
| 632 | * |
||
| 633 | * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), |
||
| 634 | * which uses \DateTime() format. |
||
| 635 | * |
||
| 636 | * 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()}, |
||
| 637 | * including strings like "now" or "-1 year" or "-3 days". |
||
| 638 | * |
||
| 639 | * @see GFFormsModel::get_date_range_where |
||
| 640 | * |
||
| 641 | * @param array $args View settings |
||
| 642 | * @param array $search_criteria Search being performed, if any |
||
| 643 | * @return array Modified `$search_criteria` array |
||
| 644 | */ |
||
| 645 | 63 | public static function process_search_dates( $args, $search_criteria = array() ) { |
|
| 711 | |||
| 712 | |||
| 713 | /** |
||
| 714 | * Process the approved only search criteria according to the View settings |
||
| 715 | * |
||
| 716 | * @param array $args View settings |
||
| 717 | * @param array $search_criteria Search being performed, if any |
||
| 718 | * @return array Modified `$search_criteria` array |
||
| 719 | */ |
||
| 720 | 62 | public static function process_search_only_approved( $args, $search_criteria ) { |
|
| 742 | |||
| 743 | |||
| 744 | /** |
||
| 745 | * Check if a certain entry is approved. |
||
| 746 | * |
||
| 747 | * If we pass the View settings ($args) it will check the 'show_only_approved' setting before |
||
| 748 | * checking the entry approved field, returning true if show_only_approved = false. |
||
| 749 | * |
||
| 750 | * @since 1.7 |
||
| 751 | * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved |
||
| 752 | * |
||
| 753 | * @uses GravityView_Entry_Approval_Status::is_approved |
||
| 754 | * |
||
| 755 | * @param array $entry Entry object |
||
| 756 | * @param array $args View settings (optional) |
||
| 757 | * |
||
| 758 | * @return bool |
||
| 759 | */ |
||
| 760 | public static function is_entry_approved( $entry, $args = array() ) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Parse search criteria for a entries search. |
||
| 780 | * |
||
| 781 | * array( |
||
| 782 | * 'search_field' => 1, // ID of the field |
||
| 783 | * 'search_value' => '', // Value of the field to search |
||
| 784 | * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains' |
||
| 785 | * 'show_only_approved' => 0 or 1 // Boolean |
||
| 786 | * ) |
||
| 787 | * |
||
| 788 | * @param array $args Array of args |
||
| 789 | * @param int $form_id Gravity Forms form ID |
||
| 790 | * @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. |
||
| 791 | */ |
||
| 792 | 63 | public static function get_search_criteria( $args, $form_id ) { |
|
| 850 | |||
| 851 | |||
| 852 | |||
| 853 | /** |
||
| 854 | * Core function to calculate View multi entries (directory) based on a set of arguments ($args): |
||
| 855 | * $id - View id |
||
| 856 | * $page_size - Page |
||
| 857 | * $sort_field - form field id to sort |
||
| 858 | * $sort_direction - ASC / DESC |
||
| 859 | * $start_date - Ymd |
||
| 860 | * $end_date - Ymd |
||
| 861 | * $class - assign a html class to the view |
||
| 862 | * $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 863 | * |
||
| 864 | * |
||
| 865 | * |
||
| 866 | * @uses gravityview_get_entries() |
||
| 867 | * @access public |
||
| 868 | * @param array $args\n |
||
| 869 | * - $id - View id |
||
| 870 | * - $page_size - Page |
||
| 871 | * - $sort_field - form field id to sort |
||
| 872 | * - $sort_direction - ASC / DESC |
||
| 873 | * - $start_date - Ymd |
||
| 874 | * - $end_date - Ymd |
||
| 875 | * - $class - assign a html class to the view |
||
| 876 | * - $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 877 | * @param int $form_id Gravity Forms Form ID |
||
| 878 | * @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 |
||
| 879 | */ |
||
| 880 | 1 | public static function get_view_entries( $args, $form_id ) { |
|
| 917 | |||
| 918 | /** |
||
| 919 | * Get an array of search parameters formatted as Gravity Forms requires |
||
| 920 | * |
||
| 921 | * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters |
||
| 922 | * |
||
| 923 | * @uses GravityView_frontend::get_search_criteria |
||
| 924 | * @uses GravityView_frontend::get_search_criteria_paging |
||
| 925 | * |
||
| 926 | * @since 1.20 |
||
| 927 | * |
||
| 928 | * @see \GV\View_Settings::defaults For $args options |
||
| 929 | * |
||
| 930 | * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults |
||
| 931 | * @param int $form_id Gravity Forms form ID to search |
||
| 932 | * |
||
| 933 | * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys |
||
| 934 | */ |
||
| 935 | 62 | public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) { |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Get the paging array for the View |
||
| 993 | * |
||
| 994 | * @since 1.19.5 |
||
| 995 | * |
||
| 996 | * @param $args |
||
| 997 | * @param int $form_id |
||
| 998 | */ |
||
| 999 | 62 | public static function get_search_criteria_paging( $args ) { |
|
| 1031 | |||
| 1032 | /** |
||
| 1033 | * Updates the View sorting criteria |
||
| 1034 | * |
||
| 1035 | * @since 1.7 |
||
| 1036 | * |
||
| 1037 | * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys |
||
| 1038 | * @param int $form_id The ID of the form used to sort |
||
| 1039 | * @return array $sorting Array with `key`, `direction` and `is_numeric` keys |
||
| 1040 | */ |
||
| 1041 | 62 | public static function updateViewSorting( $args, $form_id ) { |
|
| 1042 | 62 | $sorting = array(); |
|
| 1043 | |||
| 1044 | 62 | $has_values = isset( $_GET['sort'] ); |
|
| 1045 | |||
| 1046 | 62 | if ( $has_values && is_array( $_GET['sort'] ) ) { |
|
| 1047 | 1 | $has_values = array_filter( array_values( $_GET['sort'] ) ); |
|
| 1048 | } |
||
| 1049 | |||
| 1050 | 62 | $sort_field_id = $has_values ? $_GET['sort'] : \GV\Utils::get( $args, 'sort_field' ); |
|
| 1051 | 62 | $sort_direction = isset( $_GET['dir'] ) ? $_GET['dir'] : \GV\Utils::get( $args, 'sort_direction' ); |
|
| 1052 | |||
| 1053 | 62 | if ( is_array( $sort_field_id ) ) { |
|
| 1054 | 1 | $sort_field_id = array_pop( $sort_field_id ); |
|
| 1055 | } |
||
| 1056 | |||
| 1057 | 62 | if ( is_array( $sort_direction ) ) { |
|
| 1058 | $sort_direction = array_pop( $sort_direction ); |
||
| 1059 | } |
||
| 1060 | |||
| 1061 | 62 | if ( ! empty( $sort_field_id ) ) { |
|
| 1062 | 3 | if ( is_array( $sort_field_id ) ) { |
|
| 1063 | $sort_direction = array_values( $sort_field_id ); |
||
| 1064 | $sort_field_id = array_keys( $sort_field_id ); |
||
| 1065 | |||
| 1066 | $sort_field_id = reset( $sort_field_id ); |
||
| 1067 | $sort_direction = reset( $sort_direction ); |
||
| 1068 | } |
||
| 1069 | |||
| 1070 | 3 | $sort_field_id = self::_override_sorting_id_by_field_type( $sort_field_id, $form_id ); |
|
| 1071 | $sorting = array( |
||
| 1072 | 3 | 'key' => $sort_field_id, |
|
| 1073 | 3 | 'direction' => strtolower( $sort_direction ), |
|
| 1074 | 3 | 'is_numeric' => GVCommon::is_field_numeric( $form_id, $sort_field_id ) |
|
| 1075 | ); |
||
| 1076 | |||
| 1077 | 3 | if ( 'RAND' === $sort_direction ) { |
|
| 1078 | |||
| 1079 | $form = GFAPI::get_form( $form_id ); |
||
| 1080 | |||
| 1081 | // Get the first GF_Field field ID, set as the key for entry randomization |
||
| 1082 | if ( ! empty( $form['fields'] ) ) { |
||
| 1083 | |||
| 1084 | /** @var GF_Field $field */ |
||
| 1085 | foreach ( $form['fields'] as $field ) { |
||
| 1086 | if ( ! is_a( $field, 'GF_Field' ) ) { |
||
| 1087 | continue; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | $sorting = array( |
||
| 1091 | 'key' => $field->id, |
||
| 1092 | 'is_numeric' => false, |
||
| 1093 | 'direction' => 'RAND', |
||
| 1094 | ); |
||
| 1095 | |||
| 1096 | break; |
||
| 1097 | } |
||
| 1098 | } |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | 62 | GravityView_View::getInstance()->setSorting( $sorting ); |
|
| 1103 | |||
| 1104 | 62 | gravityview()->log->debug( '[updateViewSorting] Sort Criteria : ', array( 'data' => $sorting ) ); |
|
| 1105 | |||
| 1106 | 62 | return $sorting; |
|
| 1107 | |||
| 1108 | } |
||
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Override sorting per field |
||
| 1112 | * |
||
| 1113 | * Currently only modifies sorting ID when sorting by the full name. Sorts by first name. |
||
| 1114 | * Use the `gravityview/sorting/full-name` filter to override. |
||
| 1115 | * |
||
| 1116 | * @todo Filter from GravityView_Field |
||
| 1117 | * @since 1.7.4 |
||
| 1118 | * @internal Hi developer! Although this is public, don't call this method; we're going to replace it. |
||
| 1119 | * |
||
| 1120 | * @param int|string|array $sort_field_id Field used for sorting (`id` or `1.2`), or an array for multisorts |
||
| 1121 | * @param int $form_id GF Form ID |
||
| 1122 | * |
||
| 1123 | * @return string Possibly modified sorting ID |
||
| 1124 | */ |
||
| 1125 | 3 | public static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) { |
|
| 1126 | |||
| 1127 | 3 | if ( is_array( $sort_field_id ) ) { |
|
| 1128 | $modified_ids = array(); |
||
| 1129 | foreach ( $sort_field_id as $_sort_field_id ) { |
||
| 1130 | $modified_ids []= self::_override_sorting_id_by_field_type( $_sort_field_id, $form_id ); |
||
| 1131 | } |
||
| 1132 | return $modified_ids; |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | 3 | $form = gravityview_get_form( $form_id ); |
|
| 1136 | |||
| 1137 | 3 | $sort_field = GFFormsModel::get_field( $form, $sort_field_id ); |
|
| 1138 | |||
| 1139 | 3 | if( ! $sort_field ) { |
|
| 1140 | 1 | return $sort_field_id; |
|
| 1141 | } |
||
| 1142 | |||
| 1143 | 3 | switch ( $sort_field['type'] ) { |
|
| 1144 | |||
| 1145 | 3 | case 'address': |
|
| 1146 | // Sorting by full address |
||
| 1147 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1148 | |||
| 1149 | /** |
||
| 1150 | * Override how to sort when sorting address |
||
| 1151 | * |
||
| 1152 | * @since 1.8 |
||
| 1153 | * |
||
| 1154 | * @param string $address_part `street`, `street2`, `city`, `state`, `zip`, or `country` (default: `city`) |
||
| 1155 | * @param string $sort_field_id Field used for sorting |
||
| 1156 | * @param int $form_id GF Form ID |
||
| 1157 | */ |
||
| 1158 | $address_part = apply_filters( 'gravityview/sorting/address', 'city', $sort_field_id, $form_id ); |
||
| 1159 | |||
| 1160 | switch( strtolower( $address_part ) ){ |
||
| 1161 | case 'street': |
||
| 1162 | $sort_field_id .= '.1'; |
||
| 1163 | break; |
||
| 1164 | case 'street2': |
||
| 1165 | $sort_field_id .= '.2'; |
||
| 1166 | break; |
||
| 1167 | default: |
||
| 1168 | case 'city': |
||
| 1169 | $sort_field_id .= '.3'; |
||
| 1170 | break; |
||
| 1171 | case 'state': |
||
| 1172 | $sort_field_id .= '.4'; |
||
| 1173 | break; |
||
| 1174 | case 'zip': |
||
| 1175 | $sort_field_id .= '.5'; |
||
| 1176 | break; |
||
| 1177 | case 'country': |
||
| 1178 | $sort_field_id .= '.6'; |
||
| 1179 | break; |
||
| 1180 | } |
||
| 1181 | |||
| 1182 | } |
||
| 1183 | break; |
||
| 1184 | 3 | case 'name': |
|
| 1185 | // Sorting by full name, not first, last, etc. |
||
| 1186 | if ( floatval( $sort_field_id ) === floor( $sort_field_id ) ) { |
||
| 1187 | /** |
||
| 1188 | * @filter `gravityview/sorting/full-name` Override how to sort when sorting full name. |
||
| 1189 | * @since 1.7.4 |
||
| 1190 | * @param[in,out] string $name_part Sort by `first` or `last` (default: `first`) |
||
| 1191 | * @param[in] string $sort_field_id Field used for sorting |
||
| 1192 | * @param[in] int $form_id GF Form ID |
||
| 1193 | */ |
||
| 1194 | $name_part = apply_filters( 'gravityview/sorting/full-name', 'first', $sort_field_id, $form_id ); |
||
| 1195 | |||
| 1196 | if ( 'last' === strtolower( $name_part ) ) { |
||
| 1197 | $sort_field_id .= '.6'; |
||
| 1198 | } else { |
||
| 1199 | $sort_field_id .= '.3'; |
||
| 1200 | } |
||
| 1201 | } |
||
| 1202 | break; |
||
| 1203 | 3 | case 'list': |
|
| 1204 | $sort_field_id = false; |
||
| 1205 | break; |
||
| 1206 | 3 | case 'time': |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * @filter `gravityview/sorting/time` Override how to sort when sorting time |
||
| 1210 | * @see GravityView_Field_Time |
||
| 1211 | * @since 1.14 |
||
| 1212 | * @param[in,out] string $name_part Field used for sorting |
||
| 1213 | * @param[in] int $form_id GF Form ID |
||
| 1214 | */ |
||
| 1215 | $sort_field_id = apply_filters( 'gravityview/sorting/time', $sort_field_id, $form_id ); |
||
| 1216 | break; |
||
| 1217 | } |
||
| 1218 | |||
| 1219 | 3 | return $sort_field_id; |
|
| 1220 | } |
||
| 1221 | |||
| 1222 | /** |
||
| 1223 | * Verify if user requested a single entry view |
||
| 1224 | * @since 2.3.3 Added return null |
||
| 1225 | * @return boolean|string|null false if not, single entry slug if true, null if \GV\Entry doesn't exist yet |
||
| 1226 | */ |
||
| 1227 | 18 | public static function is_single_entry() { |
|
| 1228 | |||
| 1229 | // Since this is a public method, it can be called outside of the plugin. Don't assume things have been loaded properly. |
||
| 1230 | 18 | if ( ! class_exists( '\GV\Entry' ) ) { |
|
| 1231 | |||
| 1232 | // Not using gravityview()->log->error(), since that may not exist yet either! |
||
| 1233 | do_action( 'gravityview_log_error', '\GV\Entry not defined yet. Backtrace: ' . wp_debug_backtrace_summary() ); |
||
| 1234 | |||
| 1235 | return null; |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | 18 | $var_name = \GV\Entry::get_endpoint_name(); |
|
| 1239 | |||
| 1240 | 18 | $single_entry = get_query_var( $var_name ); |
|
| 1241 | |||
| 1242 | /** |
||
| 1243 | * Modify the entry that is being displayed. |
||
| 1244 | * |
||
| 1245 | * @internal Should only be used by things like the oEmbed functionality. |
||
| 1246 | * @since 1.6 |
||
| 1247 | */ |
||
| 1248 | 18 | $single_entry = apply_filters( 'gravityview/is_single_entry', $single_entry ); |
|
| 1249 | |||
| 1250 | 18 | if ( empty( $single_entry ) ){ |
|
| 1251 | 3 | return false; |
|
| 1252 | } else { |
||
| 1253 | 15 | return $single_entry; |
|
| 1254 | } |
||
| 1255 | } |
||
| 1256 | |||
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Register styles and scripts |
||
| 1260 | * |
||
| 1261 | * @access public |
||
| 1262 | * @return void |
||
| 1263 | */ |
||
| 1264 | 1 | public function add_scripts_and_styles() { |
|
| 1265 | 1 | global $post, $posts; |
|
| 1266 | // enqueue template specific styles |
||
| 1267 | 1 | if ( $this->getGvOutputData() ) { |
|
| 1268 | |||
| 1269 | 1 | $views = $this->getGvOutputData()->get_views(); |
|
| 1270 | |||
| 1271 | 1 | foreach ( $views as $view_id => $data ) { |
|
| 1272 | 1 | $view = \GV\View::by_id( $data['id'] ); |
|
| 1273 | 1 | $view_id = $view->ID; |
|
| 1274 | 1 | $template_id = gravityview_get_template_id( $view->ID ); |
|
| 1275 | 1 | $data = $view->as_data(); |
|
| 1276 | |||
| 1277 | /** |
||
| 1278 | * Don't enqueue the scripts or styles if it's not going to be displayed. |
||
| 1279 | * @since 1.15 |
||
| 1280 | */ |
||
| 1281 | 1 | if( is_user_logged_in() && false === GVCommon::has_cap( 'read_gravityview', $view_id ) ) { |
|
| 1282 | continue; |
||
| 1283 | } |
||
| 1284 | |||
| 1285 | // By default, no thickbox |
||
| 1286 | 1 | $js_dependencies = array( 'jquery', 'gravityview-jquery-cookie' ); |
|
| 1287 | 1 | $css_dependencies = array(); |
|
| 1288 | |||
| 1289 | 1 | $lightbox = $view->settings->get( 'lightbox' ); |
|
| 1290 | |||
| 1291 | // If the thickbox is enqueued, add dependencies |
||
| 1292 | 1 | if ( $lightbox ) { |
|
| 1293 | |||
| 1294 | /** |
||
| 1295 | * @filter `gravity_view_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox` |
||
| 1296 | * @param string $script_slug If you want to use a different lightbox script, return the name of it here. |
||
| 1297 | */ |
||
| 1298 | 1 | $js_dependencies[] = apply_filters( 'gravity_view_lightbox_script', 'thickbox' ); |
|
| 1299 | |||
| 1300 | /** |
||
| 1301 | * @filter `gravity_view_lightbox_style` Modify the lightbox CSS slug. Default: `thickbox` |
||
| 1302 | * @param string $script_slug If you want to use a different lightbox script, return the name of its CSS file here. |
||
| 1303 | */ |
||
| 1304 | 1 | $css_dependencies[] = apply_filters( 'gravity_view_lightbox_style', 'thickbox' ); |
|
| 1305 | } |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * If the form has checkbox fields, enqueue dashicons |
||
| 1309 | * @see https://github.com/katzwebservices/GravityView/issues/536 |
||
| 1310 | * @since 1.15 |
||
| 1311 | */ |
||
| 1312 | 1 | if( gravityview_view_has_single_checkbox_or_radio( $data['form'], $data['fields'] ) ) { |
|
| 1313 | $css_dependencies[] = 'dashicons'; |
||
| 1314 | } |
||
| 1315 | |||
| 1316 | 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 ); |
|
| 1317 | |||
| 1318 | 1 | $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
|
| 1319 | |||
| 1320 | 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 ); |
|
| 1321 | |||
| 1322 | 1 | wp_enqueue_script( 'gravityview-fe-view' ); |
|
| 1323 | |||
| 1324 | 1 | if ( ! empty( $data['atts']['sort_columns'] ) ) { |
|
| 1325 | wp_enqueue_style( 'gravityview_font', plugins_url( 'assets/css/font.css', GRAVITYVIEW_FILE ), $css_dependencies, GravityView_Plugin::version, 'all' ); |
||
| 1326 | } |
||
| 1327 | |||
| 1328 | 1 | $this->enqueue_default_style( $css_dependencies ); |
|
| 1329 | |||
| 1330 | 1 | self::add_style( $template_id ); |
|
| 1331 | } |
||
| 1332 | |||
| 1333 | 1 | if ( 'wp_print_footer_scripts' === current_filter() ) { |
|
| 1334 | |||
| 1335 | $js_localization = array( |
||
| 1336 | 'cookiepath' => COOKIEPATH, |
||
| 1337 | 'clear' => _x( 'Clear', 'Clear all data from the form', 'gravityview' ), |
||
| 1338 | 'reset' => _x( 'Reset', 'Reset the search form to the state that existed on page load', 'gravityview' ), |
||
| 1339 | ); |
||
| 1340 | |||
| 1341 | /** |
||
| 1342 | * @filter `gravityview_js_localization` Modify the array passed to wp_localize_script() |
||
| 1343 | * @param array $js_localization The data padded to the Javascript file |
||
| 1344 | * @param array $views Array of View data arrays with View settings |
||
| 1345 | */ |
||
| 1346 | $js_localization = apply_filters( 'gravityview_js_localization', $js_localization, $views ); |
||
| 1347 | |||
| 1348 | wp_localize_script( 'gravityview-fe-view', 'gvGlobals', $js_localization ); |
||
| 1349 | } |
||
| 1350 | } |
||
| 1351 | 1 | } |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Handle enqueuing the `gravityview_default_style` stylesheet |
||
| 1355 | * |
||
| 1356 | * @since 1.17 |
||
| 1357 | * |
||
| 1358 | * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet |
||
| 1359 | * |
||
| 1360 | * @return void |
||
| 1361 | */ |
||
| 1362 | private function enqueue_default_style( $css_dependencies = array() ) { |
||
| 1379 | |||
| 1380 | /** |
||
| 1381 | * Add template extra style if exists |
||
| 1382 | * @param string $template_id |
||
| 1383 | */ |
||
| 1384 | public static function add_style( $template_id ) { |
||
| 1396 | |||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * Inject the sorting links on the table columns |
||
| 1400 | * |
||
| 1401 | * Callback function for hook 'gravityview/template/field_label' |
||
| 1402 | * @see GravityView_API::field_label() (in includes/class-api.php) |
||
| 1403 | * |
||
| 1404 | * @since 1.7 |
||
| 1405 | * |
||
| 1406 | * @param string $label Field label |
||
| 1407 | * @param array $field Field settings |
||
| 1408 | * @param array $form Form object |
||
| 1409 | * |
||
| 1410 | * @return string Field Label |
||
| 1411 | */ |
||
| 1412 | public function add_columns_sort_links( $label = '', $field, $form ) { |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Checks if field (column) is sortable |
||
| 1458 | * |
||
| 1459 | * @param string $field Field settings |
||
| 1460 | * @param array $form Gravity Forms form array |
||
| 1461 | * |
||
| 1462 | * @since 1.7 |
||
| 1463 | * |
||
| 1464 | * @return bool True: Yes, field is sortable; False: not sortable |
||
| 1465 | */ |
||
| 1466 | 1 | public function is_field_sortable( $field_id = '', $form = array() ) { |
|
| 1496 | |||
| 1497 | } |
||
| 1498 | |||
| 1503 |