class-thelib-array.php
5 years ago
class-thelib-core.php
5 years ago
class-thelib-debug.php
5 years ago
class-thelib-html.php
5 years ago
class-thelib-net.php
5 years ago
class-thelib-session.php
5 years ago
class-thelib-ui.php
5 years ago
class-thelib-updates.php
5 years ago
class-thelib.php
5 years ago
class-thelib-core.php
304 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Main access to the Code-Library. |
| 4 | * Access via function `lib3()`. |
| 5 | * |
| 6 | * Inspired by Jigsaw plugin by Jared Novack (http://jigsaw.upstatement.com/) |
| 7 | * |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | class TheLib_Core extends TheLib { |
| 11 | |
| 12 | /** |
| 13 | * Interface to the array component. |
| 14 | * |
| 15 | * @since 1.1.5 |
| 16 | * @api |
| 17 | * |
| 18 | * @var TheLib_Array |
| 19 | */ |
| 20 | public $array = null; |
| 21 | |
| 22 | /** |
| 23 | * Interface to the Debug component. |
| 24 | * |
| 25 | * @since 1.1.0 |
| 26 | * @api |
| 27 | * |
| 28 | * @var TheLib_Debug |
| 29 | */ |
| 30 | public $debug = null; |
| 31 | |
| 32 | /** |
| 33 | * Interface to the HTML component. |
| 34 | * |
| 35 | * @since 1.1.0 |
| 36 | * @api |
| 37 | * |
| 38 | * @var TheLib_Html |
| 39 | */ |
| 40 | public $html = null; |
| 41 | |
| 42 | /** |
| 43 | * Interface to the Net component. |
| 44 | * |
| 45 | * @since 1.1.0 |
| 46 | * @api |
| 47 | * |
| 48 | * @var TheLib_Net |
| 49 | */ |
| 50 | public $net = null; |
| 51 | |
| 52 | /** |
| 53 | * Interface to the session component. |
| 54 | * |
| 55 | * @since 1.1.5 |
| 56 | * @api |
| 57 | * |
| 58 | * @var TheLib_Session |
| 59 | */ |
| 60 | public $session = null; |
| 61 | |
| 62 | /** |
| 63 | * Interface to the updates component. |
| 64 | * |
| 65 | * @since 1.1.5 |
| 66 | * @api |
| 67 | * |
| 68 | * @var TheLib_Updates |
| 69 | */ |
| 70 | public $updates = null; |
| 71 | |
| 72 | /** |
| 73 | * Interface to the UI component. |
| 74 | * |
| 75 | * @since 1.1.5 |
| 76 | * @api |
| 77 | * |
| 78 | * @var TheLib_Ui |
| 79 | */ |
| 80 | public $ui = null; |
| 81 | |
| 82 | /** |
| 83 | * Class constructor |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | * @internal |
| 87 | */ |
| 88 | public function __construct() { |
| 89 | parent::__construct(); |
| 90 | |
| 91 | self::$core = $this; |
| 92 | |
| 93 | // A List of all components. |
| 94 | $components = array( |
| 95 | 'array', |
| 96 | 'debug', |
| 97 | 'html', |
| 98 | 'net', |
| 99 | 'session', |
| 100 | 'updates', |
| 101 | 'ui', |
| 102 | ); |
| 103 | |
| 104 | // Create instances of each component. |
| 105 | foreach ( $components as $component ) { |
| 106 | if ( ! property_exists( $this, $component ) ) { continue; } |
| 107 | |
| 108 | $class_name = 'TheLib_' . ucfirst( $component ); |
| 109 | $this->$component = new $class_name(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Checks if the provided value evaluates to a boolean TRUE. |
| 115 | * |
| 116 | * Following values are considered true: |
| 117 | * - Boolean: true |
| 118 | * - Number: anything except 0 |
| 119 | * - Strings: true, yes, on (case insensitive) |
| 120 | * |
| 121 | * @since 1.1.0 |
| 122 | * @api |
| 123 | * |
| 124 | * @param mixed $value A value that will be evaluated as a boolean. |
| 125 | * @return bool True if the specified $value evaluated to TRUE. |
| 126 | */ |
| 127 | public function is_true( $value ) { |
| 128 | if ( false === $value || null === $value || '' === $value ) { |
| 129 | return false; |
| 130 | } elseif ( true === $value ) { |
| 131 | return true; |
| 132 | } elseif ( is_numeric( $value ) ) { |
| 133 | $value = intval( $value ); |
| 134 | return $value != 0; |
| 135 | } elseif ( is_string( $value ) ) { |
| 136 | $value = strtolower( trim( $value ) ); |
| 137 | return in_array( |
| 138 | $value, |
| 139 | array( 'true', 'yes', 'on', '1' ) |
| 140 | ); |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Opposite of the is_true() function. |
| 147 | * |
| 148 | * @since 3.0.0 |
| 149 | * @param mixed $value A value that will be evaluated as a boolean |
| 150 | * @return bool True if the speciefied value evals as FALSE |
| 151 | */ |
| 152 | public function is_false( $value ) { |
| 153 | return ! $this->is_true( $value ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Converts a number from any base to another base. |
| 158 | * The from/to base values can even be non-numeric values. |
| 159 | * |
| 160 | * @since 2.0.2 |
| 161 | * @api |
| 162 | * |
| 163 | * @param string $number A number in the base_from base. |
| 164 | * @param string $base_from List of characters |
| 165 | * E.g. 0123456789 to convert from decimal. |
| 166 | * @param string $base_to List of characters to use as destination base. |
| 167 | * E.g. 0123456789ABCDEF to convert to hexadecimal. |
| 168 | * @return string The converted number |
| 169 | */ |
| 170 | public function convert( $number, $base_from = '0123456789', $base_to = '0123456789ABCDEF' ) { |
| 171 | if ( $base_from == $base_to ) { |
| 172 | // No conversion needed. |
| 173 | return $number; |
| 174 | } |
| 175 | |
| 176 | $retval = ''; |
| 177 | $number_len = strlen( $number ); |
| 178 | |
| 179 | if ( '0123456789' == $base_to ) { |
| 180 | // Convert a value to normal decimal base. |
| 181 | |
| 182 | $arr_base_from = str_split( $base_from, 1 ); |
| 183 | $arr_number = str_split( $number, 1 ); |
| 184 | $base_from_len = strlen( $base_from ); |
| 185 | $retval = 0; |
| 186 | for ( $i = 1; $i <= $number_len; $i += 1 ) { |
| 187 | $retval = bcadd( |
| 188 | $retval, |
| 189 | bcmul( |
| 190 | array_search( $arr_number[$i - 1], $arr_base_from ), |
| 191 | bcpow( $base_from_len, $number_len - $i ) |
| 192 | ) |
| 193 | ); |
| 194 | } |
| 195 | } else { |
| 196 | // Convert a value to a NON-decimal base. |
| 197 | |
| 198 | if ( '0123456789' != $base_from ) { |
| 199 | // Base value is non-decimal, convert it to decimal first. |
| 200 | $base10 = $this->convert( $number, $base_from, '0123456789' ); |
| 201 | } else { |
| 202 | // Base value is decimal. |
| 203 | $base10 = $number; |
| 204 | } |
| 205 | |
| 206 | $arr_base_to = str_split( $base_to, 1 ); |
| 207 | $base_to_len = strlen( $base_to ); |
| 208 | if ( $base10 < strlen( $base_to ) ) { |
| 209 | $retval = $arr_base_to[$base10]; |
| 210 | } else { |
| 211 | while ( 0 != $base10 ) { |
| 212 | $retval = $arr_base_to[bcmod( $base10, $base_to_len )] . $retval; |
| 213 | $base10 = bcdiv( $base10, $base_to_len, 0 ); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return $retval; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Return URL link for wp.org, wpmudev, support, live chat, docs, installing plugin. |
| 224 | * |
| 225 | * @param string $plugin_name . |
| 226 | * @param string $link_for Accepts: 'chat', 'plugin', 'support', 'smush', 'docs', 'install_plugin'. |
| 227 | * @param string $campaign Utm campaign tag to be used in link. |
| 228 | * |
| 229 | * @return string |
| 230 | */ |
| 231 | public function get_link( $plugin_name, $link_for, $campaign ) { |
| 232 | $domain = 'https://premium.wpmudev.org'; |
| 233 | $wp_org = 'https://wordpress.org'; |
| 234 | $utm_tags = "?utm_source={$plugin_name}&utm_medium=plugin&utm_campaign={$campaign}"; |
| 235 | |
| 236 | $data = array( |
| 237 | 'hummingbird' => array( |
| 238 | 'wporg' => 'hummingbird-performance', |
| 239 | 'wpmudev' => 'wp-hummingbird', |
| 240 | 'pid' => '1081721', |
| 241 | ), |
| 242 | 'smush' => array( |
| 243 | 'wporg' => 'wp-smushit', |
| 244 | 'wpmudev' => 'wp-smush-pro', |
| 245 | 'pid' => '912164', |
| 246 | ), |
| 247 | 'hustle' => array( |
| 248 | 'wporg' => 'wordpress-popup', |
| 249 | 'wpmudev' => 'hustle', |
| 250 | 'pid' => '1107020', |
| 251 | ), |
| 252 | ); |
| 253 | |
| 254 | switch ( $link_for ) { |
| 255 | case 'chat': |
| 256 | $link = "{$domain}/live-support/{$utm_tags}"; |
| 257 | break; |
| 258 | case 'plugin': |
| 259 | $link = "{$domain}/project/{$data[ $plugin_name ]['wpmudev']}/{$utm_tags}"; |
| 260 | break; |
| 261 | case 'support': |
| 262 | if ( $this->is_member() ) { |
| 263 | $link = "{$domain}/forum/support#question{$utm_tags}"; |
| 264 | } else { |
| 265 | $link = "{$wp_org}/support/plugin/{$data[ $plugin_name ]['wporg']}"; |
| 266 | } |
| 267 | break; |
| 268 | case 'docs': |
| 269 | $link = "{$domain}/docs/wpmu-dev-plugins/{$plugin_name}/{$utm_tags}"; |
| 270 | break; |
| 271 | case 'install_plugin': |
| 272 | if ( $this->is_member() ) { |
| 273 | // Return the pro plugin URL. |
| 274 | $url = WPMUDEV_Dashboard::$ui->page_urls->plugins_url; |
| 275 | $link = $url . '#pid=' . $data[ $plugin_name ]['pid']; |
| 276 | } else { |
| 277 | // Return the free URL. |
| 278 | $link = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $data[ $plugin_name ]['wporg'] ), 'install-plugin_' . $data[ $plugin_name ]['wporg'] ); |
| 279 | } |
| 280 | break; |
| 281 | default: |
| 282 | $link = ''; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | return $link; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /** |
| 291 | * Check if user is a paid one in WPMU DEV |
| 292 | * |
| 293 | * @return bool |
| 294 | */ |
| 295 | public function is_member() { |
| 296 | if ( function_exists( 'is_wpmudev_member' ) ) { |
| 297 | return is_wpmudev_member(); |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | } |
| 304 |