| 1 |
<?php |
| 2 |
/** |
| 3 |
+=====================================================================+ |
| 4 |
| ____ _ ____ __ _ _ | |
| 5 |
| / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 |
| | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 |
| | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 |
| \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 |
| | |
| 10 |
| (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | |
| 11 |
+=====================================================================+ |
| 12 |
*/ |
| 13 |
|
| 14 |
if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } |
| 15 |
|
| 16 |
// ===================================================================== |
| 17 |
|
| 18 |
class CodeProfiler_WPCron { |
| 19 |
|
| 20 |
|
| 21 |
private static $options = []; |
| 22 |
private static $url = 'https://api.nintechnet.com/coupons'; |
| 23 |
private static $file = 'coupon.png'; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Hook (main site only). |
| 28 |
*/ |
| 29 |
public static function init() { |
| 30 |
/** |
| 31 |
* We run on the main site only. |
| 32 |
*/ |
| 33 |
if (! is_main_site() ) { |
| 34 |
return ['error' => 'child site']; |
| 35 |
} |
| 36 |
add_action('codeprofiler_wpcron', [ __CLASS__, 'run'] ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Install scheduled tasks (daily). |
| 42 |
*/ |
| 43 |
public static function install() { |
| 44 |
|
| 45 |
if (! is_main_site() ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if ( wp_next_scheduled('codeprofiler_wpcron') ) { |
| 50 |
wp_clear_scheduled_hook('codeprofiler_wpcron'); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Start in 30mn. |
| 54 |
*/ |
| 55 |
wp_schedule_event( time() + 1800, 'daily', 'codeprofiler_wpcron'); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Unnstall scheduled tasks (daily). |
| 61 |
*/ |
| 62 |
public static function uninstall() { |
| 63 |
|
| 64 |
if ( wp_next_scheduled('codeprofiler_wpcron') ) { |
| 65 |
wp_clear_scheduled_hook('codeprofiler_wpcron'); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Run scheduled tasks. |
| 72 |
*/ |
| 73 |
public static function run() { |
| 74 |
|
| 75 |
self::$options = self::get_option('code-profiler'); |
| 76 |
|
| 77 |
self::get_coupon(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Display any available coupon. |
| 83 |
*/ |
| 84 |
public static function display_coupon() { |
| 85 |
/** |
| 86 |
* Child site must retrieve the options from the main site. |
| 87 |
*/ |
| 88 |
self::$options = self::get_option('code-profiler'); |
| 89 |
|
| 90 |
if ( empty( self::$options['coupon']['date'] ) ) { |
| 91 |
return ['error' => 'no coupon']; |
| 92 |
} |
| 93 |
/** |
| 94 |
* Make sure it didn't expire yet. |
| 95 |
*/ |
| 96 |
$today = date('Y-m-d'); |
| 97 |
if ( $today > self::$options['coupon']['date'] ) { |
| 98 |
return ['error' => 'expired coupon']; |
| 99 |
} |
| 100 |
|
| 101 |
if (! is_file( CODE_PROFILER_UPLOAD_DIR .'/'. self::$file ) ) { |
| 102 |
return ['error' => 'missing file']; |
| 103 |
} |
| 104 |
$data = file_get_contents( CODE_PROFILER_UPLOAD_DIR .'/'. self::$file ); |
| 105 |
|
| 106 |
$until = 'This offer is valid until '. |
| 107 |
date('F d', strtotime( self::$options['coupon']['date'] ) ); |
| 108 |
|
| 109 |
if (! empty( self::$options['coupon']['url'] ) ) { |
| 110 |
$url = self::$options['coupon']['url']; |
| 111 |
} else { |
| 112 |
$url = 'https://nintechnet.com/'; |
| 113 |
} |
| 114 |
|
| 115 |
echo '<p><a href="'. esc_url( $url ) .'" alt="Go Pro! Limited time offer" '. |
| 116 |
'title="Go Pro! Limited time offer" target="_blank" rel="noreferrer noopener">'. |
| 117 |
'<img style="max-width:250px" src="data:image/png;base64, '. esc_attr( $data ) .'" />'. |
| 118 |
'<br />'. esc_html( $until ) .'</a></p>'; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Remote connection. |
| 124 |
*/ |
| 125 |
private static function get_coupon() { |
| 126 |
/** |
| 127 |
* It should not run more than once daily (86400s). |
| 128 |
*/ |
| 129 |
if (! empty( self::$options['cronjobs']['coupon']['last'] ) && |
| 130 |
self::$options['cronjobs']['coupon']['last'] + 86400 > time() ) { |
| 131 |
|
| 132 |
return ['error' => 'frequency']; |
| 133 |
} |
| 134 |
/** |
| 135 |
* Update last checked time. |
| 136 |
*/ |
| 137 |
self::$options['cronjobs']['coupon']['last'] = time(); |
| 138 |
self::update_option('code-profiler', self::$options ); |
| 139 |
|
| 140 |
/** |
| 141 |
* Connect. |
| 142 |
*/ |
| 143 |
global $wp_version; |
| 144 |
$res = wp_remote_get( |
| 145 |
self::$url, |
| 146 |
[ |
| 147 |
'timeout' => 5, |
| 148 |
'httpversion' => '1.1' , |
| 149 |
'user-agent' => 'Mozilla/5.0 (compatible; Code-Profiler/'. |
| 150 |
CODE_PROFILER_VERSION ."; WordPress/$wp_version)", |
| 151 |
'sslverify' => true, |
| 152 |
'headers' => [ |
| 153 |
'ntn-plugin' => 'cp', |
| 154 |
'ntn-cache' => md5( network_site_url() ) |
| 155 |
] |
| 156 |
] |
| 157 |
); |
| 158 |
if (! is_wp_error( $res ) && $res['response']['code'] == 200 ) { |
| 159 |
$coupon = json_decode( $res['body'], true ); |
| 160 |
|
| 161 |
if ( empty( $coupon['cp']['img'] ) ) { |
| 162 |
/** |
| 163 |
* Clear the old coupon. |
| 164 |
*/ |
| 165 |
if (! empty( self::$options['coupon'] ) ) { |
| 166 |
unset( self::$options['coupon'] ); |
| 167 |
self::update_option('code-profiler', self::$options ); |
| 168 |
} |
| 169 |
return ['error' => 'no coupon']; |
| 170 |
} |
| 171 |
/** |
| 172 |
* Save the image. |
| 173 |
*/ |
| 174 |
@ file_put_contents( CODE_PROFILER_UPLOAD_DIR .'/'. self::$file, $coupon['cp']['img'] ); |
| 175 |
$coupon['cp']['img'] = self::$file; |
| 176 |
|
| 177 |
if ( empty( self::$options['coupon'] ) || self::$options['coupon'] != $coupon['cp'] ) { |
| 178 |
/** |
| 179 |
* Save/update the coupon. |
| 180 |
*/ |
| 181 |
self::$options['coupon'] = $coupon['cp']; |
| 182 |
self::update_option('code-profiler', self::$options ); |
| 183 |
} |
| 184 |
return $coupon['cp']; |
| 185 |
} |
| 186 |
return ['error' => 'HTTP error']; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Update options. |
| 193 |
*/ |
| 194 |
private static function update_option( $option, $new_value ) { |
| 195 |
|
| 196 |
if ( is_multisite() ) { |
| 197 |
$res = update_site_option( $option, $new_value ); |
| 198 |
} else { |
| 199 |
$res = update_option( $option, $new_value ); |
| 200 |
} |
| 201 |
if ( $res == false ) { |
| 202 |
return []; |
| 203 |
} |
| 204 |
return $res; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/** |
| 209 |
* Get options. |
| 210 |
*/ |
| 211 |
private static function get_option( $option ) { |
| 212 |
|
| 213 |
if ( is_multisite() ) { |
| 214 |
$res = get_site_option( $option ); |
| 215 |
} else { |
| 216 |
$res = get_option( $option ); |
| 217 |
} |
| 218 |
if ( $res == false ) { |
| 219 |
return []; |
| 220 |
} |
| 221 |
return $res; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
CodeProfiler_WPCron::init(); |
| 226 |
|
| 227 |
// ===================================================================== |
| 228 |
// EOF |
| 229 |
|