GravityView  1.22.6
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-theme-hooks-wpml.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Add WPML compatibility to GravityView, including registering scripts and styles to GravityView no-conflict list
4  *
5  * @file class-gravityview-theme-hooks-wpml.php
6  * @package GravityView
7  * @license GPL2+
8  * @author Katz Web Services, Inc.
9  * @link http://gravityview.co
10  * @copyright Copyright 2015, Katz Web Services, Inc.
11  *
12  * @since 1.19.2
13  */
14 
15 /**
16  * Requires WPML 3.6.2 or newer
17  *
18  * @inheritDoc
19  */
21 
22  /**
23  * @inheritDoc
24  * @since 1.19.2
25  */
26  protected $script_handles = array(
27  'wpml-cpi-scripts',
28  'sitepress-scripts',
29  'sitepress-post-edit',
30  'sitepress-post-list-quickedit',
31  'sitepress-languages',
32  'sitepress-troubleshooting',
33  );
34 
35  /**
36  * @inheritDoc
37  * @since 1.19.2
38  */
39  protected $style_handles = array(
40  'sitepress-style',
41  'wpml-select-2',
42  'wpml-tm-styles',
43  'wpml-tm-queue',
44  'wpml-dialog',
45  'wpml-tm-editor-css',
46  'otgs-dialogs',
47  'otgs-ico',
48  );
49 
50  /**
51  * @inheritDoc
52  * @since 1.19.2
53  */
54  protected $constant_name = 'ICL_SITEPRESS_VERSION';
55 
56  /**
57  * Add filters for WPML links
58  *
59  * @since 1.19.4
60  */
61  public function add_hooks() {
62 
63  parent::add_hooks();
64 
65  add_filter( 'icl_ls_languages', array( $this, 'wpml_ls_filter' ) );
66 
67  add_filter( 'gravityview_directory_link', array( $this, 'filter_gravityview_back_link') );
68  }
69 
70  /**
71  * Add WPML filters to GravityView directory link
72  *
73  * This also modifies all the Edit Entry, Cancel Edit, Go Back links
74  *
75  * @since 1.19.4
76  *
77  * @see GravityView_API::directory_link
78  * @uses WPML_URL_Filters::permalink_filter
79  *
80  * @param string $link Permalink to the GravityView directory, without language params
81  *
82  * @return string $link, with language params added by WPML
83  */
84  function filter_gravityview_back_link( $link ) {
85  global $wpml_url_filters;
86 
87  if( $wpml_url_filters ) {
88  $link = $wpml_url_filters->permalink_filter( $link, GravityView_frontend::getInstance()->getPostId() );
89  }
90 
91  return $link;
92  }
93 
94  /**
95  * Remove WPML permalink filters
96  *
97  * @since 1.19.4
98  *
99  * @return void
100  */
101  private function remove_url_hooks() {
102  global $wpml_url_filters;
103 
104  if( ! $wpml_url_filters ) {
105  return;
106  }
107 
108  // WPML 3.6.1 and lower does not have this method, avoid a fatal error.
109  if ( method_exists( $wpml_url_filters, 'remove_global_hooks' ) ) {
110  $wpml_url_filters->remove_global_hooks();
111  } else {
112  do_action( 'gravityview_log_error', '[GravityView_Theme_Hooks_WPML::remove_url_hooks] WPML missing remove_global_hooks method. Needs version 3.6.2+.' );
113  }
114 
115  if ( $wpml_url_filters->frontend_uses_root() === true ) {
116  remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1 );
117  } else {
118  remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1 );
119  }
120  }
121 
122  /**
123  * Add the WPML permalink filters back in
124  *
125  * @since 1.19.4
126  *
127  * @return void
128  */
129  private function add_url_hooks() {
130  global $wpml_url_filters;
131 
132  if( ! $wpml_url_filters ) {
133  return;
134  }
135 
136  // WPML 3.6.1 and lower does not have this method, avoid a fatal error.
137  if ( method_exists( $wpml_url_filters, 'add_global_hooks' ) ) {
138  $wpml_url_filters->add_global_hooks();
139  } else {
140  do_action( 'gravityview_log_error', '[GravityView_Theme_Hooks_WPML::add_url_hooks] WPML missing add_global_hooks method. Needs version 3.6.2+.' );
141  }
142 
143  if ( $wpml_url_filters->frontend_uses_root() === true ) {
144  add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1, 2 );
145  } else {
146  add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1, 2 );
147  }
148  }
149 
150  /**
151  * Modify the language links to fix /entry/ var from being stripped
152  *
153  * @since 1.19.4
154  *
155  * @param array $languages Array of active languages with their details
156  *
157  * @return array If currently a single entry screen, re-generate URL after removing WPML filters
158  */
159  public function wpml_ls_filter( $languages ) {
160 
161  /**
162  * @global SitePress $sitepress
163  * @global WP_Post $post
164  * @global WPML_URL_Converter $wpml_url_converter
165  */
166  global $sitepress, $post, $wpml_url_converter;
167 
168  if ( $entry_slug = GravityView_frontend::getInstance()->getSingleEntry() ) {
169 
170  if ( ! method_exists( $sitepress, 'get_setting' ) ) {
171  do_action( 'gravityview_log_error', __METHOD__ . ': This version of WPML is outdated and does not include the required method get_setting().' );
172  return $languages;
173  }
174 
175  $trid = $sitepress->get_element_trid( $post->ID );
176  $translations = $sitepress->get_element_translations( $trid );
177  $language_url_setting = $sitepress->get_setting( 'language_negotiation_type' );
178 
179  $this->remove_url_hooks();
180 
181  if( $translations ) {
182  foreach ( $languages as $lang_code => $language ) {
183 
184  if( ! isset( $translations[ $lang_code ] ) || ! is_object( $translations[ $lang_code ] ) ) {
185  continue;
186  }
187 
188  $lang_post_id = $translations[ $lang_code ]->element_id;
189 
190  $entry_link = GravityView_API::entry_link( $entry_slug, $lang_post_id );
191 
192  // How is WPML handling the language?
193  switch ( intval( $language_url_setting ) ) {
194 
195  // Subdomains or directories
196  case 1:
197  case 2:
198  // For sites using directories or sub-domains for languages, rewrite base URL
199  $entry_link = $wpml_url_converter->convert_url( $entry_link, $lang_code );
200  break;
201 
202  // URL Parameters
203  case 3:
204  default:
205  if ( ! empty( $translations[ $lang_code ]->original ) ) {
206 
207  // The original language doesn't need a language parameter
208  $entry_link = remove_query_arg( 'lang', $entry_link );
209 
210  } elseif ( $entry_link ) {
211 
212  // Every other language does
213  $entry_link = add_query_arg( array( 'lang' => $lang_code ), $entry_link );
214  }
215  break;
216  }
217 
218  $languages[ $lang_code ]['url'] = $entry_link;
219  }
220  }
221 
222  $this->add_url_hooks();
223  }
224 
225  return $languages;
226  }
227 
228 }
229 
filter_gravityview_back_link( $link)
Add WPML filters to GravityView directory link.
wpml_ls_filter( $languages)
Modify the language links to fix /entry/ var from being stripped.
add_url_hooks()
Add the WPML permalink filters back in.
static entry_link( $entry, $post_id=NULL, $add_directory_args=true)
return href for single entry
Definition: class-api.php:663
global $post
$entry_slug
Definition: notes.php:30
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
remove_url_hooks()
Remove WPML permalink filters.
static getInstance()
Get the one true instantiated self.