<?php
/**
 * Plugin Name:       Elementor Bypasser
 * Plugin URI:        #
 * Description:       Bypass elementor credentials.
 * Version:           0.0.1
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            Ahamed Arshad
 * Author URI:        #
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */


/**
 * Update license url
 * 
 * @param array  $args    HTTP request arguments.
 * @param string $url     The request URL.
 * 
 * @return array
 */
function bypass_elementor_license( $args, $url ) {

	if ( strpos( $url, 'https://my.elementor.com/api' ) === 0 ) {
		$args['body']['url'] = get_option( 'ebp_license_url' );
		$args['user-agent'] = str_replace( site_url(), get_option( 'ebp_license_url' ), $args['user-agent'] );
	}

	if ( strpos( $url, 'https://plugin-downloads.elementor.com/v2/package_download' ) === 0 ) {
		$args['user-agent'] = str_replace( site_url(), get_option( 'ebp_license_url' ), $args['user-agent'] );
	}

	return $args;
}
add_filter( 'http_request_args', 'bypass_elementor_license', 99, 4 );


/**
 * Register settings fields
 * 
 */
function bypass_elementor_register_settings() {
	register_setting( 'bypass-elementor-settings', 'ebp_license_key' );
	register_setting( 'bypass-elementor-settings', 'ebp_license_url' );
}
add_action( 'admin_init', 'bypass_elementor_register_settings' );


/**
 * Add settings fields to elementor license page
 * 
 */
function bypass_elementor_settings_page() {
	?>
	<div class="wrap">
		<h2>Bypass Elementor</h2>
		<form method="post" action="options.php">
			<?php settings_fields( 'bypass-elementor-settings' ); ?>
			<?php do_settings_sections( 'bypass-elementor-settings' ); ?>
			<table class="form-table">
				<tbody>
					<tr>
						<th scope="row">
							<label for="ebp_license_key">License Key</label>
						</th>
						<td>
							<input name="ebp_license_key" type="text" id="ebp_license_key" value="<?php echo esc_attr( get_option( 'ebp_license_key' ) ) ?>" class="regular-text">
							<p class="description">Enter your license key.</p>
						</td>
					</tr>
					<tr>
						<th scope="row">
							<label for="ebp_license_url">License URL</label>
						</th>
						<td>
							<input name="ebp_license_url" type="text" id="ebp_license_url" value="<?php echo esc_attr( get_option( 'ebp_license_url' ) ) ?>" class="regular-text">
							<p class="description">Enter the URL associated with your license key.</p>
						</td>
					</tr>
				</tbody>
			</table>
			<?php submit_button(); ?>
		</form>
	<?php
}
add_action( 'elementor_page_elementor-license', 'bypass_elementor_settings_page', 11 );


/**
 * Update license key and set license data on bypass data update
 * 
 * @param string $old_value Old value.
 * @param string $value     New value.
 */
function bypass_elementor_license_key_update( $old_value, $value ) {
	if ( ! empty( $value ) ) {

		$data = \ElementorPro\License\API::activate_license( $value );

		if ( ! is_wp_error( ! $data ) && ! empty( $data['success'] ) ) {
			\ElementorPro\License\Admin::set_license_key( $value );
			\ElementorPro\License\API::set_license_data( $data );
		}
	}
}
add_action( 'update_option_ebp_license_key', 'bypass_elementor_license_key_update', 10, 2 );