Commit bfa1325c authored by alain's avatar alain 🐙
Browse files

remove updraft

parent 4ab81fac
<?php
if (!defined('ABSPATH')) die('No direct access.');
/**
* Class UDP_Checkout_Embed
*
* Create links to embed an external checkout page
*/
if (!class_exists('Updraft_Checkout_Embed')) {
class Updraft_Checkout_Embed {
/**
* Class version
*
* @var string
*/
private static $version = '1.0.1';
/**
* Products list
*
* @var array
*/
public $products = array();
/**
* Construct
*
* @param string $plugin_name Current plugin using the class
* @param string $return_url The return URL after purchase is complete / canceled. Specially useful with paypal, that forces a redirect.
* @param array $products_list The list of products. Array or object that can be converted to an array
* @param string $base_url The plugin url, to where 'checkout-embed' is located. Used to enqueue scripts and styles.
* @param array $load_in_pages Pages in which the scripts are included. Use to limit the inclusion if necessary. See $this->enqueue_scripts
*/
public function __construct($plugin_name, $return_url, $products_list, $base_url, $load_in_pages = null) {
$this->plugin_name = sanitize_key($plugin_name);
$this->return_url = $return_url;
$this->products_list = $products_list;
$this->load_in_pages = $load_in_pages;
$this->base_url = $base_url;
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('admin_footer', array($this, 'print_template'));
}
/**
* Get the product using its slug
*
* @param string $product_slug
* @param string $return_url
* @return string|bool
*/
public function get_product($product_slug, $return_url = '') {
$products = $this->get_products();
if (empty($products)) return false;
if (is_object($products)) $products = get_object_vars($products);
if (is_array($products) && array_key_exists($product_slug, $products)) {
if (!$return_url) $return_url = $this->return_url;
$return_url = add_query_arg($this->plugin_name.'_product', $product_slug, $return_url);
return apply_filters(
$this->plugin_name.'_return_url',
add_query_arg(
array(
$this->plugin_name.'_return_url' => urlencode($return_url),
'checkout_embed_product_slug' => $product_slug
),
$products[$product_slug]
),
$product_slug
);
}
return false;
}
/**
* Get the products on the remote url
* Can return an object, if the products list given to the class is. (eg. json_decode gives an object if not specified otherwise)
*
* @return array|object
*/
private function get_products() {
return apply_filters($this->plugin_name.'_checkout_embed_get_products', $this->products_list ? $this->products_list : array());
}
/**
* Enqueue the required scripts / styles
*
* @param string $hook
*/
public function enqueue_scripts($hook) {
if (is_array($this->load_in_pages)) {
if (!in_array($hook, $this->load_in_pages)) {
return;
}
}
wp_enqueue_script($this->plugin_name.'-checkout-embed', trailingslashit($this->base_url).'checkout-embed/assets/udp-checkout-embed.js', array('jquery'), self::$version, true);
wp_enqueue_style($this->plugin_name.'-checkout-embed', trailingslashit($this->base_url).'checkout-embed/assets/udp-checkout-embed.css', null, self::$version);
}
/**
* Print the template for the modal
*/
public function print_template() {
if (is_array($this->load_in_pages)) {
$screen = get_current_screen();
if (!in_array($screen->base, $this->load_in_pages)) {
return;
}
}
?>
<div style="display: none;" id="udp-modal-template">
<div class="udp-modal">
<div class="udp-modal__overlay"></div>
<div class="udp-modal__modal">
</div>
</div>
</div>
<?php }
}
}
{"updraftpremium":"https:\/\/updraftplus.com\/shop\/updraftplus-premium\/?udp_checkout_embed=1&product_id=230&utm_source=updraftplus.com&utm_medium=in-plugin-purchase&utm_campaign=startup&utm_content=updraftpremium","updraftplus-vault-storage-5-gb":"https:\/\/updraftplus.com\/shop\/updraftplus-vault-storage-5-gb\/?udp_checkout_embed=1&product_id=123680&utm_source=updraftplus.com&utm_medium=in-plugin-purchase&utm_campaign=startup&utm_content=updraftplus-vault-storage-5-gb","updraftplus-vault-storage-15-gb":"https:\/\/updraftplus.com\/shop\/updraftplus-vault-storage-15-gb\/?udp_checkout_embed=1&product_id=123688&utm_source=updraftplus.com&utm_medium=in-plugin-purchase&utm_campaign=startup&utm_content=updraftplus-vault-storage-15-gb","updraftplus-vault-storage-50-gb":"https:\/\/updraftplus.com\/shop\/updraftplus-vault-storage-50-gb\/?udp_checkout_embed=1&product_id=123692&utm_source=updraftplus.com&utm_medium=in-plugin-purchase&utm_campaign=startup&utm_content=updraftplus-vault-storage-50-gb","updraftplus-vault-storage-250-gb":"https:\/\/updraftplus.com\/shop\/updraftplus-vault-storage-250-gb\/?udp_checkout_embed=1&product_id=383555&utm_source=updraftplus.com&utm_medium=in-plugin-purchase&utm_campaign=startup&utm_content=updraftplus-vault-storage-250-gb"}
\ No newline at end of file
# Embed the plugin's checkout page
## To use in a new plugin:
- Include and instanciate `Updraft_Checkout_Embed`
```php
if (!class_exists('Updraft_Checkout_Embed')) include_once (UPDRAFTPLUS_DIR.'/includes/checkout-embed/class-udp-checkout-embed.php');
global $udp_checkout_embed;
$udp_checkout_embed = new Updraft_Checkout_Embed(
'updraftplus'
$data_url,
$load_in_pages
);
```
### Params:
- $plugin_name: (string) Current plugin using the class
- $proructs_data_url: (string) url of the merchand website (eg: https://https://updraftplus.com)
- $load_in_pages: (array) pages on which the script + css will be loaded
### Cache:
The products data is cached and expires after 7 days. To force fetching it, add `udp-force-product-list-refresh=1` to the admin page url
## Using in the admin
- Once the php is setup, you can configure the links / buttons in the admin.
Add `data-embed-checkout="{$url}"` to any link. eg:
```php
global $updraftplus_checkout_embed;
$link_data_attr = $updraftplus_checkout_embed->get_product('updraftpremium') ? 'data-embed-checkout="'.apply_filters('updraftplus_com_link', $updraftplus_checkout_embed->get_product('updraftpremium')).'"' : '';
<a target="_blank" title="Upgrade to Updraft Premium" href="<?php echo apply_filters('updraftplus_com_link', "https://updraftplus.com/shop/updraftplus-premium/");?>" <?php echo $link_data_attr; ?>><?php _e('get it here', 'updraftplus');?></a>
```
- On completion (when the order is complete), the event 'udp/checkout/done' is triggered.
- The event 'udp/checkout/close' is triggered when the user closes the modal, regardless of success.
Use this to do something with the data received:
```javascript
$(document).on('udp/checkout/done', function(event, data, $element) {
// ... do something with data, currently data.email and data.order_number
// $element clicked to open the modal.
});
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment