| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/*---------------------------------------------------------*/ |
| 5 |
/* General Buy Buttons Functions */ |
| 6 |
/*---------------------------------------------------------*/ |
| 7 |
|
| 8 |
function mbt_buybuttons_init() { |
| 9 |
add_filter('mbt_stores', 'mbt_add_basic_stores'); |
| 10 |
} |
| 11 |
add_action('mbt_init', 'mbt_buybuttons_init'); |
| 12 |
|
| 13 |
function mbt_get_stores() { |
| 14 |
return apply_filters("mbt_stores", array()); |
| 15 |
} |
| 16 |
|
| 17 |
function mbt_add_basic_stores($stores) { |
| 18 |
if(mbt_get_setting('enable_default_affiliates') or mbt_get_upgrade()) { |
| 19 |
$stores['audible'] = array('name' => 'Audible.com', 'search' => 'https://www.audible.com/search'); |
| 20 |
$stores['bnn'] = array('name' => 'Barnes and Noble', 'search' => 'https://www.barnesandnoble.com/s/${title}'); |
| 21 |
$stores['nook'] = array('name' => 'Barnes and Noble Nook', 'search' => 'https://www.barnesandnoble.com/s/${title}'); |
| 22 |
$stores['kobo'] = array('name' => 'Kobo', 'search' => 'https://www.kobo.com/us/en/search?Query=${title}'); |
| 23 |
} |
| 24 |
$stores['amazon'] = array('name' => 'Amazon', 'search' => 'https://www.amazon.com/s?field-keywords=${title}'); |
| 25 |
$stores['kindle'] = array('name' => 'Amazon Kindle', 'search' => 'https://www.amazon.com/s?field-keywords=${title}'); |
| 26 |
$stores['goodreads'] = array('name' => 'GoodReads', 'search' => 'https://www.goodreads.com/search?q=${title}'); |
| 27 |
$stores['cbd'] = array('name' => 'Christian Book Distributor', 'search' => 'https://www.christianbook.com/Christian/Books/easy_find?Ntt=${title}'); |
| 28 |
$stores['bam'] = array('name' => 'Books-A-Million', 'search' => 'https://www.booksamillion.com/search?query=${title}'); |
| 29 |
$stores['bookbaby'] = array('name' => 'BookBaby'); |
| 30 |
$stores['lifeway'] = array('name' => 'Lifeway', 'search' => 'https://www.lifeway.com/Keyword/${title}'); |
| 31 |
$stores['mardel'] = array('name' => 'Mardel', 'search' => 'https://www.mardel.com/search/?text=${title}'); |
| 32 |
$stores['smashwords'] = array('name' => 'Smashwords', 'search' => 'https://www.smashwords.com/books/search?query=${title}'); |
| 33 |
$stores['indiebound'] = array('name' => 'IndieBound', 'search' => 'https://www.indiebound.org/search/book?keys=${title}'); |
| 34 |
$stores['createspace'] = array('name' => 'CreateSpace'); |
| 35 |
$stores['alibris'] = array('name' => 'Alibris', 'search' => 'https://www.alibris.com/booksearch?keyword=${title}'); |
| 36 |
$stores['bookdepository'] = array('name' => 'Book Depository', 'search' => 'https://www.bookdepository.com/search?searchTerm=${title}'); |
| 37 |
$stores['itunes'] = array('name' => 'Apple iTunes'); |
| 38 |
$stores['ibooks'] = array('name' => 'Apple Books'); |
| 39 |
$stores['powells'] = array('name' => 'Powells', 'search' => 'https://www.powells.com/SearchResults?kw=title:${title}'); |
| 40 |
$stores['scribd'] = array('name' => 'Scribd', 'search' => 'https://www.scribd.com/search?query=${title}'); |
| 41 |
$stores['sony'] = array('name' => 'Sony Reader'); |
| 42 |
$stores['googleplay'] = array('name' => 'Google Play', 'search' => 'https://play.google.com/store/search?q=${title}'); |
| 43 |
$stores['lulu'] = array('name' => 'Lulu', 'search' => 'https://www.lulu.com/shop/search.ep?keyWords=${title}'); |
| 44 |
$stores['gumroad'] = array('name' => 'Gumroad'); |
| 45 |
$stores['celery'] = array('name' => 'Celery'); |
| 46 |
return $stores; |
| 47 |
} |
| 48 |
|
| 49 |
function mbt_buybutton_editor($data, $id, $store) { |
| 50 |
$output = '<input id="'.$id.'_name" name="'.$id.'[store]" type="hidden" value="'.esc_attr(isset($data['store']) ? $data['store'] : '').'">'; |
| 51 |
$output .= '<textarea id="'.$id.'_url" name="'.$id.'[url]" cols="80">'.esc_textarea(isset($data['url']) ? $data['url'] : '').'</textarea>'; |
| 52 |
$editor_desc = (empty($store['editor_desc']) ? __('Paste in the product URL for this item.', 'mybooktable').' <a href="'.admin_url('admin.php?page=mbt_help&mbt_video_tutorial=buy_buttons').'" target="_blank">'.__('Learn more about adding Buy Button links.', 'mybooktable').'</a>' : $store['editor_desc']); |
| 53 |
|
| 54 |
/* translators: %s: the name of the book source (store) */ |
| 55 |
$editor_search = (empty($store['search']) ? '' : ' <a class="mbt_buybutton_editor_search" href="#" data-href="'.$store['search'].'" target="_blank">'.sprintf(__('Search for books on %s.', 'mybooktable'), $store['name']).'</a>'); |
| 56 |
$output .= '<p>'.$editor_desc.$editor_search.'</p>'; |
| 57 |
return apply_filters('mbt_buybutton_editor', $output, $data, $id, $store); |
| 58 |
} |
| 59 |
|
| 60 |
function mbt_format_buybutton($data, $store) { |
| 61 |
$data = apply_filters('mbt_filter_buybutton_data', $data, $store); |
| 62 |
if(!empty($data['display']) and $data['display'] == 'text') { |
| 63 |
/* translators: %s: the name of the book source (store) */ |
| 64 |
$output = empty($data['url']) ? '' : '<li><a href="'.htmlspecialchars($data['url']).'" target="_blank" rel="nofollow">'.sprintf(__('Buy from %s', 'mybooktable'), $store['name']).'</a></li>'; |
| 65 |
} else { |
| 66 |
/* translators: %s: the name of the book source (store) */ |
| 67 |
$output = empty($data['url']) ? '' : '<div class="mbt-book-buybutton"><a href="'.htmlspecialchars($data['url']).'" target="_blank" rel="nofollow"><img src="'.mbt_image_url($data['store'].'_button.png').'" border="0" alt="'.sprintf(__('Buy from %s', 'mybooktable'), $store['name']).'"/></a></div>'; |
| 68 |
} |
| 69 |
return apply_filters('mbt_format_buybutton', $output, $data, $store); |
| 70 |
} |
| 71 |
|
| 72 |
function mbt_query_buybuttons($post_id, $query = '') { |
| 73 |
$buybuttons = get_post_meta($post_id, "mbt_buybuttons", true); |
| 74 |
$stores = mbt_get_stores(); |
| 75 |
if(!empty($buybuttons)) { |
| 76 |
foreach($buybuttons as $i=>$buybutton) { |
| 77 |
if(!isset($stores[$buybutton['store']])) { unset($buybuttons[$i]); continue; } |
| 78 |
if(!empty($query) and is_array($query)) { |
| 79 |
foreach($query as $key=>$value) { |
| 80 |
if(!empty($buybutton[$key]) and !((is_array($value) and in_array($buybutton[$key], $value)) or $buybutton[$key] == $value)) { unset($buybuttons[$i]); continue; } |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
$buybuttons = array_values($buybuttons); |
| 85 |
} |
| 86 |
return apply_filters('mbt_query_buybuttons', empty($buybuttons) ? array() : $buybuttons, $query); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
/*---------------------------------------------------------*/ |
| 92 |
/* iTunes Affiliate Settings */ |
| 93 |
/*---------------------------------------------------------*/ |
| 94 |
|
| 95 |
function mbt_itunes_affiliate_settings_init() { |
| 96 |
add_action('mbt_affiliate_settings_render', 'mbt_itunes_affiliate_settings_render'); |
| 97 |
add_action('mbt_settings_save', 'mbt_itunes_affiliate_settings_save'); |
| 98 |
add_action('wp_ajax_mbt_itunes_affiliate_token_refresh', 'mbt_itunes_affiliate_token_refresh_ajax'); |
| 99 |
} |
| 100 |
add_action('mbt_init', 'mbt_itunes_affiliate_settings_init'); |
| 101 |
|
| 102 |
function mbt_itunes_affiliate_token_refresh_ajax() { |
| 103 |
//$nonce = wp_unslash($_REQUEST['mbt_nonce']); |
| 104 |
if(isset($_REQUEST['mbt_nonce']) && isset($_REQUEST['data']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce')){ |
| 105 |
if(!current_user_can('manage_options')) { die(); } |
| 106 |
mbt_update_setting('itunes_affiliate_token', sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 107 |
echo(wp_kses_post(mbt_itunes_affiliate_token_feedback())); |
| 108 |
} |
| 109 |
die(); |
| 110 |
} |
| 111 |
|
| 112 |
function mbt_itunes_affiliate_token_feedback() { |
| 113 |
$output = ''; |
| 114 |
$affiliate_token = mbt_get_setting("itunes_affiliate_token"); |
| 115 |
if(!empty($affiliate_token)) { |
| 116 |
if(preg_match('/^[0-9A-Za-z]+$/', $affiliate_token)) { |
| 117 |
$output .= '<span class="mbt_admin_message_success">'.__('Valid Affiliate Token Format', 'mybooktable').'</span>'; |
| 118 |
} else { |
| 119 |
$output .= '<span class="mbt_admin_message_failure">'.__('Invalid Affiliate Token Format', 'mybooktable').'</span>'; |
| 120 |
} |
| 121 |
} |
| 122 |
return $output; |
| 123 |
} |
| 124 |
|
| 125 |
function mbt_itunes_affiliate_settings_save() { |
| 126 |
if( (isset($_REQUEST['page']) && $_REQUEST['page'] === 'mbt_settings') && ((isset($_REQUEST['data-settings-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-settings-nonce']),'mbt-settings-nonce'))) && isset($_REQUEST['save_settings'])) { |
| 127 |
if(isset($_REQUEST['mbt_itunes_affiliate_token'])) { |
| 128 |
mbt_update_setting('itunes_affiliate_token', sanitize_text_field(wp_unslash($_REQUEST['mbt_itunes_affiliate_token']))); |
| 129 |
mbt_update_setting('disable_itunes_affiliates', isset($_REQUEST['mbt_disable_itunes_affiliates'])); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
function mbt_itunes_affiliate_settings_render($mbt_nonce) { |
| 135 |
?> |
| 136 |
<table class="form-table"> |
| 137 |
<tbody> |
| 138 |
<tr> |
| 139 |
<th> |
| 140 |
<label for="mbt_itunes_affiliate_token"><?php esc_attr_e('iTunes Affiliates', 'mybooktable'); ?></label> |
| 141 |
<div class="mbt-affiliate-usedby"> |
| 142 |
Used by: |
| 143 |
<ul> |
| 144 |
<li>Apple iBooks Buy Button</li> |
| 145 |
<li>Apple iTunes Buy Button</li> |
| 146 |
</ul> |
| 147 |
</div> |
| 148 |
</th> |
| 149 |
<td> |
| 150 |
<div class="mbt_feedback_above mbt_feedback"><?php echo(wp_kses_post(mbt_itunes_affiliate_token_feedback())); ?></div> |
| 151 |
<label for="mbt_itunes_affiliate_token"><?php esc_attr_e('Affiliate Token:', 'mybooktable'); ?></label> |
| 152 |
<input type="text" name="mbt_itunes_affiliate_token" id="mbt_itunes_affiliate_token" value="<?php echo(esc_attr(mbt_get_setting('itunes_affiliate_token'))); ?>" class="regular-text"> |
| 153 |
<div class="mbt_feedback_refresh" data-refresh-action="mbt_itunes_affiliate_token_refresh" data-element="mbt_itunes_affiliate_token" data-refresh-nonce="<?php echo(esc_attr($mbt_nonce)); ?>"></div> |
| 154 |
<p class="description"><?php esc_attr_e('You can find your iTunes Affiliate Token by visiting your iTunes Affiliates page. The code should be near the top left of the screen.' , 'mybooktable'); ?></p> |
| 155 |
<p class="description"><input type="checkbox" name="mbt_disable_itunes_affiliates" id="mbt_disable_itunes_affiliates" <?php checked(mbt_get_setting('disable_itunes_affiliates'), true); ?> > <?php esc_attr_e('Disable iTunes Affiliate System', 'mybooktable'); ?></p> |
| 156 |
</td> |
| 157 |
</tr> |
| 158 |
</tbody> |
| 159 |
</table> |
| 160 |
<?php |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
/*---------------------------------------------------------*/ |
| 166 |
/* Powells Affiliate Settings */ |
| 167 |
/*---------------------------------------------------------*/ |
| 168 |
|
| 169 |
function mbt_powells_affiliate_settings_init() { |
| 170 |
add_action('mbt_affiliate_settings_render', 'mbt_powells_affiliate_settings_render'); |
| 171 |
add_action('mbt_settings_save', 'mbt_powells_affiliate_settings_save'); |
| 172 |
add_action('wp_ajax_mbt_powells_partner_id_refresh', 'mbt_powells_partner_id_refresh_ajax'); |
| 173 |
add_action('mbt_filter_buybutton_data', 'mbt_filter_powells_buybuttons_data', 10, 2); |
| 174 |
} |
| 175 |
add_action('mbt_init', 'mbt_powells_affiliate_settings_init'); |
| 176 |
|
| 177 |
function mbt_powells_partner_id_refresh_ajax() { |
| 178 |
if(isset($_REQUEST['mbt_nonce']) && isset($_REQUEST['data']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce')){ |
| 179 |
if(!current_user_can('manage_options')) { die(); } |
| 180 |
mbt_update_setting('powells_partner_id', sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 181 |
echo(wp_kses_post(mbt_powells_partner_id_feedback())); |
| 182 |
} |
| 183 |
die(); |
| 184 |
} |
| 185 |
|
| 186 |
function mbt_powells_partner_id_feedback() { |
| 187 |
$output = ''; |
| 188 |
$partner_id = mbt_get_setting('powells_partner_id'); |
| 189 |
if(!empty($partner_id)) { |
| 190 |
if(preg_match('/^[0-9]+$/', $partner_id)) { |
| 191 |
$output .= '<span class="mbt_admin_message_success">'.__('Valid Partner ID Format', 'mybooktable').'</span>'; |
| 192 |
} else { |
| 193 |
$output .= '<span class="mbt_admin_message_failure">'.__('Invalid Partner ID Format', 'mybooktable').'</span>'; |
| 194 |
} |
| 195 |
} |
| 196 |
return $output; |
| 197 |
} |
| 198 |
|
| 199 |
function mbt_powells_affiliate_settings_save() { |
| 200 |
if( (isset($_REQUEST['page']) && $_REQUEST['page'] === 'mbt_settings') && ((isset($_REQUEST['data-settings-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-settings-nonce']),'mbt-settings-nonce'))) && isset($_REQUEST['save_settings'])) { |
| 201 |
if(isset($_REQUEST['mbt_powells_partner_id'])) { |
| 202 |
mbt_update_setting('powells_partner_id', sanitize_text_field(wp_unslash($_REQUEST['mbt_powells_partner_id']))); |
| 203 |
mbt_update_setting('disable_powells_affiliates', isset($_REQUEST['mbt_disable_powells_affiliates'])); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
function mbt_powells_affiliate_settings_render($mbt_nonce) { |
| 209 |
?> |
| 210 |
<table class="form-table"> |
| 211 |
<tbody> |
| 212 |
<tr> |
| 213 |
<th> |
| 214 |
<label for="mbt_powells_partner_id"><?php esc_attr_e("Powell's Affiliates", 'mybooktable'); ?></label> |
| 215 |
<div class="mbt-affiliate-usedby"> |
| 216 |
Used by: |
| 217 |
<ul> |
| 218 |
<li>Powell's Buy Button</li> |
| 219 |
</ul> |
| 220 |
</div> |
| 221 |
</th> |
| 222 |
<td> |
| 223 |
<div class="mbt_feedback_above mbt_feedback"><?php echo(wp_kses_post(mbt_powells_partner_id_feedback())); ?></div> |
| 224 |
<label for="mbt_powells_partner_id"><?php esc_attr_e('Partner ID:', 'mybooktable'); ?></label> |
| 225 |
<input type="text" name="mbt_powells_partner_id" id="mbt_powells_partner_id" value="<?php echo(esc_attr(mbt_get_setting('powells_partner_id'))); ?>" class="regular-text"> |
| 226 |
<div class="mbt_feedback_refresh" data-refresh-action="mbt_powells_partner_id_refresh" data-element="mbt_powells_partner_id" data-refresh-nonce="<?php echo(esc_attr($mbt_nonce)); ?>"></div> |
| 227 |
<p class="description"><?php esc_attr_e('You can find your Powell\'s Partner ID by visiting your Powell\'s Account page and clicking the "Manage Partner" link under "Partner Program".' , 'mybooktable'); ?></p> |
| 228 |
<p class="description"><input type="checkbox" name="mbt_disable_powells_affiliates" id="mbt_disable_powells_affiliates" <?php checked(mbt_get_setting('disable_powells_affiliates'), true); ?> > <?php esc_attr_e("Disable Powell's Affiliate System", 'mybooktable'); ?></p> |
| 229 |
</td> |
| 230 |
</tr> |
| 231 |
</tbody> |
| 232 |
</table> |
| 233 |
<?php |
| 234 |
} |
| 235 |
|
| 236 |
function mbt_filter_powells_buybuttons_data($data, $store) { |
| 237 |
if($data['store'] == 'powells' and !empty($data['url']) and !mbt_is_genius_link($data['url']) and mbt_get_setting('powells_partner_id') and !mbt_get_setting('disable_powells_affiliates')) { |
| 238 |
$partner_id = mbt_get_setting('powells_partner_id'); |
| 239 |
$url = wp_parse_url($data['url']); |
| 240 |
if(stripos($url['host'], 'powells.com') !== false) { |
| 241 |
$data['url'] = 'https://'.$url['host'].$url['path'].'?partnerid='.$partner_id; |
| 242 |
} else { |
| 243 |
$data['url'] = ''; |
| 244 |
} |
| 245 |
} |
| 246 |
return $data; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
/*---------------------------------------------------------*/ |
| 252 |
/* IndieBound Affiliate Settings */ |
| 253 |
/*---------------------------------------------------------*/ |
| 254 |
|
| 255 |
function mbt_indiebound_affiliate_settings_init() { |
| 256 |
add_action('mbt_affiliate_settings_render', 'mbt_indiebound_affiliate_settings_render'); |
| 257 |
add_action('mbt_settings_save', 'mbt_indiebound_affiliate_settings_save'); |
| 258 |
add_action('wp_ajax_mbt_indiebound_affiliate_id_refresh', 'mbt_indiebound_affiliate_id_refresh_ajax'); |
| 259 |
add_action('mbt_filter_buybutton_data', 'mbt_filter_indiebound_buybuttons_data', 10, 2); |
| 260 |
} |
| 261 |
add_action('mbt_init', 'mbt_indiebound_affiliate_settings_init'); |
| 262 |
|
| 263 |
function mbt_indiebound_affiliate_id_refresh_ajax() { |
| 264 |
if(isset($_REQUEST['mbt_nonce']) && isset($_REQUEST['data']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce')){ |
| 265 |
if(!current_user_can('manage_options')) { die(); } |
| 266 |
mbt_update_setting('indiebound_affiliate_id', sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 267 |
echo(wp_kses_post(mbt_indiebound_affiliate_id_feedback())); |
| 268 |
} |
| 269 |
die(); |
| 270 |
} |
| 271 |
|
| 272 |
function mbt_indiebound_affiliate_id_feedback() { |
| 273 |
$output = ''; |
| 274 |
$affiliate_id = mbt_get_setting('indiebound_affiliate_id'); |
| 275 |
if(!empty($affiliate_id)) { |
| 276 |
if(preg_match('/^\S+$/', $affiliate_id)) { |
| 277 |
$output .= '<span class="mbt_admin_message_success">'.__('Valid Affiliate ID Format', 'mybooktable').'</span>'; |
| 278 |
} else { |
| 279 |
$output .= '<span class="mbt_admin_message_failure">'.__('Invalid Affiliate ID Format', 'mybooktable').'</span>'; |
| 280 |
} |
| 281 |
} |
| 282 |
return $output; |
| 283 |
} |
| 284 |
|
| 285 |
function mbt_indiebound_affiliate_settings_save() { |
| 286 |
if( (isset($_REQUEST['page']) && $_REQUEST['page'] === 'mbt_settings') && ((isset($_REQUEST['data-settings-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-settings-nonce']),'mbt-settings-nonce'))) && isset($_REQUEST['save_settings'])) { |
| 287 |
if(isset($_REQUEST['mbt_indiebound_affiliate_id'])) { |
| 288 |
mbt_update_setting('indiebound_affiliate_id', sanitize_text_field(wp_unslash($_REQUEST['mbt_indiebound_affiliate_id']))); |
| 289 |
mbt_update_setting('disable_indiebound_affiliates', isset($_REQUEST['mbt_disable_indiebound_affiliates'])); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
function mbt_indiebound_affiliate_settings_render($mbt_nonce) { |
| 295 |
?> |
| 296 |
<table class="form-table"> |
| 297 |
<tbody> |
| 298 |
<tr> |
| 299 |
<th> |
| 300 |
<label for="mbt_indiebound_affiliate_id"><?php esc_attr_e("IndieBound Affiliates", 'mybooktable'); ?></label> |
| 301 |
<div class="mbt-affiliate-usedby"> |
| 302 |
Used by: |
| 303 |
<ul> |
| 304 |
<li>IndieBound Buy Button</li> |
| 305 |
</ul> |
| 306 |
</div> |
| 307 |
</th> |
| 308 |
<td> |
| 309 |
<div class="mbt_feedback_above mbt_feedback"><?php echo(wp_kses_post(mbt_indiebound_affiliate_id_feedback())); ?></div> |
| 310 |
<label for="mbt_indiebound_affiliate_id"><?php esc_attr_e('Affiliate ID:', 'mybooktable'); ?></label> |
| 311 |
<input type="text" name="mbt_indiebound_affiliate_id" id="mbt_indiebound_affiliate_id" value="<?php echo(esc_attr(mbt_get_setting('indiebound_affiliate_id'))); ?>" class="regular-text"> |
| 312 |
<div class="mbt_feedback_refresh" data-refresh-action="mbt_indiebound_affiliate_id_refresh" data-element="mbt_indiebound_affiliate_id" data-refresh-nonce="<?php echo(esc_attr($mbt_nonce)); ?>"></div> |
| 313 |
<p class="description"><?php esc_attr_e('You can find your IndieBound Affiliate ID by visiting your IndieBound Affiliates page and clicking "Create your affiliate links".' , 'mybooktable'); ?></p> |
| 314 |
<p class="description"><input type="checkbox" name="mbt_disable_indiebound_affiliates" id="mbt_disable_indiebound_affiliates" <?php checked(mbt_get_setting('disable_indiebound_affiliates'), true); ?> > <?php esc_attr_e("Disable IndieBound Affiliate System", 'mybooktable'); ?></p> |
| 315 |
</td> |
| 316 |
</tr> |
| 317 |
</tbody> |
| 318 |
</table> |
| 319 |
<?php |
| 320 |
} |
| 321 |
|
| 322 |
function mbt_filter_indiebound_buybuttons_data($data, $store) { |
| 323 |
if($data['store'] == 'indiebound' and !empty($data['url']) and !mbt_is_genius_link($data['url']) and mbt_get_setting('indiebound_affiliate_id') and !mbt_get_setting('disable_indiebound_affiliates')) { |
| 324 |
$affiliate_id = mbt_get_setting('indiebound_affiliate_id'); |
| 325 |
$url = wp_parse_url($data['url']); |
| 326 |
if(stripos($url['host'], 'indiebound.org') !== false) { |
| 327 |
$data['url'] = 'https://'.$url['host'].$url['path'].'?aff='.$affiliate_id; |
| 328 |
} else { |
| 329 |
$data['url'] = ''; |
| 330 |
} |
| 331 |
} |
| 332 |
return $data; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
/*---------------------------------------------------------*/ |
| 338 |
/* Amazon Affiliate Settings */ |
| 339 |
/*---------------------------------------------------------*/ |
| 340 |
|
| 341 |
function mbt_amazon_affiliate_settings_init() { |
| 342 |
add_filter('mbt_affiliate_settings_render', 'mbt_amazon_affiliate_settings_render'); |
| 343 |
add_action('mbt_settings_save', 'mbt_amazon_affiliate_settings_save',40); |
| 344 |
add_action('mbt_settings_save', 'mbt_use_amazon_attribution_links_save',50); |
| 345 |
add_action('wp_ajax_mbt_amazon_affiliate_code_refresh', 'mbt_amazon_affiliate_code_refresh_ajax'); |
| 346 |
add_action('wp_ajax_mbt_amazon_onelink_code_refresh', 'mbt_amazon_onelink_code_refresh_ajax'); |
| 347 |
add_action('wp_footer', 'mbt_amazon_onelink_code_insert'); |
| 348 |
} |
| 349 |
add_action('mbt_init', 'mbt_amazon_affiliate_settings_init'); |
| 350 |
|
| 351 |
function mbt_amazon_affiliate_code_refresh_ajax() { |
| 352 |
if(isset($_REQUEST['mbt_nonce']) && isset($_REQUEST['data']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce')){ |
| 353 |
if(!current_user_can('manage_options')) { die(); } |
| 354 |
mbt_update_setting('amazon_buybutton_affiliate_code', sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 355 |
echo(wp_kses_post(mbt_amazon_affiliate_code_feedback())); |
| 356 |
} |
| 357 |
die(); |
| 358 |
} |
| 359 |
|
| 360 |
function mbt_amazon_affiliate_code_feedback() { |
| 361 |
$output = ''; |
| 362 |
$amazon_affiliate_code = mbt_get_setting('amazon_buybutton_affiliate_code'); |
| 363 |
if(!empty($amazon_affiliate_code)) { |
| 364 |
if(preg_match('/^\S+$/', $amazon_affiliate_code)) { |
| 365 |
$output .= '<span class="mbt_admin_message_success">'.__('Valid Affiliate Code Format', 'mybooktable').'</span>'; |
| 366 |
} else { |
| 367 |
$output .= '<span class="mbt_admin_message_failure">'.__('Invalid Affiliate Code Format', 'mybooktable').'</span>'; |
| 368 |
} |
| 369 |
} |
| 370 |
return $output; |
| 371 |
} |
| 372 |
|
| 373 |
function mbt_amazon_onelink_code_refresh_ajax() { |
| 374 |
if(isset($_REQUEST['mbt_nonce']) && isset($_REQUEST['data']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce')){ |
| 375 |
if(!current_user_can('manage_options')) { die(); } |
| 376 |
mbt_update_setting('amazon_affiliates_onelink_code', sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 377 |
echo(wp_kses_post(mbt_amazon_onelink_code_feedback())); |
| 378 |
} |
| 379 |
die(); |
| 380 |
} |
| 381 |
|
| 382 |
function mbt_amazon_onelink_code_feedback() { |
| 383 |
$output = ''; |
| 384 |
$amazon_onelink_code = mbt_get_setting('amazon_affiliates_onelink_code'); |
| 385 |
if(!empty($amazon_onelink_code)) { |
| 386 |
if(preg_match('/^<[s]cript src="[^"]+"><\/script>$/', $amazon_onelink_code)) { |
| 387 |
$output .= '<span class="mbt_admin_message_success">'.__('Valid OneLink Script Code Format', 'mybooktable').'</span>'; |
| 388 |
} else { |
| 389 |
$output .= '<span class="mbt_admin_message_failure">'.__('Invalid OneLink Script Code Format', 'mybooktable').'</span>'; |
| 390 |
} |
| 391 |
} |
| 392 |
return $output; |
| 393 |
} |
| 394 |
|
| 395 |
function mbt_amazon_affiliate_settings_render($mbt_nonce) { |
| 396 |
?> |
| 397 |
<table class="form-table"> |
| 398 |
<tbody> |
| 399 |
<tr> |
| 400 |
<th> |
| 401 |
<label for="mbt_amazon_buybutton_affiliate_code"><?php esc_attr_e('Amazon Associates', 'mybooktable'); ?></label> |
| 402 |
<div class="mbt-affiliate-usedby"> |
| 403 |
Used by: |
| 404 |
<ul> |
| 405 |
<li>Amazon Buy Button</li> |
| 406 |
<li>Kindle Buy Button</li> |
| 407 |
<li>Audible.com Buy Button</li> |
| 408 |
</ul> |
| 409 |
</div> |
| 410 |
</th> |
| 411 |
<td> |
| 412 |
<div id="mbt-amazon-affiliate-settings-tabs"> |
| 413 |
<ul> |
| 414 |
<li><a href="#mbt-amazon-affiliate-code"><?php esc_attr_e('Affiliate Code', 'mybooktable'); ?></a></li> |
| 415 |
<li><a href="#mbt-amazon-onelink-code"><?php esc_attr_e('OneLink', 'mybooktable'); ?></a></li> |
| 416 |
</ul> |
| 417 |
<div class="mbt-tab" id="mbt-amazon-affiliate-code"> |
| 418 |
<div class="mbt_api_key_feedback mbt_feedback"><?php echo(wp_kses_post(mbt_amazon_affiliate_code_feedback())); ?></div> |
| 419 |
<label for="mbt_amazon_buybutton_affiliate_code"><?php esc_attr_e('Affiliate Code:', 'mybooktable'); ?></label> |
| 420 |
<input type="text" name="mbt_amazon_buybutton_affiliate_code" id="mbt_amazon_buybutton_affiliate_code" value="<?php echo(esc_attr(mbt_get_setting('amazon_buybutton_affiliate_code'))); ?>" class="regular-text"> |
| 421 |
<div class="mbt_feedback_refresh" data-refresh-action="mbt_amazon_affiliate_code_refresh" data-element="mbt_amazon_buybutton_affiliate_code" data-refresh-nonce="<?php echo(esc_attr($mbt_nonce)); ?>"></div> |
| 422 |
<p class="description"> |
| 423 |
<?php //sprintf(__('You can find your Amazon affiliate tracking ID by visiting your %sAmazon Affiliate Homepage%s. The code should be near the top left of the screen and will end in "-20" if you live in the United States of America. %sLearn how to sign up for Amazon Associates.%s', 'mybooktable'), '<a href="https://affiliate-program.amazon.com/gp/associates/network/main.html" target="_blank">', '</a>', '<a href="'.admin_url('admin.php?page=mbt_help&mbt_video_tutorial=amazon_affiliates').'" target="_blank">', '</a>'); ?> |
| 424 |
</p> |
| 425 |
</div> |
| 426 |
<div class="mbt-tab" id="mbt-amazon-onelink-code"> |
| 427 |
<div class="mbt_api_key_feedback mbt_feedback"><?php echo(wp_kses_post(mbt_amazon_onelink_code_feedback())); ?></div> |
| 428 |
<label for="mbt_amazon_affiliates_onelink_code"><?php esc_attr_e('OneLink Script Code:', 'mybooktable'); ?></label> |
| 429 |
<textarea rows="5" cols="60" type="text" name="mbt_amazon_affiliates_onelink_code" id="mbt_amazon_affiliates_onelink_code" style="vertical-align: top;"><?php echo(esc_attr(htmlspecialchars(mbt_get_setting('amazon_affiliates_onelink_code')) ?? "", ENT_QUOTES)); ?></textarea> |
| 430 |
<div class="mbt_feedback_refresh" data-refresh-action="mbt_amazon_onelink_code_refresh" data-element="mbt_amazon_affiliates_onelink_code" data-refresh-nonce="<?php echo(esc_attr($mbt_nonce)); ?>"></div> |
| 431 |
<p class="description"> |
| 432 |
<?php esc_attr_e('You can find your Amazon OneLink Script Code by visiting your Amazon Affiliate Homepage, under Tools > OneLink.', 'mybooktable'); ?> |
| 433 |
<?php //sprintf(__('You can find your Amazon OneLink Script Code by visiting your %sAmazon Affiliate Homepage%s, under Tools > OneLink. %sClick here to learn more about Amazon OneLink%s %sLearn how to sign up for Amazon Associates.%s', 'mybooktable'), '<a href="https://affiliate-program.amazon.com/gp/associates/network/main.html" target="_blank">', '</a>', 'https://affiliate-program.amazon.com/help/node/topic/202164400', '</a>', '<a href="'.admin_url('admin.php?page=mbt_help&mbt_video_tutorial=amazon_affiliates').'" target="_blank">', '</a>'); ?> |
| 434 |
</p> |
| 435 |
</div> |
| 436 |
</div> |
| 437 |
<div><p class="description"><input type="checkbox" name="mbt_disable_amazon_affiliates" id="mbt_disable_amazon_affiliates" <?php checked(mbt_get_setting('disable_amazon_affiliates'), true); ?> > <?php esc_attr_e('Disable Amazon Affiliate System', 'mybooktable'); ?></p></div> |
| 438 |
<div><p class="description"> |
| 439 |
|
| 440 |
<input type="checkbox" name="mbt_use_amazon_attribution_links" id="mbt_use_amazon_attribution_links" <?php checked(mbt_get_setting('use_amazon_attribution_links'), true); ?> > |
| 441 |
|
| 442 |
<?php esc_attr_e('Use Amazon Attribution Links instead of affiliate links', 'mybooktable'); ?></p></div> |
| 443 |
</td> |
| 444 |
</tr> |
| 445 |
</tbody> |
| 446 |
</table> |
| 447 |
<?php |
| 448 |
} |
| 449 |
|
| 450 |
function mbt_amazon_affiliate_settings_save() { |
| 451 |
if( (isset($_REQUEST['page']) && isset($_REQUEST['mbt_amazon_buybutton_affiliate_code']) && $_REQUEST['page'] === 'mbt_settings') && ((isset($_REQUEST['data-settings-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-settings-nonce']),'mbt-settings-nonce'))) && isset($_REQUEST['save_settings'])) { |
| 452 |
if(isset($_REQUEST['mbt_amazon_buybutton_affiliate_code'])) { |
| 453 |
mbt_update_setting('amazon_buybutton_affiliate_code', sanitize_text_field(wp_unslash($_REQUEST['mbt_amazon_buybutton_affiliate_code']))); |
| 454 |
if(isset($_REQUEST['mbt_amazon_affiliates_onelink_code'])){ |
| 455 |
mbt_update_setting('amazon_affiliates_onelink_code', sanitize_text_field(wp_unslash($_REQUEST['mbt_amazon_affiliates_onelink_code']))); |
| 456 |
} |
| 457 |
mbt_update_setting('disable_amazon_affiliates', isset($_REQUEST['mbt_disable_amazon_affiliates'])); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
function mbt_use_amazon_attribution_links_save() { |
| 463 |
if( (isset($_REQUEST['page']) && $_REQUEST['page'] === 'mbt_settings') && ((isset($_REQUEST['data-settings-nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['data-settings-nonce']),'mbt-settings-nonce'))) && isset($_REQUEST['save_settings'])) { |
| 464 |
mbt_update_setting('use_amazon_attribution_links', isset($_REQUEST['mbt_use_amazon_attribution_links'])); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
function mbt_amazon_onelink_code_insert() { |
| 469 |
$onelink_code = mbt_get_setting('amazon_affiliates_onelink_code'); |
| 470 |
if($onelink_code && !mbt_get_setting('disable_amazon_affiliates')) { |
| 471 |
echo(esc_attr($onelink_code)); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
/*---------------------------------------------------------*/ |
| 478 |
/* Linkshare Affiliate Settings */ |
| 479 |
/*---------------------------------------------------------*/ |
| 480 |
|
| 481 |
function mbt_linkshare_affiliate_settings_init() { |
| 482 |
add_action('mbt_affiliate_settings_render', 'mbt_linkshare_affiliate_settings_render'); |
| 483 |
} |
| 484 |
add_action('mbt_init', 'mbt_linkshare_affiliate_settings_init'); |
| 485 |
|
| 486 |
function mbt_linkshare_affiliate_settings_render() { |
| 487 |
?> |
| 488 |
<table class="form-table"> |
| 489 |
<tbody> |
| 490 |
<tr> |
| 491 |
<th> |
| 492 |
<label style="color: #666"><?php esc_attr_e('LinkShare', 'mybooktable'); ?></label> |
| 493 |
<div class="mbt-affiliate-usedby"> |
| 494 |
Used by: |
| 495 |
<ul> |
| 496 |
<li>Kobo Buy Button</li> |
| 497 |
</ul> |
| 498 |
</div> |
| 499 |
</th> |
| 500 |
<td> |
| 501 |
<input type="text" disabled="true" value="" class="regular-text"> |
| 502 |
<p class="description"><?php echo(wp_kses_post(mbt_get_upgrade_message())); ?></p> |
| 503 |
</td> |
| 504 |
</tr> |
| 505 |
</tbody> |
| 506 |
</table> |
| 507 |
<?php |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
/*---------------------------------------------------------*/ |
| 513 |
/* Commission Junction Affiliate Settings */ |
| 514 |
/*---------------------------------------------------------*/ |
| 515 |
|
| 516 |
function mbt_cj_affiliate_settings_init() { |
| 517 |
add_action('mbt_affiliate_settings_render', 'mbt_cj_affiliate_settings_render'); |
| 518 |
} |
| 519 |
add_action('mbt_init', 'mbt_cj_affiliate_settings_init'); |
| 520 |
|
| 521 |
function mbt_cj_affiliate_settings_render() { |
| 522 |
?> |
| 523 |
<table class="form-table"> |
| 524 |
<tbody> |
| 525 |
<tr> |
| 526 |
<th> |
| 527 |
<label style="color: #666"><?php esc_attr_e('Commission Junction', 'mybooktable'); ?></label> |
| 528 |
<div class="mbt-affiliate-usedby"> |
| 529 |
Used by: |
| 530 |
<ul> |
| 531 |
<li>Audible.com Buy Button</li> |
| 532 |
<li>Barnes & Noble Buy Button</li> |
| 533 |
<li>Nook Buy Button</li> |
| 534 |
</ul> |
| 535 |
</div> |
| 536 |
</th> |
| 537 |
<td> |
| 538 |
<input type="text" disabled="true" value="" class="regular-text"> |
| 539 |
<p class="description"><?php echo(wp_kses_post(mbt_get_upgrade_message())); ?></p> |
| 540 |
</td> |
| 541 |
</tr> |
| 542 |
</tbody> |
| 543 |
</table> |
| 544 |
<?php |
| 545 |
} |
| 546 |
|
| 547 |
function mbt_get_cj_affiliate_link($url, $website_id) { |
| 548 |
$server = 'www.qksrv.net'; |
| 549 |
$scheme = 'http'; |
| 550 |
$url_info = wp_parse_url($url); |
| 551 |
if(!empty($url_info) and !empty($url_info['scheme'])) { $scheme = $url_info['scheme']; } |
| 552 |
$hashIndex = strpos($url, '#'); |
| 553 |
$frag = ""; |
| 554 |
if($hashIndex !== false) { |
| 555 |
$frag = substr($url, $hashIndex + 1); |
| 556 |
$url = substr($url, 0, $hashIndex); |
| 557 |
} |
| 558 |
$extraParams = ""; |
| 559 |
if(!empty($frag)) { $extraParams = "/fragment/".urlencode($frag); } |
| 560 |
return $scheme."://".$server."/links/".$website_id."/type/am".$extraParams."/".$url; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
/*---------------------------------------------------------*/ |
| 566 |
/* Genius Link Validation */ |
| 567 |
/*---------------------------------------------------------*/ |
| 568 |
|
| 569 |
function mbt_is_genius_link($url) { |
| 570 |
if(empty($url)) { return false; } |
| 571 |
$parsed = wp_parse_url($url); |
| 572 |
if(empty($parsed['host']) or empty($parsed['path'])) { return false; } |
| 573 |
return (strcasecmp($parsed['host'], 'buy.geni.us') == 0 or strcasecmp($parsed['host'], 'geni.us') == 0 or strcasecmp($parsed['host'], 'target.georiot.com') == 0); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
/*---------------------------------------------------------*/ |
| 579 |
/* Amazon Buy Buttons */ |
| 580 |
/*---------------------------------------------------------*/ |
| 581 |
|
| 582 |
function mbt_amazon_buybuttons_init() { |
| 583 |
if(!mbt_get_setting('disable_amazon_affiliates')) { |
| 584 |
add_filter('mbt_filter_buybutton_data', 'mbt_filter_amazon_buybutton_data', 10, 2); |
| 585 |
add_action('wp_ajax_mbt_amazon_buybutton_preview', 'mbt_amazon_buybutton_preview'); |
| 586 |
add_action('mbt_buybutton_editor', 'mbt_amazon_buybutton_editor', 10, 4); |
| 587 |
} |
| 588 |
} |
| 589 |
add_action('mbt_init', 'mbt_amazon_buybuttons_init'); |
| 590 |
|
| 591 |
function mbt_get_amazon_AISN($url) { |
| 592 |
$matches = array(); |
| 593 |
preg_match("/((dp%2F)|(dp\/)|(dp\/product\/)|(\/ASIN\/)|(gp\/product\/)|(exec\/obidos\/tg\/detail\/\-\/)|(asins=))([A-Z0-9]{10})/", $url, $matches); |
| 594 |
return empty($matches[9]) ? '' : $matches[9]; |
| 595 |
} |
| 596 |
|
| 597 |
function mbt_get_amazon_tld($url) { |
| 598 |
$matches = array(); |
| 599 |
preg_match("/amazon\.([a-zA-Z\.]+)/", $url, $matches); |
| 600 |
return empty($matches) ? 'com' : $matches[1]; |
| 601 |
} |
| 602 |
|
| 603 |
function mbt_filter_amazon_buybutton_data($data, $store) { |
| 604 |
$use_attr_links = mbt_get_setting('use_amazon_attribution_links'); |
| 605 |
if(!$use_attr_links){ |
| 606 |
if(($data['store'] == 'amazon' or $data['store'] == 'kindle') and !empty($data['url']) and !mbt_is_genius_link($data['url']) and !mbt_get_setting('disable_amazon_affiliates')) { |
| 607 |
$tld = mbt_get_amazon_tld($data['url']); |
| 608 |
$aisn = mbt_get_amazon_AISN($data['url']); |
| 609 |
$affiliatecode = mbt_get_setting('amazon_buybutton_affiliate_code'); |
| 610 |
$affiliate_tag = empty($affiliatecode) ? '' : '?tag='.$affiliatecode; |
| 611 |
$data['url'] = (empty($tld) or empty($aisn)) ? '' : 'http://www.amazon.'.$tld.'/dp/'.$aisn.$affiliate_tag; |
| 612 |
} |
| 613 |
} |
| 614 |
return $data; |
| 615 |
} |
| 616 |
|
| 617 |
function mbt_amazon_buybutton_preview() { |
| 618 |
if(isset($_REQUEST['mbt_nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']), 'mbt-ajax-nonce-amazon-buybutton')){ |
| 619 |
if(empty($_REQUEST['data'])) { die(); } |
| 620 |
$id = mbt_get_amazon_AISN(sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 621 |
if(mbt_is_genius_link(sanitize_text_field(wp_unslash($_REQUEST['data'])))) { |
| 622 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Genius Link', 'mybooktable').'</span>')); |
| 623 |
} else if(empty($id)) { |
| 624 |
echo(wp_kses_post('<span class="mbt_admin_message_failure">'.__('Invalid Amazon product link', 'mybooktable').'</span>')); |
| 625 |
} else { |
| 626 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Amazon product link', 'mybooktable').'</span>')); |
| 627 |
} |
| 628 |
} |
| 629 |
die(); |
| 630 |
} |
| 631 |
|
| 632 |
function mbt_amazon_buybutton_editor($editor, $data, $id, $store) { |
| 633 |
if($data['store'] == 'amazon' or $data['store'] == 'kindle') { |
| 634 |
$mbt_nonce = wp_create_nonce('mbt-ajax-nonce-amazon-buybutton'); |
| 635 |
$editor .= ' |
| 636 |
<script type="text/javascript"> |
| 637 |
var url = jQuery("#'.$id.'_url"); |
| 638 |
var mydiv = jQuery("<div>",{ |
| 639 |
class: "mbt_feedback_above mbt_feedback", |
| 640 |
}); |
| 641 |
url.before(jQuery(mydiv)); |
| 642 |
url.addClass("mbt_feedback_refresh mbt_feedback_refresh_initial"); |
| 643 |
url.attr("data-refresh-action", "mbt_amazon_buybutton_preview"); |
| 644 |
url.attr("data-refresh-nonce", "'.$mbt_nonce.'"); |
| 645 |
url.attr("data-element", "self"); |
| 646 |
if(typeof url.mbt_feedback !== "undefined") { url.mbt_feedback(); } |
| 647 |
</script>'; |
| 648 |
} |
| 649 |
//print_r('<pre style="padding: 10px; border: 1px solid #000; margin: 10px">'); print_r($editor ); print_r('</pre>'); |
| 650 |
|
| 651 |
|
| 652 |
return $editor; |
| 653 |
} |
| 654 |
|
| 655 |
/*---------------------------------------------------------*/ |
| 656 |
/* Audible Buy Button */ |
| 657 |
/*---------------------------------------------------------*/ |
| 658 |
|
| 659 |
function mbt_audible_buybuttons_init() { |
| 660 |
add_action('mbt_filter_buybutton_data', 'mbt_filter_audible_buybutton_data', 10, 2); |
| 661 |
add_action('wp_ajax_mbt_audible_buybutton_preview', 'mbt_audible_buybutton_preview'); |
| 662 |
add_action('mbt_buybutton_editor', 'mbt_audible_buybutton_editor', 10, 4); |
| 663 |
} |
| 664 |
add_action('mbt_init', 'mbt_audible_buybuttons_init'); |
| 665 |
|
| 666 |
function mbt_filter_audible_buybutton_data($data, $store) { |
| 667 |
if($data['store'] == 'audible' and !empty($data['url']) and !mbt_is_genius_link($data['url'])) { |
| 668 |
$data['url'] = mbt_get_cj_affiliate_link($data['url'], 7737731); |
| 669 |
} |
| 670 |
return $data; |
| 671 |
} |
| 672 |
|
| 673 |
function mbt_audible_buybutton_preview() { |
| 674 |
if(isset($_REQUEST['mbt_nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce-audible-buybutton')){ |
| 675 |
if(empty($_REQUEST['data'])) { die(); } |
| 676 |
$parsed = wp_parse_url(sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 677 |
if(mbt_is_genius_link(sanitize_text_field(wp_unslash($_REQUEST['data'])))) { |
| 678 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Genius Link', 'mybooktable').'</span>')); |
| 679 |
} else if(isset($parsed['host']) and strpos($parsed['host'], 'amazon') !== false) { |
| 680 |
$id = mbt_get_amazon_AISN(sanitize_text_field(wp_unslash($_REQUEST['data']))); |
| 681 |
if(empty($id)) { |
| 682 |
echo(wp_kses_post('<span class="mbt_admin_message_failure">'.__('Invalid Audible product link', 'mybooktable').'</span>')); |
| 683 |
} else { |
| 684 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Audible product link', 'mybooktable').'</span>')); |
| 685 |
} |
| 686 |
} else if(isset($parsed['host']) and strpos($parsed['host'], 'audible') !== false) { |
| 687 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Audible product link', 'mybooktable').'</span>')); |
| 688 |
} else { |
| 689 |
echo(wp_kses_post('<span class="mbt_admin_message_failure">'.__('Invalid Audible product link', 'mybooktable').'</span>')); |
| 690 |
} |
| 691 |
} |
| 692 |
die(); |
| 693 |
} |
| 694 |
|
| 695 |
function mbt_audible_buybutton_editor($editor, $data, $id, $store) { |
| 696 |
if($data['store'] == 'audible') { |
| 697 |
$mbt_nonce = wp_create_nonce('mbt-ajax-nonce-audible-buybutton'); |
| 698 |
$editor .= ' |
| 699 |
<script type="text/javascript"> |
| 700 |
var url = jQuery("#'.$id.'_url"); |
| 701 |
var mydiv = jQuery("<div>",{ |
| 702 |
class: "mbt_feedback_above mbt_feedback", |
| 703 |
}); |
| 704 |
url.before(jQuery(mydiv)); |
| 705 |
url.addClass("mbt_feedback_refresh mbt_feedback_refresh_initial"); |
| 706 |
url.attr("data-refresh-action", "mbt_audible_buybutton_preview"); |
| 707 |
url.attr("data-refresh-nonce", "'.$mbt_nonce.'"); |
| 708 |
url.attr("data-element", "self"); |
| 709 |
if(typeof url.mbt_feedback !== "undefined") { url.mbt_feedback(); } |
| 710 |
</script>'; |
| 711 |
} |
| 712 |
return $editor; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
/*---------------------------------------------------------*/ |
| 718 |
/* Barnes & Noble Buy Buttons */ |
| 719 |
/*---------------------------------------------------------*/ |
| 720 |
|
| 721 |
function mbt_bnn_buybuttons_init() { |
| 722 |
add_action('mbt_filter_buybutton_data', 'mbt_filter_bnn_buybutton_data', 10, 2); |
| 723 |
add_action('wp_ajax_mbt_bnn_buybutton_preview', 'mbt_bnn_buybutton_preview'); |
| 724 |
add_action('mbt_buybutton_editor', 'mbt_bnn_buybutton_editor', 10, 4); |
| 725 |
} |
| 726 |
add_action('mbt_init', 'mbt_bnn_buybuttons_init'); |
| 727 |
|
| 728 |
function mbt_is_bbn_url_valid($url) { |
| 729 |
return preg_match("/barnesandnoble.com\/((s\/([0-9]{13}))|(w\/.*(([eE][aA][nN]=[0-9]{13})|(\/[0-9]{10}))))/", $url); |
| 730 |
} |
| 731 |
|
| 732 |
function mbt_filter_bnn_buybutton_data($data, $store) { |
| 733 |
if(($data['store'] == 'bnn' or $data['store'] == 'nook') and !empty($data['url']) and !mbt_is_genius_link($data['url'])) { |
| 734 |
$data['url'] = mbt_get_cj_affiliate_link($data['url'], 7737731); |
| 735 |
} |
| 736 |
return $data; |
| 737 |
} |
| 738 |
|
| 739 |
function mbt_bnn_buybutton_preview() { |
| 740 |
if(isset($_REQUEST['mbt_nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce-bnn-buybutton')){ |
| 741 |
if(empty($_REQUEST['data'])) { die(); } |
| 742 |
if(mbt_is_genius_link(sanitize_text_field(wp_unslash($_REQUEST['data'])))) { |
| 743 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Genius Link', 'mybooktable').'</span>')); |
| 744 |
} else if(!mbt_is_bbn_url_valid(sanitize_text_field(wp_unslash($_REQUEST['data'])))) { |
| 745 |
echo(wp_kses_post('<span class="mbt_admin_message_failure">'.__('Invalid Barnes & Noble product link', 'mybooktable').'</span>')); |
| 746 |
} else { |
| 747 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Barnes & Noble product link', 'mybooktable').'</span>')); |
| 748 |
} |
| 749 |
} |
| 750 |
die(); |
| 751 |
} |
| 752 |
|
| 753 |
function mbt_bnn_buybutton_editor($editor, $data, $id, $store) { |
| 754 |
if($data['store'] == 'bnn' or $data['store'] == 'nook') { |
| 755 |
$mbt_nonce = wp_create_nonce('mbt-ajax-nonce-bnn-buybutton'); |
| 756 |
$editor .= ' |
| 757 |
<script type="text/javascript"> |
| 758 |
var url = jQuery("#'.$id.'_url"); |
| 759 |
var mydiv = jQuery("<div>",{ |
| 760 |
class: "mbt_feedback_above mbt_feedback", |
| 761 |
}); |
| 762 |
url.before(jQuery(mydiv)); |
| 763 |
url.addClass("mbt_feedback_refresh mbt_feedback_refresh_initial"); |
| 764 |
url.attr("data-refresh-action", "mbt_bnn_buybutton_preview"); |
| 765 |
url.attr("data-refresh-nonce", "'.$mbt_nonce.'"); |
| 766 |
url.attr("data-element", "self"); |
| 767 |
if(typeof url.mbt_feedback !== "undefined") { url.mbt_feedback(); } |
| 768 |
</script>'; |
| 769 |
} |
| 770 |
return $editor; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
/*---------------------------------------------------------*/ |
| 776 |
/* Kobo Buy Button */ |
| 777 |
/*---------------------------------------------------------*/ |
| 778 |
|
| 779 |
function mbt_kobo_buybuttons_init() { |
| 780 |
add_action('mbt_filter_buybutton_data', 'mbt_filter_kobo_buybutton_data', 10, 2); |
| 781 |
} |
| 782 |
add_action('mbt_init', 'mbt_kobo_buybuttons_init'); |
| 783 |
|
| 784 |
function mbt_filter_kobo_buybutton_data($data, $store) { |
| 785 |
if($data['store'] == 'kobo' and !empty($data['url']) and !mbt_is_genius_link($data['url'])) { |
| 786 |
$data['url'] = 'https://click.linksynergy.com/deeplink?id=W1PQs9y/1/c&mid=37217&murl='.urlencode($data['url']); |
| 787 |
} |
| 788 |
return $data; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
/*---------------------------------------------------------*/ |
| 794 |
/* Gumroad Buy Button */ |
| 795 |
/*---------------------------------------------------------*/ |
| 796 |
|
| 797 |
function mbt_gumroad_buybutton_init() { |
| 798 |
add_filter('mbt_buybutton_editor', 'mbt_gumroad_buybutton_editor', 10, 4); |
| 799 |
add_filter('mbt_format_buybutton', 'mbt_gumroad_buybutton_button', 10, 3); |
| 800 |
} |
| 801 |
add_action('mbt_init', 'mbt_gumroad_buybutton_init'); |
| 802 |
|
| 803 |
function mbt_gumroad_buybutton_editor($output, $data, $id, $store) { |
| 804 |
if($data['store'] == 'gumroad') { |
| 805 |
$output = '<p><input type="checkbox" id="'.esc_attr($id).'_use_shadowbox" name="'.esc_attr($id).'[use_shadowbox]" '.checked(!empty($data['use_shadowbox']), true, false).'> <label for="'.esc_attr($id).'_use_shadowbox">Use shadow box for purchase?</label></p>'.$output; |
| 806 |
} |
| 807 |
return $output; |
| 808 |
} |
| 809 |
|
| 810 |
function mbt_gumroad_buybutton_button($output, $data, $store) { |
| 811 |
if($data['store'] == 'gumroad' and !empty($data['use_shadowbox'])) { |
| 812 |
$data['url'] = $data['url'].((strpos($data['url'], '?') === false) ? '?as_embed=true&outbound_embed=true' : '&as_embed=true&outbound_embed=true'); |
| 813 |
|
| 814 |
if(!empty($data['display']) and $data['display'] == 'text') { |
| 815 |
/* translators: %s: the name of the book source (store) */ |
| 816 |
$output = empty($data['url']) ? '' : '<li><div class="mbt-shadowbox-iframe" data-href="'.htmlspecialchars($data['url']).'">'.sprintf(__('Buy from %s', 'mybooktable'), $store['name']).'</div></li>'; |
| 817 |
} else { |
| 818 |
/* translators: %s: the name of the book source (store) */ |
| 819 |
$output = empty($data['url']) ? '' : '<div class="mbt-book-buybutton"><div class="mbt-shadowbox-iframe" data-href="'.htmlspecialchars($data['url']).'"><img src="'.mbt_image_url($data['store'].'_button.png').'" border="0" alt="'.sprintf(__('Buy from %s', 'mybooktable'), $store['name']).'"/></div></div>'; |
| 820 |
} |
| 821 |
} |
| 822 |
return $output; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
/*---------------------------------------------------------*/ |
| 828 |
/* Celery Buy Button */ |
| 829 |
/*---------------------------------------------------------*/ |
| 830 |
|
| 831 |
function mbt_celery_buybutton_init() { |
| 832 |
add_filter('mbt_buybutton_editor', 'mbt_celery_buybutton_editor', 10, 4); |
| 833 |
} |
| 834 |
add_action('mbt_init', 'mbt_celery_buybutton_init'); |
| 835 |
|
| 836 |
function mbt_celery_buybutton_editor($output, $data, $id, $store) { |
| 837 |
if($data['store'] == 'celery') { |
| 838 |
$output = substr_replace($output, ' <a href="https://www.trycelery.com/" target="_blank">'.__('Sign up and Learn more about Celery Pre-Orders', 'mybooktable').'</a>', strlen($output)-4, 0); |
| 839 |
} |
| 840 |
return $output; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
/*---------------------------------------------------------*/ |
| 846 |
/* iBooks & iTunes Buy Buttons */ |
| 847 |
/*---------------------------------------------------------*/ |
| 848 |
|
| 849 |
function mbt_apple_buybuttons_init() { |
| 850 |
if(!mbt_get_setting('disable_itunes_affiliates')) { |
| 851 |
add_filter('mbt_filter_buybutton_data', 'mbt_filter_apple_buybuttons_data', 10, 2); |
| 852 |
add_action('wp_ajax_mbt_apple_buybutton_preview', 'mbt_apple_buybutton_preview'); |
| 853 |
add_action('mbt_buybutton_editor', 'mbt_apple_buybutton_editor', 10, 4); |
| 854 |
} |
| 855 |
} |
| 856 |
add_action('mbt_init', 'mbt_apple_buybuttons_init'); |
| 857 |
|
| 858 |
function mbt_filter_apple_buybuttons_data($data, $store) { |
| 859 |
if(($data['store'] == 'ibooks' or $data['store'] == 'itunes') and !empty($data['url']) and !mbt_is_genius_link($data['url']) and !mbt_get_setting('disable_itunes_affiliates')) { |
| 860 |
$token = mbt_get_setting('itunes_affiliate_token'); |
| 861 |
$host = wp_parse_url($data['url'], PHP_URL_HOST); |
| 862 |
if(isset($host)) { |
| 863 |
if( stripos($host, 'itunes.apple.com') !== false or stripos($host, 'books.apple.com') !== false) { |
| 864 |
if(stripos($host, 'geo') === false and stripos($host, 'itunes.apple.com') !== false) { $data['url'] = str_replace($host, 'geo.'.$host, $data['url']); } |
| 865 |
$data['url'] .= (wp_parse_url($data['url'], PHP_URL_QUERY) ? '&' : '?') . 'uo=8&at='.$token; |
| 866 |
} else { |
| 867 |
$data['url'] = ''; |
| 868 |
} |
| 869 |
} else { |
| 870 |
$data['url'] = ''; |
| 871 |
} |
| 872 |
} |
| 873 |
return $data; |
| 874 |
} |
| 875 |
|
| 876 |
function mbt_apple_buybutton_preview() { |
| 877 |
if(isset($_REQUEST['mbt_nonce']) && wp_verify_nonce(sanitize_key($_REQUEST['mbt_nonce']),'mbt-ajax-nonce-apple-buybutton')){ |
| 878 |
if(empty($_REQUEST['data'])) { die(); } |
| 879 |
$host = wp_parse_url(sanitize_text_field(wp_unslash($_REQUEST['data'])), PHP_URL_HOST); |
| 880 |
if(mbt_is_genius_link(sanitize_text_field(wp_unslash($_REQUEST['data'])))) { |
| 881 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid Genius Link', 'mybooktable').'</span>')); |
| 882 |
} else if(stripos($host, 'itunes.apple.com') === false and stripos($host, 'books.apple.com') === false) { |
| 883 |
echo(wp_kses_post('<span class="mbt_admin_message_failure">'.__('Invalid product link', 'mybooktable').'</span>')); |
| 884 |
} else { |
| 885 |
echo(wp_kses_post('<span class="mbt_admin_message_success">'.__('Valid product link', 'mybooktable').'</span>')); |
| 886 |
} |
| 887 |
} |
| 888 |
die(); |
| 889 |
} |
| 890 |
|
| 891 |
function mbt_apple_buybutton_editor($editor, $data, $id, $store) { |
| 892 |
if($data['store'] == 'ibooks' or $data['store'] == 'itunes') { |
| 893 |
$mbt_nonce = wp_create_nonce('mbt-ajax-nonce-apple-buybutton'); |
| 894 |
$editor .= ' |
| 895 |
<script type="text/javascript"> |
| 896 |
var url = jQuery("#'.$id.'_url"); |
| 897 |
var mydiv = jQuery("<div>",{ |
| 898 |
class: "mbt_feedback_above mbt_feedback", |
| 899 |
}); |
| 900 |
url.before(jQuery(mydiv)); |
| 901 |
url.addClass("mbt_feedback_refresh mbt_feedback_refresh_initial"); |
| 902 |
url.attr("data-refresh-action", "mbt_apple_buybutton_preview"); |
| 903 |
url.attr("data-refresh-nonce", "'.$mbt_nonce.'"); |
| 904 |
url.attr("data-element", "self"); |
| 905 |
if(typeof url.mbt_feedback !== "undefined") { url.mbt_feedback(); } |
| 906 |
</script>'; |
| 907 |
} |
| 908 |
return $editor; |
| 909 |
} |
| 910 |
|