PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/sitepress-multilingual-cms/inc/absolute-links/absolute-links.class.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 517 lines | 401 code | 99 blank | 17 comment | 111 complexity | c401e26027ed64d82c46236bb7a84a06 MD5 | raw file
  1. <?php
  2. class AbsoluteLinks{
  3. public $custom_post_query_vars = array();
  4. public $taxonomies_query_vars = array();
  5. function __construct() {
  6. //init_query_vars is using $wp_taxonomies
  7. //we have to change priority of our action
  8. //to make sure that all custom taxonomies are already registered
  9. add_action( 'init', array( $this, 'init_query_vars' ), 1000 );
  10. }
  11. function init_query_vars() {
  12. global $wp_post_types, $wp_taxonomies;
  13. //custom posts query vars
  14. foreach ( $wp_post_types as $k => $v ) {
  15. if ( $k === 'post' || $k === 'page' ) {
  16. continue;
  17. }
  18. if ( $v->query_var ) {
  19. $this->custom_post_query_vars[ $k ] = $v->query_var;
  20. }
  21. }
  22. //taxonomies query vars
  23. foreach ( $wp_taxonomies as $k => $v ) {
  24. if ( $k === 'category' ) {
  25. continue;
  26. }
  27. if ( $k == 'post_tag' && !$v->query_var ) {
  28. $tag_base = get_option( 'tag_base', 'tag' );
  29. $v->query_var = $tag_base;
  30. }
  31. if ( $v->query_var ) {
  32. $this->taxonomies_query_vars[ $k ] = $v->query_var;
  33. }
  34. }
  35. }
  36. function _process_generic_text( $source_text, &$alp_broken_links ) {
  37. global $wpdb, $wp_rewrite, $sitepress, $sitepress_settings;
  38. $sitepress_settings = $sitepress->get_settings();
  39. $default_language = $sitepress->get_default_language();
  40. $current_language = $sitepress->get_current_language();
  41. $cache_key_args = array( $default_language, $current_language, md5( $source_text ), md5( implode( '', $alp_broken_links ) ) );
  42. $cache_key = md5( json_encode( $cache_key_args ) );
  43. $cache_group = '_process_generic_text';
  44. $found = false;
  45. $text = wp_cache_get( $cache_key, $cache_group, false, $found );
  46. if ( $found ) {
  47. return $text;
  48. }
  49. $filtered_icl_post_language = filter_input( INPUT_POST, 'icl_post_language', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
  50. $text = $source_text;
  51. if ( !isset( $wp_rewrite ) ) {
  52. require_once ABSPATH . WPINC . '/rewrite.php';
  53. $wp_rewrite = new WP_Rewrite();
  54. }
  55. if ( $current_language == $default_language ) {
  56. $rewrite = $wp_rewrite->wp_rewrite_rules();
  57. } else {
  58. remove_filter( 'option_rewrite_rules', array( $sitepress, 'rewrite_rules_filter' ) );
  59. if(class_exists('WPML_Slug_Translation')) {
  60. remove_filter( 'option_rewrite_rules', array( 'WPML_Slug_Translation', 'rewrite_rules_filter' ), 1 );
  61. }
  62. $rewrite = $wp_rewrite->wp_rewrite_rules();
  63. if(class_exists('WPML_Slug_Translation')) {
  64. add_filter( 'option_rewrite_rules', array( 'WPML_Slug_Translation', 'rewrite_rules_filter' ), 1, 1 );
  65. }
  66. }
  67. $rewrite = $this->all_rewrite_rules( $rewrite );
  68. $home_url = $sitepress->language_url( empty( $filtered_icl_post_language ) ? false : $filtered_icl_post_language );
  69. if ( $sitepress_settings[ 'language_negotiation_type' ] == 3 ) {
  70. $home_url = preg_replace( "#\?lang=([a-z-]+)#i", '', $home_url );
  71. }
  72. $home_url = str_replace( "?", "\?", $home_url );
  73. if ( $sitepress_settings[ 'urls' ][ 'directory_for_default_language' ] ) {
  74. $default_language = $sitepress->get_default_language();
  75. $home_url = str_replace( $default_language . "/", "", $home_url );
  76. }
  77. $int1 = preg_match_all( '@<a([^>]*)href="((' . rtrim( $home_url, '/' ) . ')?/([^"^>^\[^\]]+))"([^>]*)>@i', $text, $alp_matches1 );
  78. $int2 = preg_match_all( '@<a([^>]*)href=\'((' . rtrim( $home_url, '/' ) . ')?/([^\'^>^\[^\]]+))\'([^>]*)>@i', $text, $alp_matches2 );
  79. $alp_matches = array();
  80. for ( $i = 0; $i < 6; $i++ ) {
  81. $alp_matches[ $i ] = array_merge( (array)$alp_matches1[ $i ], (array)$alp_matches2[ $i ] );
  82. }
  83. if ( $int1 || $int2 ) {
  84. $url_parts = parse_url( $this->get_home_url_with_no_lang_directory() );
  85. $url_parts[ 'path' ] = isset( $url_parts[ 'path' ] ) ? $url_parts[ 'path' ] : '';
  86. foreach ( $alp_matches[ 4 ] as $k => $m ) {
  87. if ( 0 === strpos( $m, 'wp-content' ) ) {
  88. continue;
  89. }
  90. $lang = false;
  91. if ( $sitepress_settings[ 'language_negotiation_type' ] == 1 ) {
  92. $m_orig = $m;
  93. $exp = explode( '/', $m, 2 );
  94. $lang = $exp[ 0 ];
  95. if ( $this->does_lang_exist( $lang ) ) {
  96. $m = $exp[ 1 ];
  97. } else {
  98. $m = $m_orig;
  99. unset( $m_orig );
  100. $lang = false;
  101. }
  102. }
  103. $pathinfo = '';
  104. $req_uri = '/' . $m;
  105. $req_uri_array = explode( '?', $req_uri );
  106. $req_uri = $req_uri_array[ 0 ];
  107. $req_uri_params = '';
  108. if ( isset( $req_uri_array[ 1 ] ) ) {
  109. $req_uri_params = $req_uri_array[ 1 ];
  110. }
  111. // separate anchor
  112. $req_uri_array = explode( '#', $req_uri );
  113. $req_uri = $req_uri_array[ 0 ];
  114. $anchor = isset( $req_uri_array[ 1 ] ) ? $req_uri_array[ 1 ] : false;
  115. $home_path = parse_url( get_home_url() );
  116. if ( isset( $home_path[ 'path' ] ) ) {
  117. $home_path = $home_path[ 'path' ];
  118. } else {
  119. $home_path = '';
  120. }
  121. $home_path = trim( $home_path, '/' );
  122. $req_uri = str_replace( $pathinfo, '', rawurldecode( $req_uri ) );
  123. $req_uri = trim( $req_uri, '/' );
  124. $req_uri = preg_replace( "|^$home_path|", '', $req_uri );
  125. $req_uri = trim( $req_uri, '/' );
  126. $pathinfo = trim( $pathinfo, '/' );
  127. $pathinfo = preg_replace( "|^$home_path|", '', $pathinfo );
  128. $pathinfo = trim( $pathinfo, '/' );
  129. if ( !empty( $pathinfo ) && !preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
  130. $request = $pathinfo;
  131. } else {
  132. // If the request uri is the index, blank it out so that we don't try to match it against a rule.
  133. if ( $req_uri == $wp_rewrite->index ) {
  134. $req_uri = '';
  135. }
  136. $request = $req_uri;
  137. }
  138. $request_match = $request;
  139. $permalink_query_vars = array();
  140. foreach ( (array)$rewrite as $match => $query ) {
  141. // If the requesting file is the anchor of the match, prepend it
  142. // to the path info.
  143. if ( ( !empty( $req_uri ) ) && ( strpos( $match, $req_uri ) === 0 ) && ( $req_uri != $request ) ) {
  144. $request_match = $req_uri . '/' . $request;
  145. }
  146. if ( preg_match( "!^$match!", $request_match, $matches ) || preg_match( "!^$match!", urldecode( $request_match ), $matches ) ) {
  147. // Got a match.
  148. // Trim the query of everything up to the '?'.
  149. $query = preg_replace( "!^.+\?!", '', $query );
  150. // Substitute the substring matches into the query.
  151. $query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
  152. // Parse the query.
  153. parse_str( $query, $permalink_query_vars );
  154. break;
  155. }
  156. }
  157. $post_name = $category_name = $tax_name = false;
  158. if ( isset( $permalink_query_vars[ 'pagename' ] ) ) {
  159. $icl_post_lang = !is_null( $filtered_icl_post_language ) ? $filtered_icl_post_language : $current_language;
  160. $sitepress->switch_lang( $icl_post_lang );
  161. $page_by_path = get_page_by_path( $permalink_query_vars[ 'pagename' ] );
  162. $sitepress->switch_lang( $current_language );
  163. if ( !empty( $page_by_path->post_type ) ) {
  164. $post_name = $permalink_query_vars[ 'pagename' ];
  165. $post_type = 'page';
  166. } else {
  167. $post_name = $permalink_query_vars[ 'pagename' ];
  168. $post_type = 'post';
  169. }
  170. } elseif ( isset( $permalink_query_vars[ 'name' ] ) ) {
  171. $post_name = $permalink_query_vars[ 'name' ];
  172. $post_type = 'post';
  173. } elseif ( isset( $permalink_query_vars[ 'category_name' ] ) ) {
  174. $category_name = $permalink_query_vars[ 'category_name' ];
  175. } elseif ( isset( $permalink_query_vars[ 'p' ] ) ) { // case or /archives/%post_id
  176. $post_data_prepared = $wpdb->prepare( "SELECT post_type, post_name FROM {$wpdb->posts} WHERE id=%d", $permalink_query_vars[ 'p' ] );
  177. list( $post_type, $post_name ) = $wpdb->get_row( $post_data_prepared, ARRAY_N );
  178. } else {
  179. if ( empty( $this->custom_post_query_vars ) or empty( $this->taxonomies_query_vars ) ) {
  180. $this->init_query_vars();
  181. }
  182. foreach ( $this->custom_post_query_vars as $query_vars_key => $query_vars_value ) {
  183. if ( isset( $permalink_query_vars[ $query_vars_value ] ) ) {
  184. $post_name = $permalink_query_vars[ $query_vars_value ];
  185. $post_type = $query_vars_key;
  186. break;
  187. }
  188. }
  189. foreach ( $this->taxonomies_query_vars as $query_vars_value ) {
  190. if ( isset( $permalink_query_vars[ $query_vars_value ] ) ) {
  191. $tax_name = $permalink_query_vars[ $query_vars_value ];
  192. $tax_type = $query_vars_value;
  193. break;
  194. }
  195. }
  196. }
  197. if ( $post_name && isset( $post_type ) ) {
  198. $icl_post_lang = !is_null( $filtered_icl_post_language ) ? $filtered_icl_post_language : $current_language;
  199. $sitepress->switch_lang( $icl_post_lang );
  200. $p = get_page_by_path( $post_name, OBJECT, $post_type );
  201. $sitepress->switch_lang( $current_language );
  202. if ( empty( $p ) ) { // fail safe
  203. if ( $post_id = url_to_postid( $home_path . '/' . $post_name ) ) {
  204. $p = get_post( $post_id );
  205. }
  206. }
  207. if ( $p ) {
  208. if ( $post_type == 'page' ) {
  209. $qvid = 'page_id';
  210. } else {
  211. $qvid = 'p';
  212. }
  213. if ( $sitepress_settings[ 'language_negotiation_type' ] == 1 && $lang ) {
  214. $langprefix = '/' . $lang;
  215. } else {
  216. $langprefix = '';
  217. }
  218. $perm_url = '(' . rtrim( $home_url, '/' ) . ')?' . $langprefix . '/' . str_replace( '?', '\?', $m );
  219. $regk = '@href=["\'](' . $perm_url . ')["\']@i';
  220. if ( $anchor ) {
  221. $anchor = "#" . $anchor;
  222. } else {
  223. $anchor = "";
  224. }
  225. // check if this is an offsite url
  226. if ( $p->post_type == 'page' && $offsite_url = get_post_meta( $p->ID, '_cms_nav_offsite_url', true ) ) {
  227. $regv = 'href="' . $offsite_url . $anchor . '"';
  228. } else {
  229. $regv = 'href="' . '/' . ltrim( $url_parts[ 'path' ], '/' ) . '?' . $qvid . '=' . $p->ID;
  230. if ( $req_uri_params != '' ) {
  231. $regv .= '&' . $req_uri_params;
  232. }
  233. $regv .= $anchor . '"';
  234. }
  235. $def_url[ $regk ] = $regv;
  236. } else {
  237. $alp_broken_links[ $alp_matches[ 2 ][ $k ] ] = array();
  238. $name = wpml_like_escape( $post_name );
  239. $p = $this->_get_ids_and_post_types( $name );
  240. if ( $p ) {
  241. foreach ( $p as $post_suggestion ) {
  242. if ( $post_suggestion->post_type == 'page' ) {
  243. $qvid = 'page_id';
  244. } else {
  245. $qvid = 'p';
  246. }
  247. $alp_broken_links[ $alp_matches[ 2 ][ $k ] ][ 'suggestions' ][ ] = array(
  248. 'absolute' => '/' . ltrim( $url_parts[ 'path' ], '/' ) . '?' . $qvid . '=' . $post_suggestion->ID,
  249. 'perma' => '/' . ltrim( str_replace( site_url(), '', get_permalink( $post_suggestion->ID ) ), '/' ),
  250. );
  251. }
  252. }
  253. }
  254. } elseif ( $category_name ) {
  255. if ( false !== strpos( $category_name, '/' ) ) {
  256. $splits = explode( '/', $category_name );
  257. $category_name = array_pop( $splits );
  258. $category_parent = array_pop( $splits );
  259. $category_parent_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $category_parent ) );
  260. $c = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id FROM {$wpdb->terms} t JOIN {$wpdb->term_taxonomy} x ON x.term_id=t.term_id AND x.taxonomy='category' AND x.parent=%d AND t.slug=%s", $category_parent_id, $category_name ) );
  261. } else {
  262. $c = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug=%s", $category_name ) );
  263. }
  264. if ( $c ) {
  265. /* not used ?? */
  266. if ( $sitepress_settings[ 'language_negotiation_type' ] == 1 && $lang ) {
  267. $langprefix = '/' . $lang;
  268. } else {
  269. $langprefix = '';
  270. }
  271. /* not used ?? */
  272. $perm_url = '(' . rtrim( $home_url, '/' ) . ')?' . $langprefix . '/' . $m;
  273. $regk = '@href=[\'"](' . $perm_url . ')[\'"]@i';
  274. $url_parts = parse_url( rtrim( get_home_url(), '/' ) . '/' );
  275. $regv = 'href="' . '/' . ltrim( $url_parts[ 'path' ], '/' ) . '?cat_ID=' . $c->term_id . '"';
  276. $def_url[ $regk ] = $regv;
  277. } elseif ( isset( $name ) ) {
  278. $alp_broken_links[ $alp_matches[ 2 ][ $k ] ] = array();
  279. $c_prepared = $wpdb->prepare( "SELECT term_id FROM {$wpdb->terms} WHERE slug LIKE %s", array( $name . '%' ) );
  280. $c = $wpdb->get_results( $c_prepared );
  281. if ( $c ) {
  282. foreach ( $c as $cat_suggestion ) {
  283. $perma = '/' . ltrim( str_replace( get_home_url(), '', get_category_link( $cat_suggestion->term_id ) ), '/' );
  284. $alp_broken_links[ $alp_matches[ 2 ][ $k ] ][ 'suggestions' ][ ] = array(
  285. 'absolute' => '?cat_ID=' . $cat_suggestion->term_id,
  286. 'perma' => $perma
  287. );
  288. }
  289. }
  290. }
  291. } elseif ( $tax_name && isset( $tax_type ) ) {
  292. if ( $sitepress_settings[ 'language_negotiation_type' ] == 1 && $lang ) {
  293. $langprefix = '/' . $lang;
  294. } else {
  295. $langprefix = '';
  296. }
  297. $perm_url = '(' . rtrim( $home_url, '/' ) . ')?' . $langprefix . '/' . $m;
  298. $regk = '@href=["\'](' . $perm_url . ')["\']@i';
  299. if ( $anchor ) {
  300. $anchor = "#" . $anchor;
  301. } else {
  302. $anchor = "";
  303. }
  304. $regv = 'href="' . '/' . ltrim( $url_parts[ 'path' ], '/' ) . '?' . $tax_type . '=' . $tax_name . $anchor . '"';
  305. $def_url[ $regk ] = $regv;
  306. }
  307. }
  308. if ( !empty( $def_url ) ) {
  309. $text = preg_replace( array_keys( $def_url ), array_values( $def_url ), $text );
  310. }
  311. $tx_qvs = !empty( $this->taxonomies_query_vars ) && is_array( $this->taxonomies_query_vars ) ? '|' . join( '|', $this->taxonomies_query_vars ) : '';
  312. $post_qvs = !empty( $this->custom_posts_query_vars ) && is_array( $this->custom_posts_query_vars ) ? '|' . join( '|', $this->custom_posts_query_vars ) : '';
  313. $int = preg_match_all( '@href=[\'"](' . rtrim( get_home_url(), '/' ) . '/?\?(p|page_id' . $tx_qvs . $post_qvs . ')=([0-9a-z-]+)(#.+)?)[\'"]@i', $text, $matches2 );
  314. if ( $int ) {
  315. $url_parts = parse_url( rtrim( get_home_url(), '/' ) . '/' );
  316. $text = preg_replace( '@href=[\'"](' . rtrim( get_home_url(), '/' ) . '/?\?(p|page_id' . $tx_qvs . $post_qvs . ')=([0-9a-z-]+)(#.+)?)[\'"]@i', 'href="' . '/' . ltrim( $url_parts[ 'path' ], '/' ) . '?$2=$3$4"', $text );
  317. }
  318. }
  319. wp_cache_set( $cache_key, $text, $cache_group );
  320. return $text;
  321. }
  322. private function get_home_url_with_no_lang_directory( ) {
  323. global $sitepress, $sitepress_settings;
  324. $sitepress_settings = $sitepress->get_settings();
  325. $home_url = rtrim( get_home_url(), '/' );
  326. if ( $sitepress_settings[ 'language_negotiation_type' ] == 1 ) {
  327. // Strip lang directory from end if it's there.
  328. $exp = explode( '/', $home_url);
  329. $lang = end( $exp );
  330. if ( $this->does_lang_exist( $lang ) ) {
  331. $home_url = substr( $home_url, 0, strlen($home_url) - strlen( $lang ) );
  332. }
  333. }
  334. return $home_url;
  335. }
  336. private function does_lang_exist ( $lang ) {
  337. global $wpdb;
  338. return $wpdb->get_var( "SELECT code FROM {$wpdb->prefix}icl_languages WHERE code='{$lang}'" );
  339. }
  340. function _get_ids_and_post_types( $name ) {
  341. global $wpdb;
  342. static $cache = array();
  343. $name = rawurlencode( $name );
  344. if ( ! isset( $cache[ $name ] ) ) {
  345. $cache[ $name ] = $wpdb->get_results( $wpdb->prepare ("SELECT ID, post_type FROM {$wpdb->posts} WHERE post_name LIKE %s AND post_type IN('post','page')", $name . '%' ) );
  346. }
  347. return $cache[ $name ];
  348. }
  349. function all_rewrite_rules($rewrite) {
  350. global $sitepress;
  351. if ( !class_exists( 'WPML_Slug_Translation' ) ) {
  352. return $rewrite;
  353. }
  354. $active_languages = $sitepress->get_active_languages();
  355. $current_language = $sitepress->get_current_language();
  356. $default_language = $sitepress->get_default_language();
  357. $cache_keys = array($current_language, $default_language);
  358. $cache_keys[] = md5(serialize($active_languages));
  359. $cache_keys[] = md5(serialize($rewrite));
  360. $cache_key = implode(':', $cache_keys);
  361. $cache_group = 'all_rewrite_rules';
  362. $cache_found = false;
  363. $final_rules = wp_cache_get($cache_key, $cache_group, false, $cache_found);
  364. if($cache_found) return $final_rules;
  365. $final_rules = $rewrite;
  366. foreach ($active_languages as $next_language) {
  367. if ($next_language['code'] == $default_language) {
  368. continue;
  369. }
  370. $sitepress->switch_lang($next_language['code']);
  371. $translated_rules = WPML_Slug_Translation::rewrite_rules_filter($final_rules);
  372. if ( is_array( $translated_rules ) && is_array($final_rules) ) {
  373. $new_rules = array_diff_assoc( $translated_rules, $final_rules );
  374. $final_rules = array_merge( $new_rules, $final_rules );
  375. }
  376. }
  377. $sitepress->switch_lang($current_language);
  378. wp_cache_set($cache_key, $final_rules, $cache_group);
  379. return $final_rules;
  380. }
  381. function process_string( $st_id, $translation = true ) {
  382. global $wpdb;
  383. if ( $st_id ) {
  384. if ( $translation ) {
  385. $string_value = $wpdb->get_var( "SELECT value FROM {$wpdb->prefix}icl_string_translations WHERE id=" . $st_id );
  386. } else {
  387. $string_value = $wpdb->get_var( "SELECT value FROM {$wpdb->prefix}icl_strings WHERE id=" . $st_id );
  388. }
  389. $alp_broken_links = array();
  390. $string_value_up = $this->_process_generic_text( $string_value, $alp_broken_links );
  391. if ( $string_value_up != $string_value ) {
  392. if ( $translation ) {
  393. $wpdb->update( $wpdb->prefix . 'icl_string_translations', array( 'value' => $string_value_up ), array( 'id' => $st_id ) );
  394. } else {
  395. $wpdb->update( $wpdb->prefix . 'icl_strings', array( 'value' => $string_value_up ), array( 'id' => $st_id ) );
  396. }
  397. }
  398. }
  399. }
  400. function process_post( $post_id ) {
  401. global $wpdb, $sitepress;
  402. delete_post_meta( $post_id, '_alp_broken_links' );
  403. $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %s", $post_id ) );
  404. $alp_broken_links = array();
  405. $this_post_language = $sitepress->get_language_for_element($post_id, 'post_' . $post->post_type);
  406. $current_language = $sitepress->get_current_language();
  407. $sitepress->switch_lang($this_post_language);
  408. $post_content = $this->_process_generic_text( $post->post_content, $alp_broken_links );
  409. $sitepress->switch_lang($current_language);
  410. if ( $post_content != $post->post_content ) {
  411. $wpdb->update( $wpdb->posts, array( 'post_content' => $post_content ), array( 'ID' => $post_id ) );
  412. }
  413. update_post_meta( $post_id, '_alp_processed', time() );
  414. if ( !empty( $alp_broken_links ) ) {
  415. update_post_meta( $post_id, '_alp_broken_links', $alp_broken_links );
  416. }
  417. }
  418. }