-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwpml.php
145 lines (127 loc) · 4.09 KB
/
wpml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Manage integration with WPML.
* @package GeoMashup
* @since 1.9.0
*/
class GeoMashupWPML {
/** @var array */
protected static $meta_queue = array();
/**
* Load WPML integrations.
* @since 1.9.0
*/
public static function load() {
add_filter( 'geo_mashup_get_language_code', array( __CLASS__, 'get_language_code' ) );
add_filter( 'geo_mashup_locations_join', array( __CLASS__, 'augment_locations_join_clause' ), 10, 2 );
add_filter( 'geo_mashup_locations_where', array( __CLASS__, 'augment_locations_where_clause' ), 10, 2 );
add_filter( 'geo_mashup_results_page_id', array( __CLASS__, 'translate_results_page_id' ) );
add_filter( 'wpml_duplicate_generic_string', array( __CLASS__, 'queue_post_meta' ), 10, 3 );
}
/**
* Use WPML's language code unless the lang querystring parameter is present.
* @since 1.9.0
* @param string $code
* @return string
*/
public static function get_language_code( $code ) {
return isset( $_GET['lang'] ) ? $_GET['lang'] : ICL_LANGUAGE_CODE;
}
/**
* Add WPML tables to post location query join clause, changing posts table references to our alias.
* @since 1.9.0
* @param string $join
* @param array $query_args
* @return string
*/
public static function augment_locations_join_clause( $join, $query_args ) {
global $wpdb, $wpml_query_filter;
if ( self::suppress_query_filters() ) {
return $join;
}
if ( 'post' != $query_args['object_name'] ) {
return $join;
}
// Apply post query filters,
$join = $wpml_query_filter->filter_single_type_join( $join, 'any' );
return str_replace( $wpdb->posts . '.', 'o.', $join );
}
/**
* Add WPML conditions to post location query where clause, changing posts table references to our alias.
*
* Also removes an interfering WPML filter.
* @since 1.9.0
* @param string $where
* @param array $query_args
* @return string
*/
public static function augment_locations_where_clause( $where, $query_args ) {
global $wpdb, $wpml_query_filter;
if ( self::suppress_query_filters() ) {
return $where;
}
if ( 'post' != $query_args['object_name'] ) {
return $where;
}
$where = $wpml_query_filter->filter_single_type_where(
$where,
$GLOBALS['geo_mashup_options']->get( 'overall', 'located_post_types' )
);
$where = str_replace( $wpdb->posts . '.', 'o.', $where );
remove_filter( 'get_translatable_documents', array( __CLASS__, 'wpml_filter_get_translatable_documents' ) );
return $where;
}
/**
* @since 1.10.0
* @param int $page_id
* @return int
*/
public static function translate_results_page_id( $page_id ) {
return apply_filters( 'wpml_object_id', $page_id, 'page' );
}
/**
* Collect geodata when WPML adds metadata without firing WP hooks.
*
* @param mixed $meta_value
* @param string $language
* @param array $context
*
* @return mixed
*/
public static function queue_post_meta( $meta_value, $language, $context )
{
self::$meta_queue[] = array(
'post_id' => $context['post_id'],
'key' => $context['key'],
'value' => $meta_value,
);
// Our added post meta action won't work until the post cache is cleared
// and it seems safer to do that on this action, near where WPML does it
add_action('icl_make_duplicate', array(__CLASS__, 'add_queued_meta', 10, 4));
return $meta_value;
}
/**
* Clean the post cache and run our post meta action.
*
* @param int $master_post_id
* @param string $lang
* @param array $post_array
* @param int $id
*
* @return mixed
*/
public static function add_queued_meta( $master_post_id, $lang, $post_array, $id ) {
clean_post_cache( $id );
foreach (self::$meta_queue as $meta) {
GeoMashupDB::action_added_post_meta( null, $meta['post_id'], $meta['key'], $meta['value'] );
}
}
/**
* Whether WPML post location query filters have been suppressed.
* @since 1.9.0
* @return bool
*/
protected static function suppress_query_filters() {
return defined( 'GEO_MASHUP_SUPPRESS_POST_FILTERS' ) && GEO_MASHUP_SUPPRESS_POST_FILTERS;
}
}