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 | 185 | 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 ) { |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Returns the single entry title for a View with variables replaced and shortcodes parsed |
||
| 498 | * |
||
| 499 | * @since 2.7.2 |
||
| 500 | * |
||
| 501 | * @param \GV\View|null $view |
||
| 502 | * @param array $entry |
||
| 503 | * @param string $passed_title |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | private function _get_single_entry_title( $view, $entry = array(), $passed_title = '' ) { |
||
| 543 | |||
| 544 | |||
| 545 | /** |
||
| 546 | * In case View post is called directly, insert the view in the post content |
||
| 547 | * |
||
| 548 | * @deprecated Use \GV\View::content() instead. |
||
| 549 | * |
||
| 550 | * @access public |
||
| 551 | * @static |
||
| 552 | * @param mixed $content |
||
| 553 | * @return string Add the View output into View CPT content |
||
| 554 | */ |
||
| 555 | 4 | public function insert_view_in_content( $content ) { |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Disable comments on GravityView post types |
||
| 562 | * @param boolean $open existing status |
||
| 563 | * @param int $post_id Post ID |
||
| 564 | * @return boolean |
||
| 565 | */ |
||
| 566 | public function comments_open( $open, $post_id ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Display a warning when a View has not been configured |
||
| 585 | * |
||
| 586 | * @since 1.19.2 |
||
| 587 | * |
||
| 588 | * @param int $view_id The ID of the View currently being displayed |
||
| 589 | * |
||
| 590 | * @return void |
||
| 591 | */ |
||
| 592 | 2 | public function context_not_configured_warning( $view_id = 0 ) { |
|
| 593 | |||
| 594 | 2 | if ( ! class_exists( 'GravityView_View' ) ) { |
|
| 595 | return; |
||
| 596 | } |
||
| 597 | |||
| 598 | 2 | $fields = GravityView_View::getInstance()->getContextFields(); |
|
| 599 | |||
| 600 | 2 | if ( ! empty( $fields ) ) { |
|
| 601 | 2 | return; |
|
| 602 | } |
||
| 603 | |||
| 604 | $context = GravityView_View::getInstance()->getContext(); |
||
| 605 | |||
| 606 | switch( $context ) { |
||
| 607 | case 'directory': |
||
| 608 | $tab = __( 'Multiple Entries', 'gravityview' ); |
||
| 609 | break; |
||
| 610 | case 'edit': |
||
| 611 | $tab = __( 'Edit Entry', 'gravityview' ); |
||
| 612 | break; |
||
| 613 | case 'single': |
||
| 614 | default: |
||
| 615 | $tab = __( 'Single Entry', 'gravityview' ); |
||
| 616 | break; |
||
| 617 | } |
||
| 618 | |||
| 619 | |||
| 620 | $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 ); |
||
| 621 | $edit_link = admin_url( sprintf( 'post.php?post=%d&action=edit#%s-view', $view_id, $context ) ); |
||
| 622 | $action_text = sprintf( esc_html__('Add fields to %s', 'gravityview' ), $tab ); |
||
| 623 | $message = esc_html__( 'You can only see this message because you are able to edit this View.', 'gravityview' ); |
||
| 624 | |||
| 625 | $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 ) ) ); |
||
| 626 | $output = sprintf( '<h3>%s <strong><a href="%s">%s</a></strong></h3><p>%s</p>', $title, esc_url( $edit_link ), $action_text, $message ); |
||
| 627 | |||
| 628 | echo GVCommon::generate_notice( $output . $image, 'gv-error error', 'edit_gravityview', $view_id ); |
||
| 629 | } |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * Core function to render a View based on a set of arguments |
||
| 634 | * |
||
| 635 | * @access public |
||
| 636 | * @static |
||
| 637 | * @param array $passed_args { |
||
| 638 | * |
||
| 639 | * Settings for rendering the View |
||
| 640 | * |
||
| 641 | * @type int $id View id |
||
| 642 | * @type int $page_size Number of entries to show per page |
||
| 643 | * @type string $sort_field Form field id to sort |
||
| 644 | * @type string $sort_direction Sorting direction ('ASC', 'DESC', or 'RAND') |
||
| 645 | * @type string $start_date - Ymd |
||
| 646 | * @type string $end_date - Ymd |
||
| 647 | * @type string $class - assign a html class to the view |
||
| 648 | * @type string $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 649 | * } |
||
| 650 | * |
||
| 651 | * @deprecated Use \GV\View_Renderer |
||
| 652 | * |
||
| 653 | * @return string|null HTML output of a View, NULL if View isn't found |
||
| 654 | */ |
||
| 655 | 1 | public function render_view( $passed_args ) { |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Process the start and end dates for a view - overrides values defined in shortcode (if needed) |
||
| 683 | * |
||
| 684 | * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), |
||
| 685 | * which uses \DateTime() format. |
||
| 686 | * |
||
| 687 | * 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()}, |
||
| 688 | * including strings like "now" or "-1 year" or "-3 days". |
||
| 689 | * |
||
| 690 | * @see GFFormsModel::get_date_range_where |
||
| 691 | * |
||
| 692 | * @param array $args View settings |
||
| 693 | * @param array $search_criteria Search being performed, if any |
||
| 694 | * @return array Modified `$search_criteria` array |
||
| 695 | */ |
||
| 696 | 74 | public static function process_search_dates( $args, $search_criteria = array() ) { |
|
| 762 | |||
| 763 | |||
| 764 | /** |
||
| 765 | * Process the approved only search criteria according to the View settings |
||
| 766 | * |
||
| 767 | * @param array $args View settings |
||
| 768 | * @param array $search_criteria Search being performed, if any |
||
| 769 | * @return array Modified `$search_criteria` array |
||
| 770 | */ |
||
| 771 | 73 | public static function process_search_only_approved( $args, $search_criteria ) { |
|
| 793 | |||
| 794 | |||
| 795 | /** |
||
| 796 | * Check if a certain entry is approved. |
||
| 797 | * |
||
| 798 | * If we pass the View settings ($args) it will check the 'show_only_approved' setting before |
||
| 799 | * checking the entry approved field, returning true if show_only_approved = false. |
||
| 800 | * |
||
| 801 | * @since 1.7 |
||
| 802 | * @since 1.18 Converted check to use GravityView_Entry_Approval_Status::is_approved |
||
| 803 | * |
||
| 804 | * @uses GravityView_Entry_Approval_Status::is_approved |
||
| 805 | * |
||
| 806 | * @param array $entry Entry object |
||
| 807 | * @param array $args View settings (optional) |
||
| 808 | * |
||
| 809 | * @return bool |
||
| 810 | */ |
||
| 811 | public static function is_entry_approved( $entry, $args = array() ) { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Parse search criteria for a entries search. |
||
| 831 | * |
||
| 832 | * array( |
||
| 833 | * 'search_field' => 1, // ID of the field |
||
| 834 | * 'search_value' => '', // Value of the field to search |
||
| 835 | * 'search_operator' => 'contains', // 'is', 'isnot', '>', '<', 'contains' |
||
| 836 | * 'show_only_approved' => 0 or 1 // Boolean |
||
| 837 | * ) |
||
| 838 | * |
||
| 839 | * @param array $args Array of args |
||
| 840 | * @param int $form_id Gravity Forms form ID |
||
| 841 | * @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. |
||
| 842 | */ |
||
| 843 | 74 | public static function get_search_criteria( $args, $form_id ) { |
|
| 905 | |||
| 906 | |||
| 907 | |||
| 908 | /** |
||
| 909 | * Core function to calculate View multi entries (directory) based on a set of arguments ($args): |
||
| 910 | * $id - View id |
||
| 911 | * $page_size - Page |
||
| 912 | * $sort_field - form field id to sort |
||
| 913 | * $sort_direction - ASC / DESC |
||
| 914 | * $start_date - Ymd |
||
| 915 | * $end_date - Ymd |
||
| 916 | * $class - assign a html class to the view |
||
| 917 | * $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 918 | * |
||
| 919 | * |
||
| 920 | * |
||
| 921 | * @uses gravityview_get_entries() |
||
| 922 | * @access public |
||
| 923 | * @param array $args\n |
||
| 924 | * - $id - View id |
||
| 925 | * - $page_size - Page |
||
| 926 | * - $sort_field - form field id to sort |
||
| 927 | * - $sort_direction - ASC / DESC |
||
| 928 | * - $start_date - Ymd |
||
| 929 | * - $end_date - Ymd |
||
| 930 | * - $class - assign a html class to the view |
||
| 931 | * - $offset (optional) - This is the start point in the current data set (0 index based). |
||
| 932 | * @param int $form_id Gravity Forms Form ID |
||
| 933 | * @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 |
||
| 934 | */ |
||
| 935 | 1 | public static function get_view_entries( $args, $form_id ) { |
|
| 972 | |||
| 973 | /** |
||
| 974 | * Get an array of search parameters formatted as Gravity Forms requires |
||
| 975 | * |
||
| 976 | * Results are filtered by `gravityview_get_entries` and `gravityview_get_entries_{View ID}` filters |
||
| 977 | * |
||
| 978 | * @uses GravityView_frontend::get_search_criteria |
||
| 979 | * @uses GravityView_frontend::get_search_criteria_paging |
||
| 980 | * |
||
| 981 | * @since 1.20 |
||
| 982 | * |
||
| 983 | * @see \GV\View_Settings::defaults For $args options |
||
| 984 | * |
||
| 985 | * @param array $args Array of View settings, as structured in \GV\View_Settings::defaults |
||
| 986 | * @param int $form_id Gravity Forms form ID to search |
||
| 987 | * |
||
| 988 | * @return array With `search_criteria`, `sorting`, `paging`, `cache` keys |
||
| 989 | */ |
||
| 990 | 73 | public static function get_view_entries_parameters( $args = array(), $form_id = 0 ) { |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Get the paging array for the View |
||
| 1048 | * |
||
| 1049 | * @since 1.19.5 |
||
| 1050 | * |
||
| 1051 | * @param $args |
||
| 1052 | * @param int $form_id |
||
| 1053 | */ |
||
| 1054 | 73 | public static function get_search_criteria_paging( $args ) { |
|
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Updates the View sorting criteria |
||
| 1089 | * |
||
| 1090 | * @since 1.7 |
||
| 1091 | * |
||
| 1092 | * @param array $args View settings. Required to have `sort_field` and `sort_direction` keys |
||
| 1093 | * @param int $form_id The ID of the form used to sort |
||
| 1094 | * @return array $sorting Array with `key`, `direction` and `is_numeric` keys |
||
| 1095 | */ |
||
| 1096 | 73 | public static function updateViewSorting( $args, $form_id ) { |
|
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Override sorting per field |
||
| 1178 | * |
||
| 1179 | * Currently only modifies sorting ID when sorting by the full name. Sorts by first name. |
||
| 1180 | * Use the `gravityview/sorting/full-name` filter to override. |
||
| 1181 | * |
||
| 1182 | * @todo Filter from GravityView_Field |
||
| 1183 | * @since 1.7.4 |
||
| 1184 | * @internal Hi developer! Although this is public, don't call this method; we're going to replace it. |
||
| 1185 | * |
||
| 1186 | * @param int|string|array $sort_field_id Field used for sorting (`id` or `1.2`), or an array for multisorts |
||
| 1187 | * @param int $form_id GF Form ID |
||
| 1188 | * |
||
| 1189 | * @return string Possibly modified sorting ID |
||
| 1190 | */ |
||
| 1191 | 5 | public static function _override_sorting_id_by_field_type( $sort_field_id, $form_id ) { |
|
| 1287 | |||
| 1288 | /** |
||
| 1289 | * Verify if user requested a single entry view |
||
| 1290 | * @since 2.3.3 Added return null |
||
| 1291 | * @return boolean|string|null false if not, single entry slug if true, null if \GV\Entry doesn't exist yet |
||
| 1292 | */ |
||
| 1293 | 26 | public static function is_single_entry() { |
|
| 1322 | |||
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Register styles and scripts |
||
| 1326 | * |
||
| 1327 | * @access public |
||
| 1328 | * @return void |
||
| 1329 | */ |
||
| 1330 | 1 | public function add_scripts_and_styles() { |
|
| 1331 | 1 | global $post, $posts; |
|
| 1332 | // enqueue template specific styles |
||
| 1333 | 1 | if ( $this->getGvOutputData() ) { |
|
| 1334 | |||
| 1335 | 1 | $views = $this->getGvOutputData()->get_views(); |
|
| 1336 | |||
| 1337 | 1 | foreach ( $views as $view_id => $data ) { |
|
| 1338 | 1 | $view = \GV\View::by_id( $data['id'] ); |
|
| 1339 | 1 | $view_id = $view->ID; |
|
| 1340 | 1 | $template_id = gravityview_get_template_id( $view->ID ); |
|
| 1341 | 1 | $data = $view->as_data(); |
|
| 1342 | |||
| 1343 | /** |
||
| 1344 | * Don't enqueue the scripts or styles if it's not going to be displayed. |
||
| 1345 | * @since 1.15 |
||
| 1346 | */ |
||
| 1347 | 1 | if( is_user_logged_in() && false === GVCommon::has_cap( 'read_gravityview', $view_id ) ) { |
|
| 1348 | continue; |
||
| 1349 | } |
||
| 1350 | |||
| 1351 | // By default, no thickbox |
||
| 1352 | 1 | $js_dependencies = array( 'jquery', 'gravityview-jquery-cookie' ); |
|
| 1353 | 1 | $css_dependencies = array(); |
|
| 1354 | |||
| 1355 | 1 | $lightbox = $view->settings->get( 'lightbox' ); |
|
| 1356 | |||
| 1357 | // If the thickbox is enqueued, add dependencies |
||
| 1358 | 1 | if ( $lightbox ) { |
|
| 1359 | |||
| 1360 | 1 | global $wp_filter; |
|
| 1361 | |||
| 1362 | 1 | if ( ! empty( $wp_filter[ 'gravity_view_lightbox_script' ] ) ) { |
|
| 1363 | gravityview()->log->warning( 'gravity_view_lightbox_script filter is deprecated use gravityview_lightbox_script instead' ); |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | /** |
||
| 1367 | * @filter `gravity_view_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox` |
||
| 1368 | * @param string $script_slug If you want to use a different lightbox script, return the name of it here. |
||
| 1369 | * @deprecated 2.5.1 Naming. See `gravityview_lightbox_script` instead. |
||
| 1370 | */ |
||
| 1371 | 1 | $js_dependency = apply_filters( 'gravity_view_lightbox_script', 'thickbox' ); |
|
| 1372 | |||
| 1373 | /** |
||
| 1374 | * @filter `gravityview_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox` |
||
| 1375 | * @since 2.5.1 |
||
| 1376 | * @param string $script_slug If you want to use a different lightbox script, return the name of it here. |
||
| 1377 | * @param \GV\View The View. |
||
| 1378 | */ |
||
| 1379 | 1 | apply_filters( 'gravityview_lightbox_script', $js_dependency, $view ); |
|
| 1380 | 1 | $js_dependencies[] = $js_dependency; |
|
| 1381 | |||
| 1382 | 1 | if ( ! empty( $wp_filter[ 'gravity_view_lightbox_style' ] ) ) { |
|
| 1383 | gravityview()->log->warning( 'gravity_view_lightbox_style filter is deprecated use gravityview_lightbox_style instead' ); |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * @filter `gravity_view_lightbox_style` Modify the lightbox CSS slug. Default: `thickbox` |
||
| 1388 | * @param string $script_slug If you want to use a different lightbox script, return the name of its CSS file here. |
||
| 1389 | * @deprecated 2.5.1 Naming. See `gravityview_lightbox_style` instead. |
||
| 1390 | */ |
||
| 1391 | 1 | $css_dependency = apply_filters( 'gravity_view_lightbox_style', 'thickbox' ); |
|
| 1392 | |||
| 1393 | /** |
||
| 1394 | * @filter `gravityview_lightbox_script` Override the lightbox script to enqueue. Default: `thickbox` |
||
| 1395 | * @since 2.5.1 |
||
| 1396 | * @param string $script_slug If you want to use a different lightbox script, return the name of it here. |
||
| 1397 | * @param \GV\View The View. |
||
| 1398 | */ |
||
| 1399 | 1 | $css_dependency = apply_filters( 'gravityview_lightbox_style', $css_dependency, $view ); |
|
| 1400 | 1 | $css_dependencies[] = $css_dependency; |
|
| 1401 | } |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * If the form has checkbox fields, enqueue dashicons |
||
| 1405 | * @see https://github.com/katzwebservices/GravityView/issues/536 |
||
| 1406 | * @since 1.15 |
||
| 1407 | */ |
||
| 1408 | 1 | if( gravityview_view_has_single_checkbox_or_radio( $data['form'], $data['fields'] ) ) { |
|
| 1409 | $css_dependencies[] = 'dashicons'; |
||
| 1410 | } |
||
| 1411 | |||
| 1412 | 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 ); |
|
| 1413 | |||
| 1414 | 1 | $script_debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
|
| 1415 | |||
| 1416 | 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 ); |
|
| 1417 | |||
| 1418 | 1 | wp_enqueue_script( 'gravityview-fe-view' ); |
|
| 1419 | |||
| 1420 | 1 | if ( ! empty( $data['atts']['sort_columns'] ) ) { |
|
| 1421 | wp_enqueue_style( 'gravityview_font', plugins_url( 'assets/css/font.css', GRAVITYVIEW_FILE ), $css_dependencies, GravityView_Plugin::version, 'all' ); |
||
| 1422 | } |
||
| 1423 | |||
| 1424 | 1 | $this->enqueue_default_style( $css_dependencies ); |
|
| 1425 | |||
| 1426 | 1 | self::add_style( $template_id ); |
|
| 1427 | } |
||
| 1428 | |||
| 1429 | 1 | if ( 'wp_print_footer_scripts' === current_filter() ) { |
|
| 1430 | |||
| 1431 | $js_localization = array( |
||
| 1432 | 'cookiepath' => COOKIEPATH, |
||
| 1433 | 'clear' => _x( 'Clear', 'Clear all data from the form', 'gravityview' ), |
||
| 1434 | 'reset' => _x( 'Reset', 'Reset the search form to the state that existed on page load', 'gravityview' ), |
||
| 1435 | ); |
||
| 1436 | |||
| 1437 | /** |
||
| 1438 | * @filter `gravityview_js_localization` Modify the array passed to wp_localize_script() |
||
| 1439 | * @param array $js_localization The data padded to the Javascript file |
||
| 1440 | * @param array $views Array of View data arrays with View settings |
||
| 1441 | */ |
||
| 1442 | $js_localization = apply_filters( 'gravityview_js_localization', $js_localization, $views ); |
||
| 1443 | |||
| 1444 | wp_localize_script( 'gravityview-fe-view', 'gvGlobals', $js_localization ); |
||
| 1445 | } |
||
| 1446 | } |
||
| 1447 | 1 | } |
|
| 1448 | |||
| 1449 | /** |
||
| 1450 | * Handle enqueuing the `gravityview_default_style` stylesheet |
||
| 1451 | * |
||
| 1452 | * @since 1.17 |
||
| 1453 | * |
||
| 1454 | * @param array $css_dependencies Dependencies for the `gravityview_default_style` stylesheet |
||
| 1455 | * |
||
| 1456 | * @return void |
||
| 1457 | */ |
||
| 1458 | private function enqueue_default_style( $css_dependencies = array() ) { |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * @filter `gravityview_use_legacy_search_css` Should GravityView use the legacy Search Bar stylesheet (from before Version 1.17)? |
||
| 1462 | * @since 1.17 |
||
| 1463 | * @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` |
||
| 1464 | */ |
||
| 1465 | $use_legacy_search_style = apply_filters( 'gravityview_use_legacy_search_style', false ); |
||
| 1466 | |||
| 1467 | $rtl = is_rtl() ? '-rtl' : ''; |
||
| 1468 | |||
| 1469 | $css_file_base = $use_legacy_search_style ? 'gv-legacy-search' : 'gv-default-styles'; |
||
| 1470 | |||
| 1471 | $path = gravityview_css_url( $css_file_base . $rtl . '.css' ); |
||
| 1472 | |||
| 1473 | wp_enqueue_style( 'gravityview_default_style', $path, $css_dependencies, GravityView_Plugin::version, 'all' ); |
||
| 1474 | } |
||
| 1475 | |||
| 1476 | /** |
||
| 1477 | * Add template extra style if exists |
||
| 1478 | * @param string $template_id |
||
| 1479 | */ |
||
| 1480 | public static function add_style( $template_id ) { |
||
| 1481 | |||
| 1482 | if ( ! empty( $template_id ) && wp_style_is( 'gravityview_style_' . $template_id, 'registered' ) ) { |
||
| 1483 | gravityview()->log->debug( 'Adding extra template style for {template_id}', array( 'template_id' => $template_id ) ); |
||
| 1484 | wp_enqueue_style( 'gravityview_style_' . $template_id ); |
||
| 1485 | } elseif ( empty( $template_id ) ) { |
||
| 1486 | gravityview()->log->error( 'Cannot add template style; template_id is empty' ); |
||
| 1487 | } else { |
||
| 1488 | gravityview()->log->error( 'Cannot add template style; {template_id} is not registered', array( 'template_id' => 'gravityview_style_' . $template_id ) ); |
||
| 1489 | } |
||
| 1490 | |||
| 1491 | } |
||
| 1492 | |||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Inject the sorting links on the table columns |
||
| 1496 | * |
||
| 1497 | * Callback function for hook 'gravityview/template/field_label' |
||
| 1498 | * @see GravityView_API::field_label() (in includes/class-api.php) |
||
| 1499 | * |
||
| 1500 | * @since 1.7 |
||
| 1501 | * |
||
| 1502 | * @param string $label Field label |
||
| 1503 | * @param array $field Field settings |
||
| 1504 | * @param array $form Form object |
||
| 1505 | * |
||
| 1506 | * @return string Field Label |
||
| 1507 | */ |
||
| 1508 | public function add_columns_sort_links( $label = '', $field, $form ) { |
||
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Checks if field (column) is sortable |
||
| 1554 | * |
||
| 1555 | * @param string $field Field settings |
||
| 1556 | * @param array $form Gravity Forms form array |
||
| 1557 | * |
||
| 1558 | * @since 1.7 |
||
| 1559 | * |
||
| 1560 | * @return bool True: Yes, field is sortable; False: not sortable |
||
| 1561 | */ |
||
| 1562 | 1 | public function is_field_sortable( $field_id = '', $form = array() ) { |
|
| 1592 | |||
| 1593 | } |
||
| 1594 | |||
| 1595 | GravityView_frontend::getInstance(); |
||
| 1596 | |||
| 1599 |