PluginProbe ʕ •ᴥ•ʔ
Tracking Code Manager / 1.4
Tracking Code Manager v1.4
trunk 1.11.8 1.11.9 1.12.0 1.12.1 1.12.2 1.12.3 1.4 1.5 2.0.0 2.0.1 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0
tracking-code-manager / includes / class-TCM-utils.php
tracking-code-manager / includes Last commit date
admin 11 years ago actions.php 11 years ago class-TCM-check.php 11 years ago class-TCM-cron.php 11 years ago class-TCM-form.php 11 years ago class-TCM-language.php 11 years ago class-TCM-logger.php 11 years ago class-TCM-manager.php 11 years ago class-TCM-options.php 11 years ago class-TCM-tracking.php 11 years ago class-TCM-utils.php 11 years ago core.php 11 years ago install.php 11 years ago uninstall.php 11 years ago
class-TCM-utils.php
182 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit;
3
4 class TCM_Utils {
5
6 function startsWith($haystack, $needle) {
7 $length = strlen($needle);
8 return (substr($haystack, 0, $length) === $needle);
9 }
10
11 function endsWith($haystack, $needle) {
12 $length = strlen($needle);
13 $start = $length * -1; //negative
14 return (substr($haystack, $start) === $needle);
15 }
16 /**
17 * Created by PhpStorm.
18 * User: GIOVANNI
19 * Date: 14/03/2015
20 * Time: 15:47
21 */
22 //verifica se il parametro needle è un elemento dell'array haystack
23 //se il parametro needle è a sua volta un array verifica che almeno un elemento
24 //sia contenuto all'interno dell'array haystack
25 function inArray($needle, $haystack) {
26 if (is_string($haystack)) {
27 //from string to numeric array
28 $temp = explode(',', $haystack);
29 $haystack = array();
30 foreach ($temp as $v) {
31 $v = trim($v);
32 $v = intval($v);
33 if ($v > 0) {
34 $haystack[] = $v;
35 }
36 }
37 }
38
39 $result = FALSE;
40 foreach ($haystack as $v) {
41 $v = intval($v);
42 //if one element of the array have -1 value means i select "all" option
43 if ($v < 0) {
44 $result = TRUE;
45 break;
46 }
47 }
48
49 if ($result) {
50 return TRUE;
51 }
52
53 $result = FALSE;
54 if (is_array($needle)) {
55 foreach ($needle as $v) {
56 $v = trim($v);
57 $v = intval($v);
58 if (in_array($v, $haystack)) {
59 $result = TRUE;
60 break;
61 }
62 }
63 } else {
64 //built-in comparison
65 $result = in_array($needle, $haystack);
66 }
67 return $result;
68 }
69
70
71 //per ottenere un campo dal $_GET oppure dal $_POST
72 function qs($name, $default = '') {
73 $result = $default;
74 if (isset($_GET[$name])) {
75 $result = $_GET[$name];
76 } elseif (isset($_POST[$name])) {
77 $result = $_POST[$name];
78 }
79
80 if (is_string($result)) {
81 $result = urldecode($result);
82 $result = trim($result);
83 }
84
85 return $result;
86 }
87
88 function query($query, $args = NULL) {
89 global $tcm;
90
91 $defaults = array('post_type' => '', 'all' => FALSE);
92 $args = wp_parse_args($args, $defaults);
93
94 $result = $tcm->Options->getCache('Query', $query . '_' . $args['post_type']);
95 if (!is_array($result) || count($result) == 0) {
96 $q = NULL;
97 $id = 'ID';
98 $name = 'post_title';
99 switch ($query) {
100 case TCM_QUERY_POSTS_OF_TYPE:
101 $options = array('posts_per_page' => -1, 'post_type' => $args['post_type']);
102 $q = get_posts($options);
103 break;
104 }
105
106 $result = array();
107 if ($q) {
108 foreach ($q as $v) {
109 $result[] = array('id' => $v->$id, 'name' => $v->$name);
110 }
111 } elseif ($query == TCM_QUERY_POST_TYPES) {
112 $q=array('post', 'page');
113 sort($q);
114 foreach ($q as $v) {
115 $result[] = array('id' => $v, 'name' => $v);
116 }
117 }
118
119 $tcm->Options->setCache('Query', $query . '_' . $args['post_type'], $result);
120 }
121
122 if ($args['all']) {
123 $first = array();
124 $first[] = array('id' => -1, 'name' => '[' . $tcm->Lang->L('All') . ']');
125 $result = array_merge($first, $result);
126 }
127
128 return $result;
129 }
130
131 //send remote request to our server to store tracking and feedback
132 function remotePost($action, $data = '') {
133 global $tcm;
134
135 $data['secret'] = 'WYSIWYG';
136 $response = wp_remote_post(TCM_INTELLYWP_RECEIVER . '?iwpm_action=' . $action, array(
137 'method' => 'POST'
138 , 'timeout' => 20
139 , 'redirection' => 5
140 , 'httpversion' => '1.1'
141 , 'blocking' => TRUE
142 , 'body' => $data
143 , 'user-agent' => 'TCM/' . TCM_PLUGIN_VERSION . '; ' . get_bloginfo('url')
144 ));
145 $data = json_decode(wp_remote_retrieve_body($response), TRUE);
146 if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200
147 || !isset($data['success']) || !$data['success']
148 ) {
149 $tcm->Logger->error('ERRORS SENDING REMOTE-POST ACTION=%s DUE TO REASON=%s', $action, $response);
150 $data = FALSE;
151 } else {
152 $tcm->Logger->debug('SUCCESSFULLY SENT REMOTE-POST ACTION=%s RESPONSE=%s', $action, $data);
153 }
154 return $data;
155 }
156
157 //wp_parse_args with null correction
158 function parseArgs($args, $defaults) {
159 if (is_null($args) || !is_array($args)) {
160 $args = array();
161 }
162 foreach ($args as $k => $v) {
163 if (is_null($args[$k])) {
164 //so can take the default value
165 unset($args[$k]);
166 } elseif (is_string($args[$k]) && $args[$k] == '' && isset($defaults[$k]) && is_array($defaults[$k])) {
167 //a very strange case, i have a blank string for rappresenting an empty array
168 unset($args[$k]);
169 }
170 }
171 $result = wp_parse_args($args, $defaults);
172 return $result;
173 }
174
175 function redirect($location) {
176 //seems that if you have installed xdebug (or some version of it) doesnt work so js added
177 wp_redirect($location);
178 ?>
179 <script> window.location.replace('<?php echo $location?>'); </script>
180 <?php }
181 }
182