GravityView  2.17
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 GravityView <[email protected]>
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  */
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  gravityview()->log->error( '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  gravityview()->log->error( '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 ( ! method_exists( $sitepress, 'get_setting' ) ) {
169  gravityview()->log->error( 'This version of WPML is outdated and does not include the required method get_setting().' );
170 
171  return $languages;
172  }
173 
174  $entry = gravityview()->request->is_entry();
175 
176  if ( ! $entry ) {
177  return $languages;
178  }
179 
180  $entry_slug = $entry->get_slug();
181 
182  $trid = $sitepress->get_element_trid( $post->ID );
183  $translations = $sitepress->get_element_translations( $trid );
184 
185  if ( ! $translations ) {
186  return $languages;
187  }
188 
189  $language_url_setting = $sitepress->get_setting( 'language_negotiation_type' );
190 
191  $this->remove_url_hooks();
192 
193  foreach ( $languages as $lang_code => $language ) {
194 
195  if ( ! isset( $translations[ $lang_code ] ) || ! is_object( $translations[ $lang_code ] ) ) {
196  continue;
197  }
198 
199  $lang_post_id = $translations[ $lang_code ]->element_id;
200 
201  $entry_link = GravityView_API::entry_link( $entry_slug, $lang_post_id );
202 
203  // How is WPML handling the language?
204  switch ( intval( $language_url_setting ) ) {
205 
206  // Subdomains or directories
207  case 1:
208  case 2:
209  // For sites using directories or sub-domains for languages, rewrite base URL
210  $entry_link = $wpml_url_converter->convert_url( $entry_link, $lang_code );
211  break;
212 
213  // URL Parameters
214  case 3:
215  default:
216  if ( ! empty( $translations[ $lang_code ]->original ) ) {
217 
218  // The original language doesn't need a language parameter
219  $entry_link = remove_query_arg( 'lang', $entry_link );
220 
221  } elseif ( $entry_link ) {
222 
223  // Every other language does
224  $entry_link = add_query_arg( array( 'lang' => $lang_code ), $entry_link );
225  }
226  break;
227  }
228 
229  $languages[ $lang_code ]['url'] = $entry_link;
230  }
231 
232  $this->add_url_hooks();
233 
234  return $languages;
235  }
236 
237 }
238 
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.
if( $add_query_args) $link
global $post
Definition: delete-entry.php:7
gravityview()
The main GravityView wrapper function.
$entry_slug
Definition: notes.php:30
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
$entry
Definition: notes.php:27
static entry_link( $entry, $post_id=NULL, $add_directory_args=true, $view_id=0)
return href for single entry
Definition: class-api.php:656
remove_url_hooks()
Remove WPML permalink filters.
static getInstance()
Get the one true instantiated self.