forked from renventura/edd-add-to-cart-urls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-add-to-cart-urls.php
198 lines (157 loc) · 6.34 KB
/
edd-add-to-cart-urls.php
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
<?php
/**
* Plugin Name: EDD Add-to-Cart URLs
* Plugin URI: https://renventura.com
* Description: Adds the appropriate add-to-cart URL for your downloads so they can be easily copied and pasted whenever you need them.
* Version: 1.0.0
* Author: Ren Ventura
* Author URI: https://renventura.com
* Text Domain: edd-a2curl
* Domain Path: /languages/
*
* License: GPL 2.0+
* License URI: http://www.opensource.org/licenses/gpl-license.php
*/
/*
Copyright 2018 Ren Ventura <renventura.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'EDD_A2CURL' ) ) :
class EDD_A2CURL {
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EDD_A2CURL ) ) {
self::$instance = new EDD_A2CURL;
self::$instance->hooks();
}
return self::$instance;
}
/**
* Action/filter hooks
*/
public function hooks() {
register_activation_hook( __FILE__, array( $this, 'activate' ) );
add_action( 'plugins_loaded', array( $this, 'loaded' ) );
add_action( 'edd_meta_box_price_fields', array( $this, 'single_price_url' ), 99 );
add_action( 'edd_download_price_table_row', array( $this, 'variable_price_url' ), 10, 3 );
}
/**
* Plugin activation
*/
public function activate() {
if ( ! self::is_edd_active() ) {
deactivate_plugins( __FILE__ );
wp_die( __( 'EDD Add-to-Cart URLs requires Easy Digital Downloads to be active.', 'edd-a2curl' ) );
}
}
/**
* Load plugin text domain
*/
public function loaded() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'edd-a2curl' );
unload_textdomain( 'edd-a2curl' );
load_textdomain( 'edd-a2curl', WP_LANG_DIR . '/edd-add-to-cart-urls/edd-add-to-cart-urls-' . $locale . '.mo' );
load_plugin_textdomain( 'edd-a2curl', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Add the URL for Downloads (no variable prices)
*/
public function single_price_url( $download_id ) {
$download = new EDD_Download( $download_id );
// Bail if dealing with variably priced Downloads (handled below)
if ( $download->has_variable_prices() ) {
return;
}
// Assemble query args
$args = apply_filters( 'edd_a2curl_simple_price_query_args', array(
'edd_action' => 'add_to_cart',
'download_id' => $download_id
) );
// Assemble full URL
$url = apply_filters(
'edd_a2curl_simple_price_redirect_url',
add_query_arg( $args, self::get_redirect_uri( $download_id ) ),
$download_id
);
?>
<div class="edd-a2c-url edd-custom-price-option-section">
<span class="edd-custom-price-option-section-title" style="display: inline-block;"><?php _e( 'Add-to-Cart URL', 'edd-a2curl' ); ?></span>
<span alt="f223" class="edd-help-tip dashicons dashicons-editor-help" title="<?php _e( 'Use this link to directly add a download to the user\'s cart. When clicked, the download will be added to the cart, and the user will be redirected.', 'edd-a2curl' ); ?>"></span>
<input type="text" id="<?php echo "edd_a2curl_{$download_id}"; ?>" class="large-text" value="<?php esc_attr_e( $url ); ?>" readonly>
</div>
<?php }
/**
* Add the URL for individual price options when variable pricing is enabled on the download
*/
function variable_price_url( $download_id, $key, $args ) {
// Assemble query args
$args = apply_filters( 'edd_a2curl_variable_price_query_args', array(
'edd_action' => 'add_to_cart',
'download_id' => $download_id,
'edd_options' => array(
'price_id' => $key
)
) );
// Assemble full URL
$url = apply_filters(
'edd_a2curl_variable_price_redirect_url',
add_query_arg( $args, self::get_redirect_uri( $download_id ) ),
$download_id, $key
);
?>
<div class="edd-a2c-url edd-custom-price-option-section">
<span class="edd-custom-price-option-section-title" style="display: inline-block;"><?php _e( 'Add-to-Cart URL', 'edd-a2curl' ); ?></span>
<span alt="f223" class="edd-help-tip dashicons dashicons-editor-help" title="<?php _e( 'Use this link to directly add this price option to the user\'s cart. When clicked, the download will be added to the cart, and the user will be redirected.', 'edd-a2curl' ); ?>"></span>
<input type="text" id="<?php echo "edd_a2curl_{$download_id}_{$key}"; ?>" class="large-text" value="<?php esc_attr_e( $url ); ?>" readonly>
</div>
<?php }
/**
* Gets the base Redirect URI
* Default: EDD checkout page
* @param (int) $download_id - ID of Download
* @return (string) - URI for the redirect
*/
public static function get_redirect_uri( $download_id = null ) {
$download_id = ( get_post_type( $download_id ) === 'download' ) ? $download_id : null;
return apply_filters( 'edd_a2curl_redirect_uri', edd_get_checkout_uri(), $download_id );
}
/**
* Check if EDD is active
*/
public static function is_edd_active() {
return is_plugin_active( 'easy-digital-downloads/easy-digital-downloads.php' );
}
}
endif;
/**
* Main function
* @return object EDD_A2CURL instance
*/
function EDD_A2CURL() {
return EDD_A2CURL::instance();
}
/**
* Kick off!
*/
EDD_A2CURL();