]> _ Git - physioassist-wordpress.git/blob
b250c964aa295eaa232d6b7bc72ae6e4249678b5
[physioassist-wordpress.git] /
1 <?php
2
3 /**
4  * WP SEO by Yoast sitemap filter class
5  *
6  * @version 1.0.2
7  */
8 class WPML_WPSEO_XML_Sitemaps_Filter {
9         /** @var  SitePress $sitepress */
10         protected $sitepress;
11
12         /**
13          * WPML_URL_Converter object.
14          *
15          * @var WPML_URL_Converter
16          */
17         private $wpml_url_converter;
18
19         /**
20          * @var WPML_Debug_BackTrace
21          */
22         private $back_trace;
23
24         /**
25          * WPSEO_XML_Sitemaps_Filter constructor.
26          *
27          * @param SitePress            $sitepress
28          * @param stdClass             $wpml_url_converter
29          * @param WPML_Debug_BackTrace $back_trace
30          */
31         public function __construct( $sitepress, $wpml_url_converter, WPML_Debug_BackTrace $back_trace = null ) {
32                 $this->sitepress          = $sitepress;
33                 $this->wpml_url_converter = $wpml_url_converter;
34                 $this->back_trace         = $back_trace;
35         }
36
37         public function init_hooks() {
38                 global $wpml_query_filter;
39
40                 if ( $this->is_per_domain() ) {
41                         add_filter( 'wpml_get_home_url', array( $this, 'get_home_url_filter' ), 10, 1 );
42                         add_filter( 'wpseo_posts_join', array( $wpml_query_filter, 'filter_single_type_join' ), 10, 2 );
43                         add_filter( 'wpseo_posts_where', array( $wpml_query_filter, 'filter_single_type_where' ), 10, 2 );
44                         add_filter( 'wpseo_typecount_join', array( $wpml_query_filter, 'filter_single_type_join' ), 10, 2 );
45                         add_filter( 'wpseo_typecount_where', array( $wpml_query_filter, 'filter_single_type_where' ), 10, 2 );
46                 } else {
47                         add_filter( 'wpseo_sitemap_page_content', array( $this, 'add_languages_to_sitemap' ) );
48                         // Remove posts under hidden language.
49                         add_filter( 'wpseo_xml_sitemap_post_url', array( $this, 'exclude_hidden_language_posts' ), 10, 2 );
50                 }
51
52                 if ( $this->is_per_directory() ) {
53                         add_filter( 'wpml_get_home_url', array( $this, 'maybe_return_original_url_in_get_home_url_filter' ), 10, 2 );
54                 }
55
56                 add_filter( 'wpseo_enable_xml_sitemap_transient_caching', array( $this, 'transient_cache_filter' ), 10, 0 );
57                 add_filter( 'wpseo_build_sitemap_post_type', array( $this, 'wpseo_build_sitemap_post_type_filter' ) );
58                 add_action( 'wpseo_xmlsitemaps_config', array( $this, 'list_domains' ) );
59                 add_filter( 'wpseo_sitemap_entry', array( $this, 'exclude_translations_of_static_home_page' ), 10, 3 );
60         }
61
62         /**
63          * Add home page urls for languages to sitemap.
64          * Do this only if configuration language per domain option is not used.
65          */
66         public function add_languages_to_sitemap() {
67                 $output = '';
68                 $default_lang = $this->sitepress->get_default_language();
69                 $active_langs = $this->sitepress->get_active_languages();
70                 unset( $active_langs[ $default_lang ] );
71
72                 foreach ( $active_langs as $lang_code => $lang_data ) {
73                         $output .= $this->sitemap_url_filter( $this->wpml_url_converter->convert_url( home_url( '/' ), $lang_code ) );
74                 }
75                 return $output;
76         }
77
78         /**
79          * Update home_url for language per-domain configuration to return correct URL in sitemap.
80          *
81          * @param string $home_url
82          *
83          * @return bool|mixed|string
84          */
85         public function get_home_url_filter( $home_url ) {
86                 return $this->wpml_url_converter->convert_url( $home_url, $this->sitepress->get_current_language() );
87         }
88
89         public function list_domains() {
90                 $ls_languages = $this->sitepress->get_ls_languages();
91                 if ( $ls_languages && $this->is_per_domain() ) {
92
93                         echo '<h3>' . esc_html__( 'WPML', 'sitepress' ) . '</h3>';
94                         echo esc_html__( 'Sitemaps for each language can be accessed below. You need to submit all these sitemaps to Google.', 'sitepress' );
95                         echo '<table class="wpml-sitemap-translations" style="margin-left: 1em; margin-top: 1em;">';
96
97                         $base_style = "style=\"
98                         background-image:url('%s');
99                         background-repeat: no-repeat;
100                         background-position: 2px center;
101                         background-size: 16px;
102                         padding-left: 20px;
103                         width: 100%%;
104                         \"
105                         ";
106
107                         foreach ( $ls_languages as $lang ) {
108                                 $url = $lang['url'] . 'sitemap_index.xml';
109                                 echo '<tr>';
110                                 echo '<td>';
111                                 echo '<a ';
112                                 echo 'href="' . esc_url( $url ) . '" ';
113                                 echo 'target="_blank" ';
114                                 echo 'class="button-secondary" ';
115                                 echo sprintf( $base_style, esc_url( $lang['country_flag_url'] ) );
116                                 echo '>';
117                                 echo esc_html( $lang['translated_name'] );
118                                 echo '</a>';
119                                 echo '</td>';
120                                 echo '</tr>';
121                         }
122                         echo '</table>';
123                 }
124         }
125
126         /**
127          * @return bool
128          */
129         public function is_per_domain() {
130                 return WPML_LANGUAGE_NEGOTIATION_TYPE_DOMAIN === (int) $this->sitepress->get_setting( 'language_negotiation_type' );
131         }
132
133         /**
134          * @return bool
135          */
136         private function is_per_directory() {
137                 return WPML_LANGUAGE_NEGOTIATION_TYPE_DIRECTORY === (int) $this->sitepress->get_setting( 'language_negotiation_type' );
138         }
139
140         public function transient_cache_filter() {
141                 return false;
142         }
143
144         public function wpseo_build_sitemap_post_type_filter( $type ) {
145                 global $sitepress_settings;
146                 // Before to build the sitemap and as we are on front-end
147                 // just make sure the links won't be translated
148                 // The setting should not be updated in DB
149                 $sitepress_settings['auto_adjust_ids'] = 0;
150
151                 if ( ! $this->is_per_domain() ) {
152                         remove_filter( 'terms_clauses', array( $this->sitepress, 'terms_clauses' ) );
153                 }
154
155                 remove_filter( 'category_link', array( $this->sitepress, 'category_link_adjust_id' ), 1 );
156
157                 return $type;
158         }
159
160         /**
161          * Exclude posts under hidden language.
162          *
163          * @param  string   $url  Post URL.
164          * @param  stdClass $post Object with some post information.
165          *
166          * @return string
167          */
168         public function exclude_hidden_language_posts( $url, $post ) {
169                 // Check that at least ID is set in post object.
170                 if ( ! isset( $post->ID ) ) {
171                         return $url;
172                 }
173
174                 // Get list of hidden languages.
175                 $hidden_languages = $this->sitepress->get_setting( 'hidden_languages', array() );
176
177                 // If there are no hidden languages return original URL.
178                 if ( empty( $hidden_languages ) ) {
179                         return $url;
180                 }
181
182                 // Get language information for post.
183                 $language_info = $this->sitepress->post_translations()->get_element_lang_code( $post->ID );
184
185                 // If language code is one of the hidden languages return empty string to skip the post.
186                 if ( in_array( $language_info, $hidden_languages, true ) ) {
187                         return '';
188                 }
189
190                 return $url;
191         }
192
193         /**
194          * Convert URL to sitemap entry format.
195          *
196          * @param string $url URl to prepare for sitemap.
197          *
198          * @return string
199          */
200         public function sitemap_url_filter( $url ) {
201                 $url = htmlspecialchars( $url );
202
203                 $output = "\t<url>\n";
204                 $output .= "\t\t<loc>" . $url . "</loc>\n";
205                 $output .= '';
206                 $output .= "\t\t<changefreq>daily</changefreq>\n";
207                 $output .= "\t\t<priority>1.0</priority>\n";
208                 $output .= "\t</url>\n";
209
210                 return $output;
211         }
212
213         /**
214          * @param $url
215          * @param $type
216          * @param $post_object
217          *
218          * @return string|bool
219          */
220         public function exclude_translations_of_static_home_page( $url, $type, $post_object ) {
221                 if ( 'post' !== $type || $this->is_per_domain() ) {
222                         return $url;
223                 }
224                 $page_on_front = (int) get_option( 'page_on_front' );
225                 if ( $page_on_front ) {
226                         $translations = $this->sitepress->post_translations()->get_element_translations( $page_on_front );
227                         unset( $translations[ $this->sitepress->get_default_language() ] );
228                         if ( in_array( $post_object->ID, $translations, true ) ) {
229                                 $url = false;
230                         }
231                 }
232                 return $url;
233         }
234
235         /**
236          * @param string $home_url
237          * @param string $original_url
238          *
239          * @return string
240          */
241         public function maybe_return_original_url_in_get_home_url_filter( $home_url, $original_url ) {
242                 $places = array(
243                         array( 'WPSEO_Post_Type_Sitemap_Provider', 'get_home_url' ),
244                         array( 'WPSEO_Sitemaps_Router', 'get_base_url' ),
245                         array( 'WPSEO_Sitemaps_Renderer', '__construct' ),
246                 );
247
248                 foreach ( $places as $place ) {
249                         if ( $this->get_back_trace()->is_class_function_in_call_stack( $place[0], $place[1] ) ) {
250                                 return $original_url;
251                         }
252                 }
253
254                 return $home_url;
255         }
256
257         /**
258          * @return WPML_Debug_BackTrace
259          */
260         private function get_back_trace() {
261                 if ( null === $this->back_trace ) {
262                         $this->back_trace = new WPML_Debug_BackTrace( phpversion() );
263                 }
264
265                 return $this->back_trace;
266         }
267 }