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
210 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 | function is($name, $compare, $default='', $ignoreCase=TRUE) { |
| 71 | $what=$this->qs($name, $default); |
| 72 | $result=FALSE; |
| 73 | if(is_string($compare)) { |
| 74 | $compare=explode(',', $compare); |
| 75 | } |
| 76 | if($ignoreCase){ |
| 77 | $what=strtolower($what); |
| 78 | } |
| 79 | |
| 80 | foreach($compare as $v) { |
| 81 | if($ignoreCase){ |
| 82 | $v=strtolower($v); |
| 83 | } |
| 84 | if($what==$v) { |
| 85 | $result=TRUE; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | return $result; |
| 90 | } |
| 91 | |
| 92 | public function twitter($name) { |
| 93 | ?> |
| 94 | <a href="https://twitter.com/<?php echo $name?>" class="twitter-follow-button" data-show-count="false" data-dnt="true">Follow @<?php echo $name?></a> |
| 95 | <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| 96 | <?php |
| 97 | } |
| 98 | |
| 99 | //per ottenere un campo dal $_GET oppure dal $_POST |
| 100 | function qs($name, $default = '') { |
| 101 | $result = $default; |
| 102 | if (isset($_GET[$name])) { |
| 103 | $result = $_GET[$name]; |
| 104 | } elseif (isset($_POST[$name])) { |
| 105 | $result = $_POST[$name]; |
| 106 | } |
| 107 | |
| 108 | if (is_string($result)) { |
| 109 | $result = urldecode($result); |
| 110 | $result = trim($result); |
| 111 | } |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | |
| 116 | function query($query, $args = NULL) { |
| 117 | global $tcm; |
| 118 | |
| 119 | $defaults = array('post_type' => '', 'all' => FALSE); |
| 120 | $args = wp_parse_args($args, $defaults); |
| 121 | |
| 122 | $result = $tcm->Options->getCache('Query', $query . '_' . $args['post_type']); |
| 123 | if (!is_array($result) || count($result) == 0) { |
| 124 | $q = NULL; |
| 125 | $id = 'ID'; |
| 126 | $name = 'post_title'; |
| 127 | switch ($query) { |
| 128 | case TCM_QUERY_POSTS_OF_TYPE: |
| 129 | $options = array('posts_per_page' => -1, 'post_type' => $args['post_type']); |
| 130 | $q = get_posts($options); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | $result = array(); |
| 135 | if ($q) { |
| 136 | foreach ($q as $v) { |
| 137 | $result[] = array('id' => $v->$id, 'name' => $v->$name); |
| 138 | } |
| 139 | } elseif ($query == TCM_QUERY_POST_TYPES) { |
| 140 | $q=array('post', 'page'); |
| 141 | sort($q); |
| 142 | foreach ($q as $v) { |
| 143 | $result[] = array('id' => $v, 'name' => $v); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | $tcm->Options->setCache('Query', $query . '_' . $args['post_type'], $result); |
| 148 | } |
| 149 | |
| 150 | if ($args['all']) { |
| 151 | $first = array(); |
| 152 | $first[] = array('id' => -1, 'name' => '[' . $tcm->Lang->L('All') . ']'); |
| 153 | $result = array_merge($first, $result); |
| 154 | } |
| 155 | |
| 156 | return $result; |
| 157 | } |
| 158 | |
| 159 | //send remote request to our server to store tracking and feedback |
| 160 | function remotePost($action, $data = '') { |
| 161 | global $tcm; |
| 162 | |
| 163 | $data['secret'] = 'WYSIWYG'; |
| 164 | $response = wp_remote_post(TCM_INTELLYWP_RECEIVER . '?iwpm_action=' . $action, array( |
| 165 | 'method' => 'POST' |
| 166 | , 'timeout' => 20 |
| 167 | , 'redirection' => 5 |
| 168 | , 'httpversion' => '1.1' |
| 169 | , 'blocking' => TRUE |
| 170 | , 'body' => $data |
| 171 | , 'user-agent' => 'TCM/' . TCM_PLUGIN_VERSION . '; ' . get_bloginfo('url') |
| 172 | )); |
| 173 | $data = json_decode(wp_remote_retrieve_body($response), TRUE); |
| 174 | if (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200 |
| 175 | || !isset($data['success']) || !$data['success'] |
| 176 | ) { |
| 177 | $tcm->Logger->error('ERRORS SENDING REMOTE-POST ACTION=%s DUE TO REASON=%s', $action, $response); |
| 178 | $data = FALSE; |
| 179 | } else { |
| 180 | $tcm->Logger->debug('SUCCESSFULLY SENT REMOTE-POST ACTION=%s RESPONSE=%s', $action, $data); |
| 181 | } |
| 182 | return $data; |
| 183 | } |
| 184 | |
| 185 | //wp_parse_args with null correction |
| 186 | function parseArgs($args, $defaults) { |
| 187 | if (is_null($args) || !is_array($args)) { |
| 188 | $args = array(); |
| 189 | } |
| 190 | foreach ($args as $k => $v) { |
| 191 | if (is_null($args[$k])) { |
| 192 | //so can take the default value |
| 193 | unset($args[$k]); |
| 194 | } elseif (is_string($args[$k]) && $args[$k] == '' && isset($defaults[$k]) && is_array($defaults[$k])) { |
| 195 | //a very strange case, i have a blank string for rappresenting an empty array |
| 196 | unset($args[$k]); |
| 197 | } |
| 198 | } |
| 199 | $result = wp_parse_args($args, $defaults); |
| 200 | return $result; |
| 201 | } |
| 202 | |
| 203 | function redirect($location) { |
| 204 | //seems that if you have installed xdebug (or some version of it) doesnt work so js added |
| 205 | wp_redirect($location); |
| 206 | ?> |
| 207 | <script> window.location.replace('<?php echo $location?>'); </script> |
| 208 | <?php } |
| 209 | } |
| 210 |