class-wpml-st-string-tracking-ajax-factory.php
1.83 KB
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
<?php
use WPML\Core\Twig_Loader_Filesystem;
use WPML\Core\Twig_Environment;
class WPML_ST_String_Tracking_AJAX_Factory implements IWPML_AJAX_Action_Loader {
const ACTION_POSITION_IN_SOURCE = 'view_string_in_source';
const ACTION_POSITION_IN_PAGE = 'view_string_in_page';
public function create() {
if ( $this->is_string_position_view() ) {
return new WPML_ST_String_Tracking_AJAX(
$this->get_st_string_positions(),
new WPML_Super_Globals_Validation(),
(string) filter_var( $_GET['action'], FILTER_SANITIZE_STRING )
);
}
return null;
}
private function is_string_position_view() {
return isset( $_GET['action'], $_GET['nonce'] )
&& in_array(
$_GET['action'],
array(
self::ACTION_POSITION_IN_SOURCE,
self::ACTION_POSITION_IN_PAGE,
),
true
)
&& wp_verify_nonce( $_GET['nonce'], $_GET['action'] );
}
/** @return WPML_ST_String_Positions_In_Page|WPML_ST_String_Positions_In_Source */
private function get_st_string_positions() {
global $sitepress, $wpdb;
$string_positions_mapper = new WPML_ST_DB_Mappers_String_Positions( $wpdb );
if ( self::ACTION_POSITION_IN_PAGE === $_GET['action'] ) {
return new WPML_ST_String_Positions_In_Page(
new WPML_ST_String_Factory( $wpdb ),
$string_positions_mapper,
$this->get_template_service()
);
} else {
return new WPML_ST_String_Positions_In_Source(
$sitepress,
$string_positions_mapper,
$this->get_template_service(),
new WPML_WP_API()
);
}
}
private function get_template_service() {
$loader = new Twig_Loader_Filesystem( array( WPML_ST_PATH . WPML_ST_String_Positions::TEMPLATE_PATH ) );
$options = array();
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$options['debug'] = true;
}
$twig_env = new Twig_Environment( $loader, $options );
return new WPML_Twig_Template( $twig_env );
}
}