class-license-menu.php 44.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
<?php

/**
 * @copyright   Copyright (c) Todd Lahman LLC
 * @since       https://www.toddlahman.com/
 *
 *    Intellectual Property rights, and copyright, reserved by Todd Lahman, LLC as allowed by law include,
 *    but are not limited to, the working concept, function, and behavior of this library,
 *    the logical code structure and expression as written.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


class BackWPup_Pro_License_Menu {

	/**
	 * @var null
	 */
	protected static $_instance = null;
	/**
	 * @var string
	 */
	public $file             = '';
	public $software_title   = '';
	public $software_version = '';
	public $plugin_or_theme  = '';
	public $api_url          = '';
	public $data_prefix      = '';
	public $slug             = '';
	public $plugin_name      = '';
	public $text_domain      = '';
	public $extra            = '';

	public function __construct( $file, $software_title, $software_version, $plugin_or_theme, $api_url, $text_domain = '', $extra = '' ) {
		$this->file            = $file;
		$this->software_title  = $software_title;
		$this->version         = $software_version;
		$this->plugin_or_theme = $plugin_or_theme;
		$this->api_url         = $api_url;
		$this->text_domain     = $text_domain;
		$this->data_prefix     = $slug = str_ireplace( array( ' ', '_', '&', '?' ), '_', strtolower( $this->software_title ) );
		$this->extra           = $extra;

		add_action( 'backwpup_admin_pages', array( $this, 'register_menu' ) );
		add_action( 'admin_init', array( $this, 'load_settings' ) );

		if ( is_admin() ) {
			// Check for external connection blocking
			add_action( 'admin_notices', array( $this, 'check_external_blocking' ) );

			/**
			 * Software Product ID is the product title string
			 * This value must be unique, and it must match the API tab for the product in WooCommerce
			 */
			$this->ame_software_product_id = $this->software_title;

			/**
			 * Set all data defaults here
			 */
			$this->ame_data_key                = $this->data_prefix . '_data';
			$this->ame_api_key                 = 'api_key';
			$this->ame_activation_email        = 'activation_email';
			$this->ame_product_id_key          = $this->data_prefix . '_product_id';
			$this->ame_instance_key            = $this->data_prefix . '_instance';
			$this->ame_deactivate_checkbox_key = $this->data_prefix . '_deactivate_checkbox';
			$this->ame_activated_key           = $this->data_prefix . '_activated';

			/**
			 * Set all admin menu data
			 */
			$this->ame_deactivate_checkbox         = $this->data_prefix . '_deactivate_checkbox';
			$this->ame_activation_tab_key          = $this->data_prefix . '_dashboard';
			$this->ame_deactivation_tab_key        = $this->data_prefix . '_deactivation';
			$this->ame_menu_tab_activation_title   = __( 'API Key Activation', $this->text_domain );
			$this->ame_menu_tab_deactivation_title = __( 'API Key Deactivation', $this->text_domain );
			$this->ame_settings_menu_title         = $this->software_title . ' ' . __( 'Activation', $this->text_domain );
			$this->ame_settings_title              = $this->software_title . ' ' . $this->ame_menu_tab_activation_title;

			/**
			 * Set all software update data here
			 */
			$this->ame_options           = get_option( $this->ame_data_key );
			$this->ame_plugin_name       = $this->plugin_or_theme == 'plugin' ? untrailingslashit( plugin_basename( $this->file ) ) : get_stylesheet(); // same as plugin slug. if a theme use a theme name like 'twentyeleven'
			$this->ame_product_id        = get_option( $this->ame_product_id_key ); // Software Title
			$this->ame_renew_license_url = $this->api_url . 'my-account'; // URL to renew an API Key. Trailing slash in the upgrade_url is required.
			$this->ame_instance_id       = get_option( $this->ame_instance_key ); // Instance ID (unique to each blog activation)
			/**
			 * Some web hosts have security policies that block the : (colon) and // (slashes) in http://,
			 * so only the host portion of the URL can be sent. For example the host portion might be
			 * www.example.com or example.com. http://www.example.com includes the scheme http,
			 * and the host www.example.com.
			 * Sending only the host also eliminates issues when a client site changes from http to https,
			 * but their activation still uses the original scheme.
			 * To send only the host, use a line like the one below:
			 *
			 * $this->ame_domain = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
			 */
			$this->ame_domain           = str_ireplace( array( 'http://', 'https://' ), '', home_url() ); // blog domain name
			$this->ame_software_version = $this->version; // The software version
			$this->ame_plugin_or_theme  = $this->plugin_or_theme; // 'theme' or 'plugin'
			$options                    = get_option( $this->ame_data_key );

			/**
			 * Check for software updates
			 */
			if ( ! empty( $options ) && $options !== false ) {
				$this->check_for_update();
			}

			if ( get_option( $this->ame_activated_key ) != 'Activated' ) {
				add_action( 'after_plugin_row_' . plugin_basename( $file ), array( $this, 'license_row' ), 12, 2 );
			}
		}

		if ( $this->plugin_or_theme == 'plugin' ) {
			$option = get_option( $this->data_prefix . '_data', false );
			if ( $option === false ) {
				$this->activation();
			}
		}

		/**
		 * Deletes all data if plugin deactivated
		 */
		if ( $this->plugin_or_theme == 'plugin' ) {
			register_deactivation_hook( $this->file, array( $this, 'uninstall' ) );
		}

		if ( $this->plugin_or_theme == 'theme' ) {
			add_action( 'switch_theme', array( $this, 'uninstall' ) );
		}
	}

	/**
	 * Check for software updates.
	 */
	public function check_for_update() {
		$this->plugin_name = $this->ame_plugin_name;

		// Slug should be the same as the plugin/theme directory name
		if ( strpos( $this->plugin_name, '.php' ) !== 0 ) {
			$this->slug = dirname( $this->plugin_name );
		} else {
			$this->slug = $this->plugin_name;
		}

		/*********************************************************************
		 * The plugin and theme filters should not be active at the same time
		 *********************************************************************/
		/**
		 * More info:
		 * function set_site_transient moved from wp-includes/functions.php
		 * to wp-includes/option.php in WordPress 3.4
		 *
		 * set_site_transient() contains the pre_set_site_transient_{$transient} filter
		 * {$transient} is either update_plugins or update_themes
		 *
		 * Transient data for plugins and themes exist in the Options table:
		 * _site_transient_update_themes
		 * _site_transient_update_plugins
		 */

		// uses the flag above to determine if this is a plugin or a theme update request
		if ( $this->plugin_or_theme == 'plugin' ) {
			/**
			 * Plugin Updates
			 */
			// Check For Plugin Updates
			add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_check' ) );
			// Check For Plugin Information to display on the update details page
			add_filter( 'plugins_api', array( $this, 'request' ), 10, 3 );
		} else if ( $this->plugin_or_theme == 'theme' ) {
			/**
			 * Theme Updates
			 */
			// Check For Theme Updates
			add_filter( 'pre_set_site_transient_update_themes', array( $this, 'update_check' ) );

			// Check For Theme Information to display on the update details page
			//add_filter( 'themes_api', array( $this, 'request' ), 10, 3 );

		}
	}

	/**
	 * Generate the default data arrays
	 */
	public function activation() {
		$global_options = array(
			$this->ame_api_key          => '',
			$this->ame_activation_email => '',
		);

		update_option( $this->ame_data_key, $global_options );

		$single_options = array(
			$this->ame_product_id_key          => $this->ame_software_product_id,
			$this->ame_instance_key            => wp_generate_password( 12, false ),
			$this->ame_deactivate_checkbox_key => 'on',
			$this->ame_activated_key           => 'Deactivated',
		);

		foreach ( $single_options as $key => $value ) {
			update_option( $key, $value );
		}
	}

	/**
	 * @param string $file             Must be $this->file from the root plugin file, or theme functions file.
	 * @param string $software_title   Must be exactly the same as the Software Title in the product.
	 * @param string $software_version This products current software version.
	 * @param string $plugin_or_theme  'plugin' or 'theme'
	 * @param string $api_url          The URL to the site that is running the API Manager. Example: https://www.toddlahman.com/ Must have a trailing slash.
	 * @param string $extra            Extra data. Whatever you want.
	 *
	 * @return \AM_License_Menu|null
	 */
	public static function instance( $file, $software_title, $software_version, $plugin_or_theme, $api_url, $extra ) {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self( $file, $software_title, $software_version, $plugin_or_theme, $api_url, $extra );
		}

		return self::$_instance;
	}

	/**
	 * Register submenu specific to this product.
	 */
	public function register_menu() {
		add_submenu_page(
			$this->text_domain,
			__( $this->ame_settings_menu_title, $this->text_domain ),
			__( $this->ame_settings_menu_title, $this->text_domain ),
			'manage_options',
			$this->ame_activation_tab_key,
			array( $this, 'config_page' )
		);
	}

	/**
	 * Deletes all data if plugin deactivated
	 *
	 * @return void
	 */
	public function uninstall() {
		global $blog_id;

		$this->license_key_deactivation();

		// Remove options
		if ( is_multisite() ) {
			switch_to_blog( $blog_id );

			foreach (
				array(
					$this->ame_data_key,
					$this->ame_product_id_key,
					$this->ame_instance_key,
					$this->ame_deactivate_checkbox_key,
					$this->ame_activated_key,
				) as $option
			) {

				delete_option( $option );
			}

			restore_current_blog();
		} else {
			foreach (
				array(
					$this->ame_data_key,
					$this->ame_product_id_key,
					$this->ame_instance_key,
					$this->ame_deactivate_checkbox_key,
					$this->ame_activated_key
				) as $option
			) {

				delete_option( $option );
			}
		}
	}

	/**
	 * Deactivates the license on the API server
	 *
	 * @return void
	 */
	public function license_key_deactivation() {
		$activation_status = get_option( $this->ame_activated_key );
		$api_email         = $this->ame_options[ $this->ame_activation_email ];
		$api_key           = $this->ame_options[ $this->ame_api_key ];

		$args = array(
			'email'       => $api_email,
			'licence_key' => $api_key,
		);

		if ( $activation_status == 'Activated' && $api_key != '' && $api_email != '' ) {
			$this->deactivate( $args ); // reset API Key activation
		}
	}

	public function deactivate( $args ) {

		$defaults = array(
			'request'    => 'deactivation',
			'product_id' => $this->ame_product_id,
			'instance'   => $this->ame_instance_id,
			'platform'   => $this->ame_domain
		);

		$args = wp_parse_args( $defaults, $args );

		$target_url = esc_url_raw( $this->create_software_api_url( $args ) );

		$request = wp_safe_remote_get( $target_url );

		// $request = wp_remote_post( $this->api_url . 'wc-api/am-software-api/', array( 'body' => $args ) );

		if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
			// Request failed
			return false;
		}

		$response = wp_remote_retrieve_body( $request );

		return $response;
	}

	// Draw option page

	public function create_software_api_url( $args ) {

		$api_url = add_query_arg( 'wc-api', 'am-software-api', $this->api_url );

		return $api_url . '&' . http_build_query( $args );
	}

	// Register settings

	/**
	 * Check for external blocking contstant.
	 */
	public function check_external_blocking() {
		// show notice if external requests are blocked through the WP_HTTP_BLOCK_EXTERNAL constant
		if ( defined( 'WP_HTTP_BLOCK_EXTERNAL' ) && WP_HTTP_BLOCK_EXTERNAL === true ) {
			// check if our API endpoint is in the allowed hosts
			$host = parse_url( $this->api_url, PHP_URL_HOST );

			if ( ! defined( 'WP_ACCESSIBLE_HOSTS' ) || stristr( WP_ACCESSIBLE_HOSTS, $host ) === false ) {
				?>
				<div class="error">
					<p><?php printf( __( '<b>Warning!</b> You\'re blocking external requests which means you won\'t be able to get %s updates. Please add %s to %s.', $this->text_domain ), $this->ame_software_product_id, '<strong>' . $host . '</strong>', '<code>WP_ACCESSIBLE_HOSTS</code>' ); ?></p>
				</div>
				<?php
			}
		}
	}

	// Provides text for api key section

	public function config_page() {
		$settings_tabs = array(
			$this->ame_activation_tab_key   => __( $this->ame_menu_tab_activation_title, $this->text_domain ),
			$this->ame_deactivation_tab_key => __( $this->ame_menu_tab_deactivation_title, $this->text_domain )
		);
		$current_tab   = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $this->ame_activation_tab_key;
		$tab           = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : $this->ame_activation_tab_key;
		?>
		<div class='wrap'>
			<h2><?php _e( $this->ame_settings_title, $this->text_domain ); ?></h2>
			<h2 class="nav-tab-wrapper">
				<?php
				foreach ( $settings_tabs as $tab_page => $tab_name ) {
					$active_tab = $current_tab == $tab_page ? 'nav-tab-active' : '';
					echo '<a class="nav-tab ' . $active_tab . '" href="?page=' . $this->ame_activation_tab_key . '&tab=' . $tab_page . '">' . $tab_name . '</a>';
				}
				?>
			</h2>
			<form method='post'>
				<div class="main">
					<?php
					if ( $tab == $this->ame_activation_tab_key ) {
						settings_fields( $this->ame_data_key );
						do_settings_sections( $this->ame_activation_tab_key );
						submit_button( __( 'Save Changes', $this->text_domain ) );
					} else {
						settings_fields( $this->ame_deactivate_checkbox );
						do_settings_sections( $this->ame_deactivation_tab_key );
						submit_button( __( 'Save Changes', $this->text_domain ) );
					}
					?>
				</div>
			</form>
		</div>
		<?php
	}

	// Returns the API Key status from the WooCommerce API Manager on the server

	public function load_settings() {
		register_setting( $this->ame_data_key, $this->ame_data_key, array( $this, 'validate_options' ) );
		// API Key
		add_settings_section( $this->ame_api_key, __( 'API Key Activation', $this->text_domain ), array(
			$this,
			'wc_am_api_key_text'
		), $this->ame_activation_tab_key );
		add_settings_field( 'status', __( 'API Key Status', $this->text_domain ), array(
			$this,
			'wc_am_api_key_status'
		), $this->ame_activation_tab_key, $this->ame_api_key );
		add_settings_field( $this->ame_api_key, __( 'API Key', $this->text_domain ), array(
			$this,
			'wc_am_api_key_field'
		), $this->ame_activation_tab_key, $this->ame_api_key );
		add_settings_field( $this->ame_activation_email, __( 'API Email', $this->text_domain ), array(
			$this,
			'wc_am_api_email_field'
		), $this->ame_activation_tab_key, $this->ame_api_key );
		// Activation settings
		register_setting( $this->ame_deactivate_checkbox, $this->ame_deactivate_checkbox, array( $this, 'wc_am_license_key_deactivation' ) );
		add_settings_section( 'deactivate_button', __( 'API Deactivation', $this->text_domain ), array(
			$this,
			'wc_am_deactivate_text'
		), $this->ame_deactivation_tab_key );
		add_settings_field( 'deactivate_button', __( 'Deactivate API Key', $this->text_domain ), array(
			$this,
			'wc_am_deactivate_textarea'
		), $this->ame_deactivation_tab_key, 'deactivate_button' );
		//Fix for register_setting() not trigger
		if ( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] === $this->ame_activation_tab_key && isset( $_POST[ $this->ame_data_key ] ) ) {
			update_option( $this->ame_data_key, $_POST[ $this->ame_data_key ] );
			$this->ame_options = get_option( $this->ame_data_key );
		}
		if (  isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] === $this->ame_activation_tab_key && isset( $_GET[ 'tab' ] ) && $_GET[ 'tab' ] === $this->ame_deactivation_tab_key && isset( $_POST[ $this->ame_deactivate_checkbox ] ) ) {
			update_option( $this->ame_deactivate_checkbox, $_POST[ $this->ame_deactivate_checkbox ] );
			$this->ame_options = get_option( $this->ame_data_key );
		}
	}

	// Returns API Key text field

	public function validate_options( $input ) {
		// Load existing options, validate, and update with changes from input before returning
		$options                                = $this->ame_options;
		if ( empty( $input[ $this->ame_api_key ] ) ){
			$input[ $this->ame_activation_email ] = $input[$this->ame_data_key ][ $this->ame_activation_email ];
			$input[ $this->ame_api_key ] = $input[$this->ame_data_key ][ $this->ame_api_key ];
		}
		$options[ $this->ame_api_key ]          = trim( $input[ $this->ame_api_key ] );
		$options[ $this->ame_activation_email ] = trim( $input[ $this->ame_activation_email ] );
		$api_email                              = trim( $input[ $this->ame_activation_email ] );
		$api_key                                = trim( $input[ $this->ame_api_key ] );
		$activation_status                      = get_option( $this->ame_activated_key );
		$checkbox_status                        = get_option( $this->ame_deactivate_checkbox );
		$current_api_key                        = $this->ame_options[ $this->ame_api_key ];
		// Should match the settings_fields() value

		if ( $_REQUEST[ 'option_page' ] != $this->ame_deactivate_checkbox ) {
			if ( $activation_status == 'Deactivated' || $activation_status == '' || $api_key == '' || $api_email == '' || $checkbox_status == 'on' || $current_api_key != $api_key ) {
				/**
				 * If this is a new key, and an existing key already exists in the database,
				 * deactivate the existing key before activating the new key.
				 */
				if ( $current_api_key != $api_key ) {
					$this->replace_license_key( $current_api_key );
				}

				$args             = array(
					'email'       => $api_email,
					'licence_key' => $api_key,
				);
				$activate_results = json_decode( $this->activate( $args ), true );


				if ( $activate_results[ 'activated' ] === true ) {
					add_settings_error( 'activate_text', 'activate_msg', sprintf( __( '%s activated. ', $this->text_domain ), esc_attr( $this->software_title ) ) . "{$activate_results['message']}.", 'updated' );
					update_option( $this->ame_activated_key, 'Activated' );
					//update_option( $this->ame_data_key, $options ); //makes endless loop
					update_option( $this->ame_deactivate_checkbox, 'off' );
				}

				if ( $activate_results == false ) {
					add_settings_error( 'api_key_check_text', 'api_key_check_error', __( 'Connection failed to the License Key API server. Try again later.', $this->text_domain ), 'error' );
					$options[ $this->ame_api_key ]          = '';
					$options[ $this->ame_activation_email ] = '';
					update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
				}

				if ( isset( $activate_results[ 'code' ] ) ) {
					switch ( $activate_results[ 'code' ] ) {
						case '100':
							add_settings_error( 'api_email_text', 'api_email_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_activation_email ] = '';
							$options[ $this->ame_api_key ]          = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '101':
							add_settings_error( 'api_key_text', 'api_key_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '102':
							add_settings_error( 'api_key_purchase_incomplete_text', 'api_key_purchase_incomplete_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '103':
							add_settings_error( 'api_key_exceeded_text', 'api_key_exceeded_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '104':
							add_settings_error( 'api_key_not_activated_text', 'api_key_not_activated_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '105':
							add_settings_error( 'api_key_invalid_text', 'api_key_invalid_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
						case '106':
							add_settings_error( 'sub_not_active_text', 'sub_not_active_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
							$options[ $this->ame_api_key ]          = '';
							$options[ $this->ame_activation_email ] = '';
							update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
							break;
					}
				}
			} // End Plugin Activation
		}

		return $options;
	}

	// Returns API email text field

	public function replace_license_key( $current_api_key ) {
		$args = array(
			'email'       => $this->ame_options[ $this->ame_activation_email ],
			'licence_key' => $current_api_key,
		);

		$reset = $this->deactivate( $args ); // reset API Key activation

		if ( $reset == true ) {
			return true;
		}

		return add_settings_error( 'not_deactivated_text', 'not_deactivated_error', __( 'The API Key could not be deactivated. Use the API Key Deactivation tab to manually deactivate the API Key before activating a new API Key.', $this->text_domain ), 'updated' );
	}

	// Sanitizes and validates all input and output for Dashboard

	public function activate( $args ) {

		$defaults = array(
			'request'          => 'activation',
			'product_id'       => $this->ame_product_id,
			'instance'         => $this->ame_instance_id,
			'platform'         => $this->ame_domain,
			'software_version' => $this->ame_software_version
		);

		$args = wp_parse_args( $defaults, $args );

		$target_url = esc_url_raw( $this->create_software_api_url( $args ) );

		$request = wp_safe_remote_get( $target_url );

		// $request = wp_remote_post( $this->api_url . 'wc-api/am-software-api/', array( 'body' => $args ) );

		if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
			// Request failed
			return false;
		}

		$response = wp_remote_retrieve_body( $request );

		return $response;
	}

	// Returns the API Key status from the WooCommerce API Manager on the server

	public function wc_am_api_key_text() {
	}

	// Deactivate the current API Key before activating the new API Key

	public function wc_am_api_key_status() {
		$license_status       = $this->license_key_status();
		$license_status_check = ( ! empty( $license_status[ 'status_check' ] ) && $license_status[ 'status_check' ] == 'active' ) ?  __( 'Activated', $this->text_domain ) : __( 'Deactivated', $this->text_domain );
		if ( ! empty( $license_status_check ) ) {
			echo $license_status_check;
		}
	}

	// Deactivates the API Key to allow key to be used on another blog

	public function license_key_status() {
		//$activation_status = get_option( $this->ame_activated_key );
		$args = array(
			'email'       => $this->ame_options[ $this->ame_activation_email ],
			'licence_key' => $this->ame_options[ $this->ame_api_key ],
		);

		return json_decode( $this->status( $args ), true );
	}

	/**
	 * Checks if the software is activated or deactivated
	 *
	 * @param  array $args
	 *
	 * @return array
	 */
	public function status( $args ) {

		$defaults = array(
			'request'    => 'status',
			'product_id' => $this->ame_product_id,
			'instance'   => $this->ame_instance_id,
			'platform'   => $this->ame_domain
		);

		$args = wp_parse_args( $defaults, $args );

		$target_url = esc_url_raw( $this->create_software_api_url( $args ) );

		$request = wp_safe_remote_get( $target_url );

		// $request = wp_remote_post( $this->api_url . 'wc-api/am-software-api/', array( 'body' => $args ) );

		if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
			// Request failed
			return false;
		}

		$response = wp_remote_retrieve_body( $request );

		return $response;
	}

	public function wc_am_api_key_field() {
		echo "<input id='api_key' name='" . $this->ame_data_key . "[" . $this->ame_api_key . "]' size='25' type='text' value='" . $this->ame_options[ $this->ame_api_key ] . "' />";
		if ( $this->ame_options[ $this->ame_api_key ] ) {
			echo "<span class='dashicons dashicons-yes' style='color: #66ab03;'></span>";
		} else {
			echo "<span class='dashicons dashicons-no' style='color: #ca336c;'></span>";
		}
	}

	// Loads admin style sheets

	public function wc_am_api_email_field() {
		echo "<input id='activation_email' name='" . $this->ame_data_key . "[" . $this->ame_activation_email . "]' size='25' type='text' value='" . $this->ame_options[ $this->ame_activation_email ] . "' />";
		if ( $this->ame_options[ $this->ame_activation_email ] ) {
			echo "<span class='dashicons dashicons-yes' style='color: #66ab03;'></span>";
		} else {
			echo "<span class='dashicons dashicons-no' style='color: #ca336c;'></span>";
		}
	}

	// API Key URL

	public function wc_am_license_key_deactivation( $input ) {
		$activation_status = get_option( $this->ame_activated_key );
		$args              = array(
			'email'       => $this->ame_options[ $this->ame_activation_email ],
			'licence_key' => $this->ame_options[ $this->ame_api_key ],
		);

		// For testing activation status_extra data
		// $activate_results = json_decode( $this->status( $args ), true );
		// print_r($activate_results); exit;

		$options = ( $input == 'on' ? 'on' : 'off' );
		if ( $options == 'on' && $activation_status == 'Activated' && $this->ame_options[ $this->ame_api_key ] != '' && $this->ame_options[ $this->ame_activation_email ] != '' ) {
			// deactivates API Key key activation
			$activate_results = json_decode( $this->deactivate( $args ), true );

			// Used to display results for development
			//print_r($activate_results); exit();

			if ( $activate_results[ 'deactivated' ] === true ) {
				$update = array(
					$this->ame_api_key          => '',
					$this->ame_activation_email => ''
				);

				$merge_options = array_merge( $this->ame_options, $update );

				update_option( $this->ame_data_key, $merge_options );
				update_option( $this->ame_activated_key, 'Deactivated' );
				add_settings_error( 'wc_am_deactivate_text', 'deactivate_msg', __( 'API Key deactivated. ', $this->text_domain ) . "{$activate_results['activations_remaining']}.", 'updated' );

				return $options;
			}

			if ( isset( $activate_results[ 'code' ] ) ) {
				switch ( $activate_results[ 'code' ] ) {
					case '100':
						add_settings_error( 'api_email_text', 'api_email_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_activation_email ] = '';
						$options[ $this->ame_api_key ]          = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '101':
						add_settings_error( 'api_key_text', 'api_key_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '102':
						add_settings_error( 'api_key_purchase_incomplete_text', 'api_key_purchase_incomplete_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '103':
						add_settings_error( 'api_key_exceeded_text', 'api_key_exceeded_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '104':
						add_settings_error( 'api_key_not_activated_text', 'api_key_not_activated_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '105':
						add_settings_error( 'api_key_invalid_text', 'api_key_invalid_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
					case '106':
						add_settings_error( 'sub_not_active_text', 'sub_not_active_error', "{$activate_results['error']}. {$activate_results['additional info']}", 'error' );
						$options[ $this->ame_api_key ]          = '';
						$options[ $this->ame_activation_email ] = '';
						update_option( $this->ame_options[ $this->ame_activated_key ], 'Deactivated' );
						break;
				}
			}
		} else {

			return $options;
		}

		return false;
	}

	public function wc_am_deactivate_text() { }

	public function wc_am_deactivate_textarea() {
		echo '<input type="checkbox" id="' . $this->ame_deactivate_checkbox . '" name="' . $this->ame_deactivate_checkbox . '" value="on"';
		echo checked( get_option( $this->ame_deactivate_checkbox ), 'on' );
		echo '/>';
		?><span class="description"><?php _e( 'Deactivates an API Key so it can be used on another blog.', $this->text_domain ); ?></span>
		<?php
	}

	public function css_scripts() {
		wp_register_style( $this->ame_data_key . '-css', $this->plugin_url() . 'am/admin-settings.css', array(), $this->version, 'all' );
		wp_enqueue_style( $this->ame_data_key . '-css' );
	}

	/**
	 * Returns the plugin URL.
	 *
	 * @return string
	 */
	public function plugin_url() {
		if ( $this->plugin_or_theme == 'plugin' ) {
			return plugins_url( '/', $this->file );
		}

		if ( $this->plugin_or_theme == 'theme' ) {
			return get_stylesheet_directory_uri() . '/';
		}

		return plugins_url( '/', $this->file );
	}

	// Upgrade API URL

	/**
	 * Check for updates against the remote server.
	 *
	 * @since  1.0.0
	 *
	 * @param  object $transient
	 *
	 * @return object $transient
	 */
	public function update_check( $transient ) {
		if ( empty( $transient->checked ) ) {
			return $transient;
		}

		$args = array(
			'request'          => 'pluginupdatecheck',
			'slug'             => $this->slug,
			'plugin_name'      => $this->plugin_name,
			//'version'			=>	$transient->checked[$this->plugin_name],
			'version'          => $this->ame_software_version,
			'product_id'       => $this->ame_product_id,
			'api_key'          => $this->ame_options[ $this->ame_api_key ],
			'activation_email' => $this->ame_options[ $this->ame_activation_email ],
			'instance'         => $this->ame_instance_id,
			'domain'           => $this->ame_domain,
			'software_version' => $this->ame_software_version,
			'extra'            => $this->extra,
		);

		// Check for a plugin update
		$response = $this->plugin_information( $args );
		// Displays an admin error message in the WordPress dashboard
		$this->check_response_for_errors( $response );

		// Set version variables
		if ( isset( $response ) && is_object( $response ) && $response !== false ) {
			// New plugin version from the API
			$new_ver = (string) $response->new_version;
			// Current installed plugin version
			$curr_ver = (string) $this->ame_software_version;
			//$curr_ver = (string)$transient->checked[$this->plugin_name];
		}

		// If there is a new version, modify the transient to reflect an update is available
		if ( isset( $new_ver ) && isset( $curr_ver ) ) {
			if ( $response !== false && version_compare( $new_ver, $curr_ver, '>' ) ) {
				if ( $this->plugin_or_theme == 'plugin' ) {
					$transient->response[ $this->plugin_name ] = $response;
				} else if ( $this->plugin_or_theme == 'theme' ) {
					$transient->response[ $this->plugin_name ][ 'new_version' ] = $response->new_version;
					$transient->response[ $this->plugin_name ][ 'url' ]         = $response->url;
					$transient->response[ $this->plugin_name ][ 'package' ]     = $response->package;
				}
			}
		}

		return $transient;
	}

	/**
	 * Sends and receives data to and from the server API
	 *
	 * @since  1.0.0
	 *
	 * @return object $response
	 */
	public function plugin_information( $args ) {
		$target_url = esc_url_raw( $this->create_upgrade_api_url( $args ) );
		$request    = wp_safe_remote_get( $target_url );

		//$request = wp_remote_post( $this->api_url . 'wc-api/upgrade-api/', array( 'body' => $args ) );

		if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
			return false;
		}

		$response = unserialize( wp_remote_retrieve_body( $request ) );

		/**
		 * For debugging errors from the API
		 * For errors like: unserialize(): Error at offset 0 of 170 bytes
		 * Comment out $response above first
		 */
		// $response = wp_remote_retrieve_body( $request );
		// print_r($response); exit;

		if ( is_object( $response ) ) {
			return $response;
		} else {
			return false;
		}
	}

	private function create_upgrade_api_url( $args ) {
		$upgrade_url = add_query_arg( 'wc-api', 'upgrade-api', $this->api_url );

		return $upgrade_url . '&' . http_build_query( $args );
	}

	/**
	 * Displays an admin error message in the WordPress dashboard
	 *
	 * @param  array $response
	 *
	 * @return string
	 */
	public function check_response_for_errors( $response ) {
		if ( ! empty( $response ) ) {
			if ( isset( $response->errors[ 'no_key' ] ) && $response->errors[ 'no_key' ] == 'no_key' && isset( $response->errors[ 'no_subscription' ] ) && $response->errors[ 'no_subscription' ] == 'no_subscription' ) {
				add_action( 'admin_notices', array( $this, 'no_key_error_notice' ) );
				add_action( 'admin_notices', array( $this, 'no_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'exp_license' ] ) && $response->errors[ 'exp_license' ] == 'exp_license' ) {
				add_action( 'admin_notices', array( $this, 'expired_license_error_notice' ) );
			} else if ( isset( $response->errors[ 'hold_subscription' ] ) && $response->errors[ 'hold_subscription' ] == 'hold_subscription' ) {
				add_action( 'admin_notices', array( $this, 'on_hold_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'cancelled_subscription' ] ) && $response->errors[ 'cancelled_subscription' ] == 'cancelled_subscription' ) {
				add_action( 'admin_notices', array( $this, 'cancelled_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'exp_subscription' ] ) && $response->errors[ 'exp_subscription' ] == 'exp_subscription' ) {
				add_action( 'admin_notices', array( $this, 'expired_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'suspended_subscription' ] ) && $response->errors[ 'suspended_subscription' ] == 'suspended_subscription' ) {
				add_action( 'admin_notices', array( $this, 'suspended_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'pending_subscription' ] ) && $response->errors[ 'pending_subscription' ] == 'pending_subscription' ) {
				add_action( 'admin_notices', array( $this, 'pending_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'trash_subscription' ] ) && $response->errors[ 'trash_subscription' ] == 'trash_subscription' ) {
				add_action( 'admin_notices', array( $this, 'trash_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'no_subscription' ] ) && $response->errors[ 'no_subscription' ] == 'no_subscription' ) {
				add_action( 'admin_notices', array( $this, 'no_subscription_error_notice' ) );
			} else if ( isset( $response->errors[ 'no_activation' ] ) && $response->errors[ 'no_activation' ] == 'no_activation' ) {
				add_action( 'admin_notices', array( $this, 'no_activation_error_notice' ) );
			} else if ( isset( $response->errors[ 'no_key' ] ) && $response->errors[ 'no_key' ] == 'no_key' ) {
				add_action( 'admin_notices', array( $this, 'no_key_error_notice' ) );
			} else if ( isset( $response->errors[ 'download_revoked' ] ) && $response->errors[ 'download_revoked' ] == 'download_revoked' ) {
				add_action( 'admin_notices', array( $this, 'download_revoked_error_notice' ) );
			} else if ( isset( $response->errors[ 'switched_subscription' ] ) && $response->errors[ 'switched_subscription' ] == 'switched_subscription' ) {
				add_action( 'admin_notices', array( $this, 'switched_subscription_error_notice' ) );
			}
		}
	}

	/**
	 * Generic request helper.
	 *
	 * @since  1.0.0
	 *
	 * @param  array $args
	 *
	 * @return object $response or boolean false
	 */
	public function request( $false, $action, $args ) {
		// Is this a plugin or a theme?
		//if ( $this->plugin_or_theme == 'plugin' ) {
		//	$version = get_site_transient( 'update_plugins' );
		//} else if ( $this->plugin_or_theme == 'theme' ) {
		//
		//	$version = get_site_transient( 'update_themes' );
		//}

		// Check if this plugins API is about this plugin
		if ( isset( $args->slug ) ) {
			if ( $args->slug != $this->slug ) {
				return $false;
			}
		} else {
			return $false;
		}

		$args = array(
			'request'          => 'plugininformation',
			'plugin_name'      => $this->plugin_name,
			//'version'			=>	$version->checked[$this->plugin_name],
			'version'          => $this->ame_software_version,
			'product_id'       => $this->ame_product_id,
			'api_key'          => $this->ame_options[ $this->ame_api_key ],
			'activation_email' => $this->ame_options[ $this->ame_activation_email ],
			'instance'         => $this->ame_instance_id,
			'domain'           => $this->ame_domain,
			'software_version' => $this->ame_software_version,
			'extra'            => $this->extra,
		);

		$response = $this->plugin_information( $args );

		// If everything is okay return the $response
		if ( isset( $response ) && is_object( $response ) && $response !== false ) {
			return $response;
		}
	}

	/**
	 * Display license expired error notice
	 */
	public function expired_license_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The license key for %s has expired. You can reactivate or purchase a license key from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription on-hold error notice
	 */
	public function on_hold_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s is on-hold. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription cancelled error notice
	 */
	public function cancelled_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s has been cancelled. You can renew the subscription from your account <a href="%s" target="_blank">dashboard</a>. A new license key will be emailed to you after your order has been completed.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription expired error notice
	 */
	public function expired_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s has expired. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription expired error notice
	 */
	public function suspended_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s has been suspended. You can reactivate the subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription expired error notice
	 */
	public function pending_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s is still pending. You can check on the status of the subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription expired error notice
	 */
	public function trash_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'The subscription for %s has been placed in the trash and will be deleted soon. You can purchase a new subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display subscription expired error notice
	 */
	public function no_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'A subscription for %s could not be found. You can purchase a subscription from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display missing key error notice
	 */
	public function no_key_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'A license key for %s could not be found. Maybe you forgot to enter a license key when setting up %s, or the key was deactivated in your account. You can reactivate or purchase a license key from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display missing download permission revoked error notice
	 */
	public function download_revoked_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'Download permission for %s has been revoked possibly due to a license key or subscription expiring. You can reactivate or purchase a license key from your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Display no activation error notice
	 */
	public function no_activation_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( '%s has not been activated. Go to the settings page and enter the license key and license email to activate %s.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_software_product_id );
	}

	/**
	 * Display switched activation error notice
	 */
	public function switched_subscription_error_notice() {
		echo sprintf( '<div id="message" class="error"><p>' . __( 'You changed the subscription for %s, so you will need to enter your new API License Key in the settings page. The License Key should have arrived in your email inbox, if not you can get it by logging into your account <a href="%s" target="_blank">dashboard</a>.', $this->text_domain ) . '</p></div>', $this->ame_software_product_id, $this->ame_renew_license_url );
	}

	/**
	 * Added the Fields for licensing this after Plugin-Row.
	 *
	 * @since	0.1
	 * @uses	is_network_admin, current_user_can, network_admin_url, get_site_option
	 * @return	bool
	 */
	public function license_row( $plugin_file, $plugin_data ) {

		// Security Check
		if ( function_exists( 'is_network_admin' ) && is_network_admin() ) {
			if ( ! current_user_can( 'manage_network_plugins' ) ) {
				return false;
			}
		} else if ( function_exists( 'current_user_can' ) && ! current_user_can( 'activate_plugins' ) ) {
			return false;
		}

		$file= 'admin.php?page=' . $this->ame_activation_tab_key;
		$settings_page = is_multisite() ? network_admin_url( $file ) : admin_url( $file );

		?>
		<tr class="active" id="<?php echo $this->text_domain; ?>-license">
			<td></td>
			<td scope="row" colspan="3" class="error<?php echo $this->text_domain; ?>-license-status">
				<?php printf( __( 'The <strong>%s</strong> API Key has not been activated, so automatic updates have been disabled until you enter a valid key. %sClick here to activate.%s', $this->text_domain ), esc_attr( $this->software_title ), '<a href="' . esc_url( $settings_page ) . '">', '</a>' ); ?>
			</td>
		</tr>
		<?php
	}
}