· 6 years ago · Dec 04, 2019, 01:58 PM
1<?php
2function init_wprthumb() {
3 new wprthumb();
4}
5
6if ( is_admin() ) {
7 add_action( 'load-post.php', 'init_wprthumb' );
8 add_action( 'load-post-new.php', 'init_wprthumb' );
9}
10
11
12class wprthumb {
13
14 /**
15 * Hook into the appropriate actions when the wprthumb is constructed.
16 */
17 public function __construct() {
18 add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
19 add_action( 'save_post', array( $this, 'save' ) );
20 }
21
22 /**
23 * Adds the meta box container.
24 */
25 public function add_meta_box( $post_type ) {
26 if ( post_type_supports( $post_type, 'thumbnail' ) ) {
27 add_meta_box(
28 'some_meta_box_name'
29 , 'Remote Post Thumbnail'
30 , array( $this, 'render_meta_box_content' )
31 , $post_type
32 , 'side'
33 , 'default'
34 );
35 }
36 }
37
38 /**
39 * Save the meta when the post is saved.
40 */
41 public function save( $post_id ) {
42
43 /*
44 * We need to verify this came from the our screen and with proper authorization,
45 * because save_post can be triggered at other times.
46 */
47
48 // Check if our nonce is set.
49 if ( ! isset( $_POST['wprthumb_nonce'] ) )
50 return $post_id;
51
52 $nonce = $_POST['wprthumb_nonce'];
53
54 // Verify that the nonce is valid.
55 if ( ! wp_verify_nonce( $nonce, 'wprthumb' ) )
56 return $post_id;
57
58 // If this is an autosave, our form has not been submitted,
59 // so we don't want to do anything.
60 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
61 return $post_id;
62
63 // Check the user's permissions.
64 if ( 'page' == $_POST['post_type'] ) {
65
66 if ( ! current_user_can( 'edit_page', $post_id ) )
67 return $post_id;
68
69 } else {
70
71 if ( ! current_user_can( 'edit_post', $post_id ) )
72 return $post_id;
73 }
74
75 /* All good, its safe for us to save the data now. */
76
77 // Sanitize the user input.
78 $image = sanitize_text_field( $_POST['remote_thumb'] );
79
80 $upload_dir = wp_upload_dir();
81 //Get the remote image and save to uploads directory
82 $img_name = time().'.jpg'.basename( $image, array( 'timeout' => 150 ) );
83 $img = wp_remote_get( $image );
84
85 if ( is_wp_error( $img ) ) {
86 $error_message = $img->get_error_message();
87 add_action( 'admin_notices', array( $this, 'wprthumb_admin_notice' ) );
88 }
89 else {
90 $img = wp_remote_retrieve_body( $img );
91 $fp = fopen( $upload_dir['path'].'/'.$img_name , 'w' );
92 fwrite( $fp, $img );
93 fclose( $fp );
94
95 $wp_filetype = wp_check_filetype( $img_name , null );
96 $attachment = array(
97 'post_mime_type' => $wp_filetype['type'],
98 'post_title' => preg_replace( '/\.[^.]+$/', '', $img_name ),
99 'post_content' => '',
100 'post_status' => 'inherit'
101 );
102
103 //require for wp_generate_attachment_metadata which generates image related meta-data also creates thumbs
104 require_once ABSPATH . 'wp-admin/includes/image.php';
105 $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'].'/'.$img_name, $post_id );
106 //Generate post thumbnail of different sizes.
107 $attach_data = wp_generate_attachment_metadata( $attach_id , $upload_dir['path'].'/'.$img_name );
108 wp_update_attachment_metadata( $attach_id, $attach_data );
109 //Set as featured image.
110 delete_post_meta( $post_id, '_thumbnail_id' );
111 add_post_meta( $post_id , '_thumbnail_id' , $attach_id, true );
112 }
113 }
114
115 /**
116 * Render Meta Box content.
117 */
118 public function render_meta_box_content( $post ) {
119
120 // Add an nonce field so we can check for it later.
121 wp_nonce_field( 'wprthumb', 'wprthumb_nonce' );
122
123 // Display the form, using the current value.
124 echo '<label for="remote_thumb">';
125 _e( 'Enter remote image url', 'wprthumb' );
126 echo '</label> ';
127 echo '<input type="text" id="remote_thumb" name="remote_thumb" size="25" />';
128 }
129
130 /**
131 * Admin notice for errors.
132 */
133 function wprthumb_admin_notice() { ?>
134 <div class="error">
135 <p><?php _e( 'Error while fetching remote thumbnail! Please try again.', 'wprthumb' ); ?></p>
136 </div>
137 <?php
138 }
139}
140
141// Loads the updater classes
142$updater = new EDD_Theme_Updater_Admin(
143
144 // Config settings
145 $config = array(
146 'remote_api_url' => 'https://nakal.my.id/', // Site where EDD is hosted
147 'item_name' => 'Kazefuri', // Name of theme
148 'theme_slug' => 'kazefuri', // Theme slug
149 'version' => '1.0', // The current version of this theme
150 'author' => 'Z-Studio', // The author of this theme
151 'download_id' => '', // Optional, used for generating a license renewal link
152 'renew_url' => '', // Optional, allows for a custom license renewal link
153 'beta' => false, // Optional, set to true to opt into beta versions
154 ),
155
156 // Strings
157 $strings = array(
158 'theme-license' => __( 'Theme License', 'edd-theme-updater' ),
159 'enter-key' => __( 'Enter your theme license key.', 'edd-theme-updater' ),
160 'license-key' => __( 'License Key', 'edd-theme-updater' ),
161 'license-action' => __( 'License Action', 'edd-theme-updater' ),
162 'deactivate-license' => __( 'Deactivate License', 'edd-theme-updater' ),
163 'activate-license' => __( 'Activate License', 'edd-theme-updater' ),
164 'status-unknown' => __( 'License status is unknown.', 'edd-theme-updater' ),
165 'renew' => __( 'Renew?', 'edd-theme-updater' ),
166 'unlimited' => __( 'unlimited', 'edd-theme-updater' ),
167 'license-key-is-active' => __( 'License key is active.', 'edd-theme-updater' ),
168 'expires%s' => __( 'Expires %s.', 'edd-theme-updater' ),
169 'expires-never' => __( 'Lifetime License.', 'edd-theme-updater' ),
170 '%1$s/%2$-sites' => __( 'You have %1$s / %2$s sites activated.', 'edd-theme-updater' ),
171 'license-key-expired-%s' => __( 'License key expired %s.', 'edd-theme-updater' ),
172 'license-key-expired' => __( 'License key has expired.', 'edd-theme-updater' ),
173 'license-keys-do-not-match' => __( 'License keys do not match.', 'edd-theme-updater' ),
174 'license-is-inactive' => __( 'License is inactive.', 'edd-theme-updater' ),
175 'license-key-is-disabled' => __( 'License key is disabled.', 'edd-theme-updater' ),
176 'site-is-inactive' => __( 'Site is inactive.', 'edd-theme-updater' ),
177 'license-status-unknown' => __( 'License status is unknown.', 'edd-theme-updater' ),
178 'update-notice' => __( "Updating this theme will lose any customizations you have made. 'Cancel' to stop, 'OK' to update.", 'edd-theme-updater' ),
179 'update-available' => __('<strong>%1$s %2$s</strong> is available. <a href="%3$s" class="thickbox" title="%4s">Check out what\'s new</a> or <a href="%5$s"%6$s>update now</a>.', 'edd-theme-updater' ),
180 )
181
182);
183
184$l = trim( get_option( $updater->theme_slug . '_license_key' ) );
185$response = $updater->get_api_response( array(
186 'edd_action' => 'check_license',
187 'license' => $l,
188 'item_name' => urlencode( $updater->item_name ),
189 'url' => home_url()
190) );
191
192if ( !is_wp_error( $response ) || 200 == wp_remote_retrieve_response_code( $response ) ) {
193 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
194 if ( $license_data->license == 'valid' ) {
195 require_once(get_template_directory() . '/inc/meta.php');
196 require_once(get_template_directory() . '/inc/panel.php');
197 require_once(get_template_directory() . '/inc/widget.php');
198 }
199}
200
201if (!function_exists('the_thumbnail')) {
202 function the_thumbnail($ID) {
203 if ( has_post_thumbnail($ID) ) {
204 $thumb = get_the_post_thumbnail($ID, 'thumb', array( 'itemprop' => 'image' ) );
205 } else {
206 $thumb = '<img src="'.get_post_meta( $ID, 'dev_featured', true).'" title="'. get_the_title($ID).'" itemprop="image" />';
207 }
208 return $thumb;
209 }
210
211}
212
213# Echo translated text
214function _d($text){
215 echo translate($text,'dvtr');
216}
217
218
219# Return Translated Text
220function __d($text) {
221 return translate($text,'dvtr');
222}
223
224function parallelize_hostnames($url, $id)
225 {
226 $hostname = par_get_hostname($url);
227 $url = str_replace(parse_url(get_bloginfo("url"), PHP_URL_HOST), $hostname, $url);
228 return $url;
229 }
230 function par_get_hostname($name)
231 {
232 list(, $domain) = explode("://", site_url());
233 $domain = trim(trim($domain), "/");
234 $hosts = array();
235 $hosts[] = "i0.wp.com/" . $domain;
236 $hosts[] = "i1.wp.com/" . $domain;
237 $hosts[] = "i2.wp.com/" . $domain;
238 $hosts[] = "i3.wp.com/" . $domain;
239 $subdomains = $hosts;
240 $host = abs(crc32(basename($name)) % count($subdomains));
241 $hostname = $subdomains[$host];
242 return $hostname;
243 }
244 if (get_option("photonx") == "1") {
245 add_filter("wp_get_attachment_url", "parallelize_hostnames", 10, 2);
246 }
247
248class EDD_Theme_Updater_Admin {
249
250 /**
251 * Variables required for the theme updater
252 *
253 * @since 1.0.0
254 * @type string
255 */
256 protected $remote_api_url = null;
257 public $theme_slug = null;
258 protected $version = null;
259 protected $author = null;
260 protected $download_id = null;
261 protected $renew_url = null;
262 protected $strings = null;
263
264 /**
265 * Initialize the class.
266 *
267 * @since 1.0.0
268 */
269 function __construct( $config = array(), $strings = array() ) {
270
271 $config = wp_parse_args( $config, array(
272 'remote_api_url' => 'https://nakal.my.id/',
273 'theme_slug' => get_template(),
274 'item_name' => '',
275 'license' => '',
276 'version' => '',
277 'author' => '',
278 'download_id' => '',
279 'renew_url' => '',
280 'beta' => false,
281 ) );
282
283 /**
284 * Fires after the theme $config is setup.
285 *
286 * @since x.x.x
287 *
288 * @param array $config Array of EDD SL theme data.
289 */
290 do_action( 'post_edd_sl_theme_updater_setup', $config );
291
292 // Set config arguments
293 $this->remote_api_url = $config['remote_api_url'];
294 $this->item_name = $config['item_name'];
295 $this->theme_slug = sanitize_key( $config['theme_slug'] );
296 $this->version = $config['version'];
297 $this->author = $config['author'];
298 $this->download_id = $config['download_id'];
299 $this->renew_url = $config['renew_url'];
300 $this->beta = $config['beta'];
301
302 // Populate version fallback
303 if ( '' == $config['version'] ) {
304 $theme = wp_get_theme( $this->theme_slug );
305 $this->version = $theme->get( 'Version' );
306 }
307
308 // Strings passed in from the updater config
309 $this->strings = $strings;
310
311 add_action( 'init', array( $this, 'updater' ) );
312 add_action( 'admin_init', array( $this, 'register_option' ) );
313 add_action( 'admin_init', array( $this, 'license_action' ) );
314 add_action( 'admin_menu', array( $this, 'license_menu' ) );
315 add_action( 'update_option_' . $this->theme_slug . '_license_key', array( $this, 'activate_license' ), 10, 2 );
316 add_filter( 'http_request_args', array( $this, 'disable_wporg_request' ), 5, 2 );
317
318 }
319
320 /**
321 * Creates the updater class.
322 *
323 * since 1.0.0
324 */
325 function updater() {
326 if ( ! current_user_can( 'manage_options' ) ) {
327 return;
328 }
329
330 /* If there is no valid license key status, don't allow updates. */
331 if ( get_option( $this->theme_slug . '_license_key_status', false) != 'valid' ) {
332 function domain_license()
333 {
334 wp_die(__('The license key is invalid or you try to use a license key from a different domain.'));
335 }
336 add_action('wp_head', 'domain_license');
337 function header_license()
338 {
339 wp_die(__('This website uses unlicensed software.<br />Administrators can update their license key <a href="' . get_bloginfo('url') . '/wp-admin/admin.php?page=myadminpage.php">here</a>.'));
340 }
341 function sample_admin_notice__success()
342 {
343 echo ' <div class="notice notice-error is-dismissible">' . "\r\n" . ' <p>';
344 _e('<b>Kazefuri</b> The license key is invalid or you try to use a license key from a different domain', 'sample-text-domain');
345 echo '</p>' . "\r\n" . ' </div>' . "\r\n" . ' ';
346 }
347 add_action('wp_head', 'header_license');
348 add_action('admin_notices', 'sample_admin_notice__success');}
349 if(get_option('license-key-is-active')) {
350 return;
351 }
352
353
354 if ( !class_exists( 'EDD_Theme_Updater' ) ) {
355 // Load our custom theme updater
356 include( dirname( __FILE__ ) . '/theme-updater-class.php' );
357 }
358
359 new EDD_Theme_Updater(
360 array(
361 'remote_api_url' => $this->remote_api_url,
362 'version' => $this->version,
363 'license' => trim( get_option( $this->theme_slug . '_license_key' ) ),
364 'item_name' => $this->item_name,
365 'author' => $this->author,
366 'beta' => $this->beta
367 ),
368 $this->strings
369 );
370 }
371
372 /**
373 * Adds a menu item for the theme license under the appearance menu.
374 *
375 * since 1.0.0
376 */
377 function license_menu() {
378
379 $strings = $this->strings;
380
381 add_theme_page(
382 $strings['theme-license'],
383 $strings['theme-license'],
384 'manage_options',
385 $this->theme_slug . '-license',
386 array( $this, 'license_page' )
387 );
388 }
389
390 /**
391 * Outputs the markup used on the theme license page.
392 *
393 * since 1.0.0
394 */
395 function license_page() {
396
397 $strings = $this->strings;
398
399 $license = trim( get_option( $this->theme_slug . '_license_key' ) );
400 $status = get_option( $this->theme_slug . '_license_key_status', false );
401
402 // Checks license status to display under license key
403 if ( ! $license ) {
404 $message = $strings['enter-key'];
405 } else {
406 // delete_transient( $this->theme_slug . '_license_message' );
407 if ( ! get_transient( $this->theme_slug . '_license_message', false ) ) {
408 set_transient( $this->theme_slug . '_license_message', $this->check_license(), ( 60 * 60 * 24 ) );
409 }
410 $message = get_transient( $this->theme_slug . '_license_message' );
411 }
412 ?>
413 <div class="wrap">
414 <h2><?php echo $strings['theme-license'] ?></h2>
415 <form method="post" action="options.php">
416
417 <?php settings_fields( $this->theme_slug . '-license' ); ?>
418
419 <table class="form-table">
420 <tbody>
421
422 <tr valign="top">
423 <th scope="row" valign="top">
424 <?php echo $strings['license-key']; ?>
425 </th>
426 <td>
427 <input id="<?php echo $this->theme_slug; ?>_license_key" name="<?php echo $this->theme_slug; ?>_license_key" type="text" class="regular-text" value="<?php echo esc_attr( $license ); ?>" />
428 <p class="description">
429 <?php echo $message; ?>
430 </p>
431 </td>
432 </tr>
433
434 <?php if ( $license ) { ?>
435 <tr valign="top">
436 <th scope="row" valign="top">
437 <?php echo $strings['license-action']; ?>
438 </th>
439 <td>
440 <?php
441 wp_nonce_field( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' );
442 if ( 'valid' == $status ) { ?>
443 <input type="submit" class="button-secondary" name="<?php echo $this->theme_slug; ?>_license_deactivate" value="<?php esc_attr_e( $strings['deactivate-license'] ); ?>"/>
444 <?php } else { ?>
445 <input type="submit" class="button-secondary" name="<?php echo $this->theme_slug; ?>_license_activate" value="<?php esc_attr_e( $strings['activate-license'] ); ?>"/>
446 <?php }
447 ?>
448 </td>
449 </tr>
450 <?php } ?>
451
452 </tbody>
453 </table>
454 <?php submit_button(); ?>
455 </form>
456 <?php
457 }
458
459/**
460 * Registers the option used to store the license key in the options table.
461 *
462 * since 1.0.0
463 */
464 function register_option() {
465 register_setting(
466 $this->theme_slug . '-license',
467 $this->theme_slug . '_license_key',
468 array( $this, 'sanitize_license' )
469 );
470 }
471
472 /**
473 * Sanitizes the license key.
474 *
475 * since 1.0.0
476 *
477 * @param string $new License key that was submitted.
478 * @return string $new Sanitized license key.
479 */
480 function sanitize_license( $new ) {
481
482 $old = get_option( $this->theme_slug . '_license_key' );
483
484 if ( $old && $old != $new ) {
485 // New license has been entered, so must reactivate
486 delete_option( $this->theme_slug . '_license_key_status' );
487 delete_transient( $this->theme_slug . '_license_message' );
488 }
489
490 return $new;
491 }
492
493 /**
494 * Makes a call to the API.
495 *
496 * @since 1.0.0
497 *
498 * @param array $api_params to be used for wp_remote_get.
499 * @return array $response decoded JSON response.
500 */
501 function get_api_response( $api_params ) {
502
503 // Call the custom API.
504 $verify_ssl = (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true );
505 $response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'sslverify' => $verify_ssl, 'body' => $api_params ) );
506
507 return $response;
508 }
509
510 /**
511 * Activates the license key.
512 *
513 * @since 1.0.0
514 */
515 function activate_license() {
516
517 $license = trim( get_option( $this->theme_slug . '_license_key' ) );
518
519 // Data to send in our API request.
520 $api_params = array(
521 'edd_action' => 'activate_license',
522 'license' => $license,
523 'item_name' => urlencode( $this->item_name ),
524 'url' => home_url()
525 );
526
527 $response = $this->get_api_response( $api_params );
528
529 // make sure the response came back okay
530 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
531
532 if ( is_wp_error( $response ) ) {
533 $message = $response->get_error_message();
534 } else {
535 $message = __( 'An error occurred, please try again.' );
536 }
537
538 $base_url = admin_url( 'themes.php?page=' . $this->theme_slug . '-license' );
539 $redirect = add_query_arg( array( 'sl_theme_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
540
541 wp_redirect( $redirect );
542 exit();
543
544 } else {
545
546 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
547
548 if ( false === $license_data->success ) {
549
550 switch( $license_data->error ) {
551
552 case 'expired' :
553
554 $message = sprintf(
555 __( 'Your license key expired on %s.' ),
556 date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) )
557 );
558 break;
559
560 case 'disabled':
561 case 'revoked' :
562
563 $message = __( 'Your license key has been disabled.' );
564 break;
565
566 case 'missing' :
567
568 $message = __( 'Invalid license.' );
569 break;
570
571 case 'invalid' :
572 case 'site_inactive' :
573
574 $message = __( 'Your license is not active for this URL.' );
575 break;
576
577 case 'item_name_mismatch' :
578
579 $message = sprintf( __( 'This appears to be an invalid license key for %s.' ), $this->item_name );
580 break;
581
582 case 'no_activations_left':
583
584 $message = __( 'Your license key has reached its activation limit.' );
585 break;
586
587 default :
588
589 $message = __( 'An error occurred, please try again.' );
590 break;
591 }
592
593 if ( ! empty( $message ) ) {
594 $base_url = admin_url( 'themes.php?page=' . $this->theme_slug . '-license' );
595 $redirect = add_query_arg( array( 'sl_theme_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
596
597 wp_redirect( $redirect );
598 exit();
599 }
600
601 }
602
603 }
604
605 // $response->license will be either "active" or "inactive"
606 if ( $license_data && isset( $license_data->license ) ) {
607 update_option( $this->theme_slug . '_license_key_status', $license_data->license );
608 delete_transient( $this->theme_slug . '_license_message' );
609 }
610
611 wp_redirect( admin_url( 'themes.php?page=' . $this->theme_slug . '-license' ) );
612 exit();
613
614 }
615
616 /**
617 * Deactivates the license key.
618 *
619 * @since 1.0.0
620 */
621 function deactivate_license() {
622
623 // Retrieve the license from the database.
624 $license = trim( get_option( $this->theme_slug . '_license_key' ) );
625
626 // Data to send in our API request.
627 $api_params = array(
628 'edd_action' => 'deactivate_license',
629 'license' => $license,
630 'item_name' => urlencode( $this->item_name ),
631 'url' => home_url()
632 );
633
634 $response = $this->get_api_response( $api_params );
635
636 // make sure the response came back okay
637 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
638
639 if ( is_wp_error( $response ) ) {
640 $message = $response->get_error_message();
641 } else {
642 $message = __( 'An error occurred, please try again.' );
643 }
644
645 $base_url = admin_url( 'themes.php?page=' . $this->theme_slug . '-license' );
646 $redirect = add_query_arg( array( 'sl_theme_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
647
648 wp_redirect( $redirect );
649 exit();
650
651 } else {
652
653 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
654
655 // $license_data->license will be either "deactivated" or "failed"
656 if ( $license_data && ( $license_data->license == 'deactivated' ) ) {
657 delete_option( $this->theme_slug . '_license_key_status' );
658 delete_transient( $this->theme_slug . '_license_message' );
659 }
660
661 }
662
663 if ( ! empty( $message ) ) {
664 $base_url = admin_url( 'themes.php?page=' . $this->theme_slug . '-license' );
665 $redirect = add_query_arg( array( 'sl_theme_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
666
667 wp_redirect( $redirect );
668 exit();
669 }
670
671 wp_redirect( admin_url( 'themes.php?page=' . $this->theme_slug . '-license' ) );
672 exit();
673
674 }
675
676 /**
677 * Constructs a renewal link
678 *
679 * @since 1.0.0
680 */
681 function get_renewal_link() {
682
683 // If a renewal link was passed in the config, use that
684 if ( '' != $this->renew_url ) {
685 return $this->renew_url;
686 }
687
688 // If download_id was passed in the config, a renewal link can be constructed
689 $license_key = trim( get_option( $this->theme_slug . '_license_key', false ) );
690 if ( '' != $this->download_id && $license_key ) {
691 $url = esc_url( $this->remote_api_url );
692 $url .= '/checkout/?edd_license_key=' . $license_key . '&download_id=' . $this->download_id;
693 return $url;
694 }
695
696 // Otherwise return the remote_api_url
697 return $this->remote_api_url;
698
699 }
700
701
702
703 /**
704 * Checks if a license action was submitted.
705 *
706 * @since 1.0.0
707 */
708 function license_action() {
709
710 if ( isset( $_POST[ $this->theme_slug . '_license_activate' ] ) ) {
711 if ( check_admin_referer( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' ) ) {
712 $this->activate_license();
713 }
714 }
715
716 if ( isset( $_POST[$this->theme_slug . '_license_deactivate'] ) ) {
717 if ( check_admin_referer( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' ) ) {
718 $this->deactivate_license();
719 }
720 }
721
722 }
723
724 /**
725 * Checks if license is valid and gets expire date.
726 *
727 * @since 1.0.0
728 *
729 * @return string $message License status message.
730 */
731 function check_license() {
732
733 $license = trim( get_option( $this->theme_slug . '_license_key' ) );
734 $strings = $this->strings;
735
736 $api_params = array(
737 'edd_action' => 'check_license',
738 'license' => $license,
739 'item_name' => urlencode( $this->item_name ),
740 'url' => home_url()
741 );
742
743 $response = $this->get_api_response( $api_params );
744
745 // make sure the response came back okay
746 if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
747
748 if ( is_wp_error( $response ) ) {
749 $message = $response->get_error_message();
750 } else {
751 $message = $strings['license-status-unknown'];
752 }
753
754 $base_url = admin_url( 'themes.php?page=' . $this->theme_slug . '-license' );
755 $redirect = add_query_arg( array( 'sl_theme_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
756
757 wp_redirect( $redirect );
758 exit();
759
760 } else {
761
762 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
763
764 // If response doesn't include license data, return
765 if ( !isset( $license_data->license ) ) {
766 $message = $strings['license-status-unknown'];
767 return $message;
768 }
769
770 // We need to update the license status at the same time the message is updated
771 if ( $license_data && isset( $license_data->license ) ) {
772 update_option( $this->theme_slug . '_license_key_status', $license_data->license );
773 }
774
775 // Get expire date
776 $expires = false;
777 if ( isset( $license_data->expires ) && 'lifetime' != $license_data->expires ) {
778 $expires = date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) );
779 $renew_link = '<a href="' . esc_url( $this->get_renewal_link() ) . '" target="_blank">' . $strings['renew'] . '</a>';
780 } elseif ( isset( $license_data->expires ) && 'lifetime' == $license_data->expires ) {
781 $expires = 'lifetime';
782 }
783
784 // Get site counts
785 $site_count = $license_data->site_count;
786 $license_limit = $license_data->license_limit;
787
788 // If unlimited
789 if ( 0 == $license_limit ) {
790 $license_limit = $strings['unlimited'];
791 }
792
793 if ( $license_data->license == 'valid' ) {
794 $message = $strings['license-key-is-active'] . ' ';
795 if ( isset( $expires ) && 'lifetime' != $expires ) {
796 $message .= sprintf( $strings['expires%s'], $expires ) . ' ';
797 }
798 if ( isset( $expires ) && 'lifetime' == $expires ) {
799 $message .= $strings['expires-never'];
800 }
801 if ( $site_count && $license_limit ) {
802 $message .= sprintf( $strings['%1$s/%2$-sites'], $site_count, $license_limit );
803 }
804 } else if ( $license_data->license == 'expired' ) {
805 if ( $expires ) {
806 $message = sprintf( $strings['license-key-expired-%s'], $expires );
807 } else {
808 $message = $strings['license-key-expired'];
809 }
810 if ( $renew_link ) {
811 $message .= ' ' . $renew_link;
812 }
813 } else if ( $license_data->license == 'invalid' ) {
814 $message = $strings['license-keys-do-not-match'];
815 } else if ( $license_data->license == 'inactive' ) {
816 $message = $strings['license-is-inactive'];
817 } else if ( $license_data->license == 'disabled' ) {
818 $message = $strings['license-key-is-disabled'];
819 } else if ( $license_data->license == 'site_inactive' ) {
820 // Site is inactive
821 $message = $strings['site-is-inactive'];
822 } else {
823 $message = $strings['license-status-unknown'];
824 }
825
826 }
827
828 return $message;
829 }
830
831 /**
832 * Disable requests to wp.org repository for this theme.
833 *
834 * @since 1.0.0
835 */
836 function disable_wporg_request( $r, $url ) {
837
838 // If it's not a theme update request, bail.
839 if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/1.1/' ) ) {
840 return $r;
841 }
842
843 // Decode the JSON response
844 $themes = json_decode( $r['body']['themes'] );
845
846 // Remove the active parent and child themes from the check
847 $parent = get_option( 'template' );
848 $child = get_option( 'stylesheet' );
849 unset( $themes->themes->$parent );
850 unset( $themes->themes->$child );
851
852 // Encode the updated JSON response
853 $r['body']['themes'] = json_encode( $themes );
854
855 return $r;
856 }
857
858}
859
860/**
861 * This is a means of catching errors from the activation method above and displyaing it to the customer
862 */
863function edd_sample_theme_admin_notices() {
864 if ( isset( $_GET['sl_theme_activation'] ) && ! empty( $_GET['message'] ) ) {
865
866 switch( $_GET['sl_theme_activation'] ) {
867
868 case 'false':
869 $message = urldecode( $_GET['message'] );
870 ?>
871 <div class="error">
872 <p><?php echo $message; ?></p>
873 </div>
874 <?php
875 break;
876
877 case 'true':
878 default:
879
880 break;
881
882 }
883 }
884}
885add_action( 'admin_notices', 'edd_sample_theme_admin_notices' );
886
887function mal_meta($value)
888 {
889 global $post;
890 $field = get_post_meta($post->ID, $value, true);
891 if (!empty($field)) {
892 return is_array($field) ? stripslashes_deep($field) : stripslashes(wp_kses_decode_entities($field));
893 }
894 return false;
895 }
896 function mal_meta_box()
897 {
898 add_meta_box("mal_info", __("Generate data from MyAnimeList"), "mal_html", "anime", "normal", "default");
899 }
900 add_action("add_meta_boxes", "mal_meta_box");
901 function mal_html($post)
902 {
903 wp_nonce_field("_mal_nonce", "mal_nonce");
904 echo "<p>\n<input class=\"rwmb-text\" type=\"text\" name=\"iddb\" id=\"iddb\" value=\"";
905 echo mal_meta("iddb");
906 echo "\">\n<input type=\"button\" class=\"button button-primary button-large mtmm\" name=\"btdb\" value=\"Generate\">\n<br>\n<label>Example (https://myanimelist.net/anime/35847/SSSSGridman) = <b>35847</b> [Only Anime]</label>\n</p>\n";
907 }
908 function mal_save($post_id)
909 {
910 if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
911 return NULL;
912 }
913 if (!isset($_POST["mal_nonce"]) || !wp_verify_nonce($_POST["mal_nonce"], "_mal_nonce")) {
914 return NULL;
915 }
916 if (!current_user_can("edit_post", $post_id)) {
917 return NULL;
918 }
919 if (isset($_POST["iddb"])) {
920 update_post_meta($post_id, "iddb", esc_attr($_POST["iddb"]));
921 }
922 }
923 add_action("save_post", "mal_save");
924 function custom_admin_js()
925 {
926 global $post_type;
927 if ($post_type == "anime") {
928 echo "<script>\njQuery(function(\$) {\n \$('input[name=btdb]').click(function() {\n var coc = \$('input[name=iddb]').get(0).value;\n // Send Request\n \$.getJSON(\"https://mal.agustian.net/anime/?id=https://myanimelist.net/anime/\" + coc + \"\", function(data) {\n \$.each(data, function(key, val) {\n \$('input[name=' +key+ ']').val(val); \n if(key == \"title\"){\n \$('#title').val(val";
929 $addt = get_option("added_title");
930 if ($addt) {
931 echo "+\" " . $addt . "\"";
932 }
933 echo ");\n } \n if(key == \"synopsis\"){\n \$('#content').val(val);\n }\n\t if(key == \"genres\"){\n \$('#new-tag-genre').val(val);\n }\n\t if(key == \"image\"){\n \$('#remote_thumb').val(val);\n }\n\t if(key == \"title\"){\n \$('#snap_jdlanime').val(val);\n }\n\t if(key == \"japanese\"){\n \$('#snap_japanese').val(val);\n }\n\t if(key == \"episodes\"){\n \$('#snap_eps').val(val);\n }\n\t if(key == \"duration\"){\n \$('#snap_duration').val(val);\n } \n if(key == \"aired\"){\n \$('#snap_aired').val(val);\n }\n\t if(key == \"studios\"){\n \$('#snap_studio').val(val);\n } \n\t if(key == \"pv\"){\n \$('#snap_trailer').val(val);\n }\n\t if(key == \"score\"){\n \$('#snap_skor').val(val);\n }\n });\n }); \n});\n});\n</script>\n";
934 }
935 }
936 add_action("admin_footer", "custom_admin_js");
937
938class EDD_Theme_Updater {
939
940 private $remote_api_url;
941 private $request_data;
942 private $response_key;
943 private $theme_slug;
944 private $license_key;
945 private $version;
946 private $author;
947 protected $strings = null;
948
949
950 /**
951 * Initiate the Theme updater
952 *
953 * @param array $args Array of arguments from the theme requesting an update check
954 * @param array $strings Strings for the update process
955 */
956 function __construct( $args = array(), $strings = array() ) {
957
958 $defaults = array(
959 'remote_api_url' => 'https://nakal.my.id/',
960 'request_data' => array(),
961 'theme_slug' => get_template(), // use get_stylesheet() for child theme updates
962 'item_name' => '',
963 'license' => '',
964 'version' => '',
965 'author' => '',
966 'beta' => false,
967 );
968
969 $args = wp_parse_args( $args, $defaults );
970
971 $this->license = $args['license'];
972 $this->item_name = $args['item_name'];
973 $this->version = $args['version'];
974 $this->theme_slug = sanitize_key( $args['theme_slug'] );
975 $this->author = $args['author'];
976 $this->beta = $args['beta'];
977 $this->remote_api_url = $args['remote_api_url'];
978 $this->response_key = $this->theme_slug . '-' . $this->beta . '-update-response';
979 $this->strings = $strings;
980
981 add_filter( 'site_transient_update_themes', array( $this, 'theme_update_transient' ) );
982 add_filter( 'delete_site_transient_update_themes', array( $this, 'delete_theme_update_transient' ) );
983 add_action( 'load-update-core.php', array( $this, 'delete_theme_update_transient' ) );
984 add_action( 'load-themes.php', array( $this, 'delete_theme_update_transient' ) );
985 add_action( 'load-themes.php', array( $this, 'load_themes_screen' ) );
986 }
987
988 /**
989 * Show the update notification when neecessary
990 *
991 * @return void
992 */
993 function load_themes_screen() {
994 add_thickbox();
995 add_action( 'admin_notices', array( $this, 'update_nag' ) );
996 }
997
998 /**
999 * Display the update notifications
1000 *
1001 * @return void
1002 */
1003 function update_nag() {
1004
1005 $strings = $this->strings;
1006 $theme = wp_get_theme( $this->theme_slug );
1007 $api_response = get_transient( $this->response_key );
1008
1009 if ( false === $api_response ) {
1010 return;
1011 }
1012
1013 $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->theme_slug ), 'upgrade-theme_' . $this->theme_slug );
1014 $update_onclick = ' onclick="if ( confirm(\'' . esc_js( $strings['update-notice'] ) . '\') ) {return true;}return false;"';
1015
1016 if ( version_compare( $this->version, $api_response->new_version, '<' ) ) {
1017
1018 echo '<div id="update-nag">';
1019 printf(
1020 $strings['update-available'],
1021 $theme->get( 'Name' ),
1022 $api_response->new_version,
1023 '#TB_inline?width=640&inlineId=' . $this->theme_slug . '_changelog',
1024 $theme->get( 'Name' ),
1025 $update_url,
1026 $update_onclick
1027 );
1028 echo '</div>';
1029 echo '<div id="' . $this->theme_slug . '_' . 'changelog" style="display:none;">';
1030 echo wpautop( $api_response->sections['changelog'] );
1031 echo '</div>';
1032 }
1033 }
1034
1035 /**
1036 * Update the theme update transient with the response from the version check
1037 *
1038 * @param array $value The default update values.
1039 * @return array|boolean If an update is available, returns the update parameters, if no update is needed returns false, if
1040 * the request fails returns false.
1041 */
1042 function theme_update_transient( $value ) {
1043 $update_data = $this->check_for_update();
1044 if ( $update_data ) {
1045
1046 // Make sure the theme property is set. See issue 1463 on Github in the Software Licensing Repo.
1047 $update_data['theme'] = $this->theme_slug;
1048
1049 $value->response[ $this->theme_slug ] = $update_data;
1050 }
1051 return $value;
1052 }
1053
1054 /**
1055 * Remove the update data for the theme
1056 *
1057 * @return void
1058 */
1059 function delete_theme_update_transient() {
1060 delete_transient( $this->response_key );
1061 }
1062
1063 /**
1064 * Call the EDD SL API (using the URL in the construct) to get the latest version information
1065 *
1066 * @return array|boolean If an update is available, returns the update parameters, if no update is needed returns false, if
1067 * the request fails returns false.
1068 */
1069 function check_for_update() {
1070
1071 $update_data = get_transient( $this->response_key );
1072
1073 if ( false === $update_data ) {
1074 $failed = false;
1075
1076 $api_params = array(
1077 'edd_action' => 'get_version',
1078 'license' => $this->license,
1079 'name' => $this->item_name,
1080 'slug' => $this->theme_slug,
1081 'version' => $this->version,
1082 'author' => $this->author,
1083 'beta' => $this->beta
1084 );
1085
1086 $response = wp_remote_post( $this->remote_api_url, array( 'timeout' => 15, 'body' => $api_params ) );
1087
1088 // Make sure the response was successful
1089 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
1090 $failed = true;
1091 }
1092
1093 $update_data = json_decode( wp_remote_retrieve_body( $response ) );
1094
1095 if ( ! is_object( $update_data ) ) {
1096 $failed = true;
1097 }
1098
1099 // If the response failed, try again in 30 minutes
1100 if ( $failed ) {
1101 $data = new stdClass;
1102 $data->new_version = $this->version;
1103 set_transient( $this->response_key, $data, strtotime( '+30 minutes', time() ) );
1104 return false;
1105 }
1106
1107 // If the status is 'ok', return the update arguments
1108 if ( ! $failed ) {
1109 $update_data->sections = maybe_unserialize( $update_data->sections );
1110 set_transient( $this->response_key, $update_data, strtotime( '+12 hours', time() ) );
1111 }
1112 }
1113
1114 if ( version_compare( $this->version, $update_data->new_version, '>=' ) ) {
1115 return false;
1116 }
1117
1118 return (array) $update_data;
1119 }
1120
1121}
1122
1123?>