GravityView  1.19.4
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  * @inheritDoc
17  */
19 
20  /**
21  * @inheritDoc
22  * @since 1.19.2
23  */
24  protected $script_handles = array(
25  'wpml-cpi-scripts',
26  'sitepress-scripts',
27  'sitepress-post-edit',
28  'sitepress-post-list-quickedit',
29  'sitepress-languages',
30  'sitepress-troubleshooting',
31  );
32 
33  /**
34  * @inheritDoc
35  * @since 1.19.2
36  */
37  protected $style_handles = array(
38  'sitepress-style',
39  'wpml-select-2',
40  'wpml-tm-styles',
41  'wpml-tm-queue',
42  'wpml-dialog',
43  'wpml-tm-editor-css',
44  'otgs-dialogs',
45  'otgs-ico',
46  );
47 
48  /**
49  * @inheritDoc
50  * @since 1.19.2
51  */
52  protected $constant_name = 'ICL_SITEPRESS_VERSION';
53 
54  /**
55  * Add filters for WPML links
56  *
57  * @since 1.19.4
58  */
59  public function add_hooks() {
60 
61  parent::add_hooks();
62 
63  add_filter( 'icl_ls_languages', array( $this, 'wpml_ls_filter' ) );
64 
65  add_filter( 'gravityview_directory_link', array( $this, 'filter_gravityview_back_link') );
66  }
67 
68  /**
69  * Add WPML filters to GravityView directory link
70  *
71  * This also modifies all the Edit Entry, Cancel Edit, Go Back links
72  *
73  * @since 1.19.4
74  *
75  * @see GravityView_API::directory_link
76  * @uses WPML_URL_Filters::permalink_filter
77  *
78  * @param string $link Permalink to the GravityView directory, without language params
79  *
80  * @return string $link, with language params added by WPML
81  */
82  function filter_gravityview_back_link( $link ) {
83  global $wpml_url_filters;
84 
85  $link = $wpml_url_filters->permalink_filter( $link, GravityView_frontend::getInstance()->getPostId() );
86 
87  return $link;
88  }
89 
90  /**
91  * Remove WPML permalink filters
92  *
93  * @since 1.19.4
94  *
95  * @return void
96  */
97  private function remove_url_hooks() {
98  global $wpml_url_filters;
99 
100  $wpml_url_filters->remove_global_hooks();
101 
102  if ( $wpml_url_filters->frontend_uses_root() === true ) {
103  remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1 );
104  } else {
105  remove_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1 );
106  }
107  }
108 
109  /**
110  * Add the WPML permalink filters back in
111  *
112  * @since 1.19.4
113  *
114  * @return void
115  */
116  private function add_url_hooks() {
117  global $wpml_url_filters;
118 
119  $wpml_url_filters->add_global_hooks();
120 
121  if ( $wpml_url_filters->frontend_uses_root() === true ) {
122  add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter_root' ), 1, 2 );
123  } else {
124  add_filter( 'page_link', array( $wpml_url_filters, 'page_link_filter' ), 1, 2 );
125  }
126  }
127 
128  /**
129  * Modify the language links to fix /entry/ var from being stripped
130  *
131  * @since 1.19.4
132  *
133  * @param array $languages Array of active languages with their details
134  *
135  * @return array If currently a single entry screen, re-generate URL after removing WPML filters
136  */
137  public function wpml_ls_filter( $languages ) {
138  global $sitepress, $post;
139 
140  if ( $entry_slug = GravityView_frontend::getInstance()->getSingleEntry() ) {
141 
142  $trid = $sitepress->get_element_trid( $post->ID );
143  $translations = $sitepress->get_element_translations( $trid );
144 
145  $this->remove_url_hooks();
146 
147  if( $translations ) {
148  foreach ( $languages as $lang_code => $language ) {
149 
150  $lang_post_id = $translations[ $lang_code ]->element_id;
151 
152  $entry_link = GravityView_API::entry_link( $entry_slug, $lang_post_id );
153 
154  if ( ! empty( $translations[ $lang_code ]->original ) ) {
155 
156  // The original doesn't need a language parameter
157  $languages[ $lang_code ]['url'] = remove_query_arg( 'lang', $entry_link );
158 
159  } elseif ( $entry_link ) {
160 
161  // Every other language does
162  $languages[ $lang_code ]['url'] = add_query_arg( array( 'lang' => $lang_code ), $entry_link );
163 
164  }
165  }
166  }
167 
168  $this->add_url_hooks();
169  }
170 
171  return $languages;
172  }
173 
174 }
175 
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:642
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.
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.