| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Etsy-Shop |
| 4 |
*/ |
| 5 |
/* |
| 6 |
Plugin Name: Etsy Shop |
| 7 |
Plugin URI: http://wordpress.org/extend/plugins/etsy-shop/ |
| 8 |
Description: Inserts Etsy products in page or post using shortcode method. |
| 9 |
Author: Frédéric Sheedy |
| 10 |
Text Domain: etsy-shop |
| 11 |
Version: 3.1 |
| 12 |
*/ |
| 13 |
|
| 14 |
/* |
| 15 |
* Copyright 2011-2026 Frédéric Sheedy |
| 16 |
* |
| 17 |
* This program is free software; you can redistribute it and/or modify |
| 18 |
* it under the terms of the GNU General Public License, version 2, as |
| 19 |
* published by the Free Software Foundation. |
| 20 |
* |
| 21 |
* This program is distributed in the hope that it will be useful, |
| 22 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
* GNU General Public License for more details. |
| 25 |
* |
| 26 |
* You should have received a copy of the GNU General Public License |
| 27 |
* along with this program; if not, write to the Free Software |
| 28 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 29 |
*/ |
| 30 |
|
| 31 |
/* Roadmap |
| 32 |
* TODO: allow more than 100 items |
| 33 |
* TODO: customize currency |
| 34 |
* TODO: get Etsy translations |
| 35 |
* TODO: Add MCE Button / block |
| 36 |
*/ |
| 37 |
|
| 38 |
define( 'ETSY_SHOP_VERSION', '3.1' ); |
| 39 |
define( 'ETSY_SHOP_CACHE_PREFIX', 'etsy_shop_cache_' ); |
| 40 |
|
| 41 |
// plugin activation |
| 42 |
register_activation_hook( __FILE__, 'etsy_shop_activate' ); |
| 43 |
|
| 44 |
// check for update |
| 45 |
add_action( 'plugins_loaded', 'etsy_shop_update' ); |
| 46 |
|
| 47 |
// add Settings link |
| 48 |
add_filter( 'plugin_action_links', 'etsy_shop_plugin_action_links', 10, 2 ); |
| 49 |
|
| 50 |
|
| 51 |
function etsy_shop_update() { |
| 52 |
$etsy_shop_version = get_option( 'etsy_shop_version' ); |
| 53 |
if ( $etsy_shop_version != ETSY_SHOP_VERSION ) { |
| 54 |
|
| 55 |
// upgrade logic here |
| 56 |
|
| 57 |
// initialize timeout option if not already there |
| 58 |
if( !get_option( 'etsy_shop_timeout' ) ) { |
| 59 |
add_option( 'etsy_shop_timeout', '10' ); |
| 60 |
} |
| 61 |
|
| 62 |
// initialize cache life option if not already there |
| 63 |
if( !get_option( 'etsy_shop_cache_life' ) ) { |
| 64 |
add_option( 'etsy_shop_cache_life', '21600' ); // 6 hours in seconds |
| 65 |
} |
| 66 |
|
| 67 |
// v3.0 - Remove debug option |
| 68 |
if( get_option( 'etsy_shop_debug_mode' ) ) { |
| 69 |
//delete_option( 'etsy_shop_debug_mode' ); |
| 70 |
} |
| 71 |
|
| 72 |
// update the version value |
| 73 |
update_option( 'etsy_shop_version', ETSY_SHOP_VERSION ); |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
function etsy_shop_activate() { |
| 79 |
etsy_shop_update(); |
| 80 |
} |
| 81 |
|
| 82 |
function etsy_shop_process() { |
| 83 |
if ( func_num_args() === 1) { |
| 84 |
$attributes = func_get_arg(0); |
| 85 |
$shop_id = wp_strip_all_tags( $attributes['shop_name'] ); |
| 86 |
$section_id = wp_strip_all_tags( $attributes['section_id'] ); |
| 87 |
$listing_id = wp_strip_all_tags( $attributes['listing_id'] ); |
| 88 |
$show_available_tag = ( !$attributes['show_available_tag'] ? 'true' : wp_strip_all_tags( $attributes['show_available_tag'] ) ); |
| 89 |
$language = ( !$attributes['language'] ? null : wp_strip_all_tags( $attributes['language']) ); |
| 90 |
$columns = ( !$attributes['columns'] ? 3 : (int) wp_strip_all_tags( $attributes['columns'] ) ); |
| 91 |
$thumb_size = ( !$attributes['thumb_size'] ? "medium" : wp_strip_all_tags( $attributes['thumb_size'] ) ); |
| 92 |
$width = ( !$attributes['width'] ? "172px" : wp_strip_all_tags( $attributes['width'] ) ); |
| 93 |
$height = ( !$attributes['height'] ? "135px" : wp_strip_all_tags( $attributes['height'] ) ); |
| 94 |
$limit = ( !$attributes['limit'] ? 100 : (int) $attributes['limit'] ); |
| 95 |
$offset = ( !$attributes['offset'] ? 0 : (int) $attributes['offset'] ); |
| 96 |
} else { |
| 97 |
return __( 'Etsy Shop: invalid number of arguments', 'etsy-shop' ); |
| 98 |
} |
| 99 |
|
| 100 |
// Filter the values |
| 101 |
$shop_id = preg_replace( '/[^a-zA-Z0-9,]/', '', $shop_id ); |
| 102 |
$section_id = preg_replace( '/[^a-zA-Z0-9,]/', '', $section_id ); |
| 103 |
$listing_id = preg_replace( '/[^a-zA-Z0-9,]/', '', $listing_id ); |
| 104 |
$width = esc_attr($width); |
| 105 |
$height = esc_attr($height); |
| 106 |
|
| 107 |
//Filter the thumb size |
| 108 |
switch ( $thumb_size ) { |
| 109 |
case ( "small" ): |
| 110 |
$thumb_size = "url_75x75"; |
| 111 |
break; |
| 112 |
case ( "medium" ): |
| 113 |
$thumb_size = "url_170x135"; |
| 114 |
break; |
| 115 |
case ( "large" ): |
| 116 |
$thumb_size = "url_570xN"; |
| 117 |
break; |
| 118 |
case ( "original" ): |
| 119 |
$thumb_size = "url_fullxfull"; |
| 120 |
break; |
| 121 |
default: |
| 122 |
$thumb_size = "url_570xN"; |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
// Filter Language |
| 127 |
if ( strlen( $language ) != 2 ) { |
| 128 |
$language = null; |
| 129 |
} |
| 130 |
|
| 131 |
if ( $shop_id != '' && $section_id != '' ) { |
| 132 |
// generate listing for shop section |
| 133 |
$listings_array = etsy_shop_getShopSectionListings( $shop_id, $section_id, $limit, $offset ); |
| 134 |
if ( is_array( $listings_array ) ) { |
| 135 |
$listings_type = $listings_array[0]; |
| 136 |
$listings = $listings_array[1]; |
| 137 |
} else { |
| 138 |
$listings = $listings_array; |
| 139 |
} |
| 140 |
|
| 141 |
if ( !is_wp_error( $listings ) ) { |
| 142 |
$data = "<!-- etsy_shop_cache $listings_type -->"; |
| 143 |
|
| 144 |
$data .= '<div class="etsy-shop-listing-container">'; |
| 145 |
$n = 0; |
| 146 |
|
| 147 |
// verify if we use target blank |
| 148 |
if ( get_option( 'etsy_shop_target_blank' ) ) { |
| 149 |
$target = '_blank'; |
| 150 |
} else { |
| 151 |
$target = '_self'; |
| 152 |
} |
| 153 |
|
| 154 |
foreach ( $listings->results as $result ) { |
| 155 |
|
| 156 |
if ( !empty($listing_id) && $result->listing_id != $listing_id ) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
|
| 160 |
if ($language && isset( $result->translations->$language->title ) ) { |
| 161 |
$title = $result->translations->$language->title; |
| 162 |
} else { |
| 163 |
$title = $result->title; |
| 164 |
} |
| 165 |
|
| 166 |
if ($result->price->divisor > 0) { |
| 167 |
$price = $result->price->amount/$result->price->divisor; |
| 168 |
} else { |
| 169 |
$price = $result->price->amount; |
| 170 |
} |
| 171 |
|
| 172 |
$listing_html = etsy_shop_generateListing( |
| 173 |
$result->listing_id, |
| 174 |
$title, |
| 175 |
$result->state, |
| 176 |
$price, |
| 177 |
$result->price->currency_code, |
| 178 |
$result->quantity, |
| 179 |
$result->url, |
| 180 |
$result->images[0]->$thumb_size, |
| 181 |
$target, |
| 182 |
$show_available_tag, |
| 183 |
$width, |
| 184 |
$height |
| 185 |
); |
| 186 |
if ( $listing_html !== false ) { |
| 187 |
$data = $data.'<div class="etsy-shop-listing">'.$listing_html.'</div>'; |
| 188 |
} |
| 189 |
} |
| 190 |
$data = $data.'</div>'; |
| 191 |
} else { |
| 192 |
$data = $listings->get_error_message(); |
| 193 |
} |
| 194 |
} else { |
| 195 |
// must have 2 arguments |
| 196 |
$data = __( 'Etsy Shop: empty arguments', 'etsy-shop' ); |
| 197 |
} |
| 198 |
|
| 199 |
return $data; |
| 200 |
} |
| 201 |
|
| 202 |
// Process shortcode |
| 203 |
function etsy_shop_shortcode( $atts ) { |
| 204 |
// if API Key exist |
| 205 |
if ( get_option( 'etsy_shop_api_key' ) ) { |
| 206 |
$attributes = shortcode_atts( array( |
| 207 |
'shop_name' => null, |
| 208 |
'section_id' => null, |
| 209 |
'listing_id' => null, |
| 210 |
'thumb_size' => null, |
| 211 |
'language' => null, |
| 212 |
'columns' => null, |
| 213 |
'limit' => null, |
| 214 |
'offset' => null, |
| 215 |
'show_available_tag' => true, |
| 216 |
'width' => "172px", |
| 217 |
'height' => "135px" |
| 218 |
), $atts ); |
| 219 |
|
| 220 |
$content = etsy_shop_process( $attributes ); |
| 221 |
return $content; |
| 222 |
} else { |
| 223 |
// no API Key set |
| 224 |
return __( 'Etsy Shop: Shortcode detected but API KEY is not set.', 'etsy-shop' ); |
| 225 |
} |
| 226 |
} |
| 227 |
add_shortcode( 'etsy-shop', 'etsy_shop_shortcode' ); |
| 228 |
|
| 229 |
function etsy_shop_getShopSectionListings( $etsy_shop_name, $etsy_section_id, $limit, $offset ) { |
| 230 |
$name = ETSY_SHOP_CACHE_PREFIX.$etsy_shop_name.'-c'.$etsy_section_id.'-'.$limit.'-'.$offset; |
| 231 |
$etsy_cached_content = get_transient( $name ); |
| 232 |
$cache_type = 'none'; |
| 233 |
|
| 234 |
if ( false === $etsy_cached_content ) { |
| 235 |
$etsy_shop_id = etsy_shop_getShopId( $etsy_shop_name ); |
| 236 |
if ( !is_wp_error( $etsy_shop_id ) ) { |
| 237 |
$reponse = etsy_shop_api_request( "shops/$etsy_shop_id/shop-sections/listings", "shop_section_ids=$etsy_section_id" . '&limit='.$limit.'&offset='.$offset ); |
| 238 |
$listing_list = etsy_shop_generateListingList( $reponse ); |
| 239 |
$reponse = etsy_shop_api_request( "listings/batch", "includes=images,translations&listing_ids=$listing_list" ); |
| 240 |
} else { |
| 241 |
// return WP_Error |
| 242 |
$reponse = $etsy_shop_id; |
| 243 |
} |
| 244 |
|
| 245 |
if ( !is_wp_error( $reponse ) ) { |
| 246 |
// if request OK |
| 247 |
set_transient( $name, $reponse, get_option( 'etsy_shop_cache_life' ) ); |
| 248 |
} else { |
| 249 |
// return WP_Error |
| 250 |
return $reponse; |
| 251 |
} |
| 252 |
} else { |
| 253 |
// return cached content |
| 254 |
$cache_type = $name; |
| 255 |
$reponse = $etsy_cached_content; |
| 256 |
} |
| 257 |
|
| 258 |
$data = json_decode( $reponse ); |
| 259 |
$final = array ($cache_type, $data); |
| 260 |
return $final; |
| 261 |
} |
| 262 |
|
| 263 |
function etsy_shop_getShopSection( $etsy_shop_name, $etsy_section_id ) { |
| 264 |
$etsy_shop_id = etsy_shop_getShopId( $etsy_shop_name ); |
| 265 |
$reponse = etsy_shop_api_request( "shops/$etsy_shop_id/sections/$etsy_section_id", NULL , 1 ); |
| 266 |
if ( !is_wp_error( $reponse ) ) { |
| 267 |
$data = json_decode( $reponse ); |
| 268 |
} else { |
| 269 |
// return WP_Error |
| 270 |
return $reponse; |
| 271 |
} |
| 272 |
|
| 273 |
return $data; |
| 274 |
} |
| 275 |
|
| 276 |
function etsy_shop_testAPIKey() { |
| 277 |
$reponse = etsy_shop_api_request( 'openapi-ping' ); |
| 278 |
if ( !is_wp_error( $reponse ) ) { |
| 279 |
$data = json_decode( $reponse ); |
| 280 |
} else { |
| 281 |
// return WP_Error |
| 282 |
return $reponse; |
| 283 |
} |
| 284 |
|
| 285 |
return $data; |
| 286 |
} |
| 287 |
|
| 288 |
function etsy_shop_generateShopSectionList($etsy_shop_name) { |
| 289 |
$list = etsy_shop_getShopSectionList($etsy_shop_name); |
| 290 |
if ( !is_wp_error( $list ) ) { |
| 291 |
$data = ''; |
| 292 |
foreach ( $list->results as $result ) { |
| 293 |
$data .= '<td>'.$result->title.'</td>'; |
| 294 |
$data .= '<td>'.$result->shop_section_id.'</td>'; |
| 295 |
$data .= '<td>'.$result->active_listing_count.'</td>'; |
| 296 |
$data .= '<td>[etsy-shop shop_name="'.get_option( 'etsy_shop_quickstart_shop_id' ).'" section_id="'.$result->shop_section_id.'"]</td></tr>'; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
} else { |
| 301 |
$data = 'ERROR: '.$list->get_error_message(); |
| 302 |
} |
| 303 |
|
| 304 |
return $data; |
| 305 |
} |
| 306 |
|
| 307 |
function etsy_shop_getShopSectionList($etsy_shop_name) { |
| 308 |
$etsy_shop_id = etsy_shop_getShopId( $etsy_shop_name ); |
| 309 |
if ( !is_wp_error( $etsy_shop_id ) ) { |
| 310 |
$reponse = etsy_shop_api_request( "/shops/$etsy_shop_id/sections" ); |
| 311 |
} else { |
| 312 |
return $etsy_shop_id; |
| 313 |
} |
| 314 |
|
| 315 |
if ( !is_wp_error( $reponse ) ) { |
| 316 |
$data = json_decode( $reponse ); |
| 317 |
} else { |
| 318 |
// return WP_Error |
| 319 |
return $reponse; |
| 320 |
} |
| 321 |
|
| 322 |
return $data; |
| 323 |
} |
| 324 |
|
| 325 |
function etsy_shop_generateListingList($response) { |
| 326 |
$data = json_decode( $response ); |
| 327 |
$array = array(); |
| 328 |
foreach ( $data->results as $result ) { |
| 329 |
$array[] = $result->listing_id; |
| 330 |
} |
| 331 |
|
| 332 |
$list = implode('%2C', $array); |
| 333 |
return $list; |
| 334 |
} |
| 335 |
|
| 336 |
function etsy_shop_getShopId($etsy_shop_name) { |
| 337 |
$reponse = etsy_shop_api_request( "shops", 'shop_name='.$etsy_shop_name ); |
| 338 |
if ( !is_wp_error( $reponse ) ) { |
| 339 |
$data = json_decode( $reponse ); |
| 340 |
if ( !isset( $data->results[0]->shop_id ) ) { |
| 341 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: Your shop ID is invalid.', 'etsy-shop' ) ); |
| 342 |
} |
| 343 |
$shop_id = $data->results[0]->shop_id; |
| 344 |
} else { |
| 345 |
// return WP_Error |
| 346 |
return $reponse; |
| 347 |
} |
| 348 |
|
| 349 |
return $shop_id; |
| 350 |
} |
| 351 |
|
| 352 |
function etsy_shop_api_request( $etsy_request, $query_args = null ) { |
| 353 |
$etsy_api_key = get_option( 'etsy_shop_api_key' ); |
| 354 |
$url = "https://api.etsy.com/v3/application/$etsy_request"; |
| 355 |
$headers = array( 'User-Agent' => 'wordpress_etsy_shop_plugin_'.ETSY_SHOP_VERSION, 'x-api-key' => $etsy_api_key ); |
| 356 |
|
| 357 |
if ( $query_args ) { |
| 358 |
$url = $url . '?' . $query_args; |
| 359 |
} |
| 360 |
|
| 361 |
$wp_request_args = array( 'timeout' => get_option( 'etsy_shop_timeout' ), 'headers' => $headers ); |
| 362 |
$request = wp_remote_request( $url , $wp_request_args ); |
| 363 |
|
| 364 |
if ( !is_wp_error( $request ) ) { |
| 365 |
if ( $request['response']['code'] == 200 ) { |
| 366 |
$request_body = $request['body']; |
| 367 |
} else { |
| 368 |
$json = json_decode( $request['body'], true ); |
| 369 |
$error = __( 'Not specified', 'etsy-shop' ); |
| 370 |
if ( null != $json && is_array( $json ) && array_key_exists( 'error', $json ) ) { |
| 371 |
$error = $json['error']; |
| 372 |
} |
| 373 |
if ( $request['headers']['x-error-detail'] == 'Not all requested shop sections exist.' ) { |
| 374 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: Your section ID is invalid.', 'etsy-shop' ) ); |
| 375 |
} elseif ( $request['response']['code'] == 0 ) { |
| 376 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: The plugin timed out waiting for etsy.com reponse. Please change Time out value in the Etsy Shop Options page.', 'etsy-shop' ) ); |
| 377 |
} elseif ( $error === 'Invalid API key' ) { |
| 378 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: Invalid API Key, make sure your API Key is approved.', 'etsy-shop' ) ); |
| 379 |
} else { |
| 380 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: API reponse should be HTTP 200 <br>API Error Description:', 'etsy-shop' ) . ' ' . $error ); |
| 381 |
} |
| 382 |
} |
| 383 |
} else { |
| 384 |
return new WP_Error( 'etsy-shop', __( 'Etsy Shop: Error on API Request', 'etsy-shop' ) ); |
| 385 |
} |
| 386 |
|
| 387 |
return $request_body; |
| 388 |
} |
| 389 |
|
| 390 |
function etsy_shop_generateListing($listing_id, $title, $state, $price, $currency_code, $quantity, $url, $imgurl, $target, $show_available_tag, $width = "172px", $height = "135px") { |
| 391 |
// if the Shop Item is active |
| 392 |
if ( $state == 'active' ) { |
| 393 |
|
| 394 |
if ( $show_available_tag === 'true' ) { |
| 395 |
$state = __( 'Available', 'etsy-shop' ); |
| 396 |
} else { |
| 397 |
$state = ' '; |
| 398 |
} |
| 399 |
|
| 400 |
// Determine Currency Symbol |
| 401 |
if ( $currency_code == 'EUR' ) { |
| 402 |
// Euro sign |
| 403 |
$currency_symbol = '€'; |
| 404 |
} else if ( $currency_code == 'GBP' ) { |
| 405 |
// Pound sign |
| 406 |
$currency_symbol = '£'; |
| 407 |
} else if ( $currency_code == 'DKK' or $currency_code == 'NOK' ) { |
| 408 |
// Nothing for now |
| 409 |
$currency_symbol = ''; |
| 410 |
} else { |
| 411 |
// Dollar Sign |
| 412 |
$currency_symbol = '$'; |
| 413 |
} |
| 414 |
|
| 415 |
$script_tags = ' |
| 416 |
<div class="etsy-shop-listing-card" id="' . $listing_id . '" style="width:' . $width . '"> |
| 417 |
<a title="' . $title . '" href="' . $url . '" target="' . $target . '" class="etsy-shop-listing-thumb"> |
| 418 |
<img alt="' . $title . '" src="' . $imgurl . '" style="height:' . $height . ';"> |
| 419 |
|
| 420 |
<div class="etsy-shop-listing-detail"> |
| 421 |
<p class="etsy-shop-listing-title">'.$title.'</p> |
| 422 |
<p class="etsy-shop-listing-maker">'.$state.'</p> |
| 423 |
</div> |
| 424 |
<p class="etsy-shop-listing-price">'.$currency_symbol.$price.' <span class="etsy-shop-currency-code">'.$currency_code.'</span></p> |
| 425 |
</a> |
| 426 |
</div>'; |
| 427 |
|
| 428 |
return $script_tags; |
| 429 |
} else { |
| 430 |
return false; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
// Custom CSS |
| 435 |
|
| 436 |
add_action( 'wp_print_styles', 'etsy_shop_css' ); |
| 437 |
|
| 438 |
function etsy_shop_css() { |
| 439 |
$link = plugins_url( 'etsy-shop.css', __FILE__ ); |
| 440 |
wp_register_style( 'etsy_shop_style', $link, null, ETSY_SHOP_VERSION ); |
| 441 |
wp_enqueue_style( 'etsy_shop_style' ); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
// Options Menu |
| 446 |
add_action( 'admin_menu', 'etsy_shop_menu' ); |
| 447 |
|
| 448 |
function etsy_shop_menu() { |
| 449 |
add_options_page( __( 'Etsy Shop Options', 'etsy-shop' ), __( 'Etsy Shop', 'etsy-shop' ), 'manage_options', basename( __FILE__ ), 'etsy_shop_options_page' ); |
| 450 |
} |
| 451 |
|
| 452 |
function etsy_shop_enqueue( $hook ) { |
| 453 |
if ( 'settings_page_etsy-shop' !== $hook ) { |
| 454 |
return; |
| 455 |
} |
| 456 |
wp_enqueue_script( |
| 457 |
'ajax-script', |
| 458 |
plugins_url( '/js/etsy-shop-admin.js', __FILE__ ), |
| 459 |
array( 'jquery' ), |
| 460 |
'1.0.0', |
| 461 |
true |
| 462 |
); |
| 463 |
$title_nonce = wp_create_nonce( 'etsy_shop_delete' ); |
| 464 |
wp_localize_script( |
| 465 |
'ajax-script', |
| 466 |
'etsy_shop_admin_ajax', |
| 467 |
array( |
| 468 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 469 |
'nonce' => $title_nonce, |
| 470 |
) |
| 471 |
); |
| 472 |
} |
| 473 |
|
| 474 |
add_action( 'wp_ajax_etsy_shop_delete_cache', 'etsy_shop_delete_cache_ajax_handler' ); |
| 475 |
function etsy_shop_delete_cache_ajax_handler() { |
| 476 |
// did the user is allowed? |
| 477 |
if ( !current_user_can( 'manage_options' ) ) { |
| 478 |
wp_die( __( 'You do not have sufficient permissions to access this page.', 'etsy-shop' ) ); |
| 479 |
} |
| 480 |
|
| 481 |
check_ajax_referer( 'etsy_shop_delete' ); |
| 482 |
|
| 483 |
global $wpdb; |
| 484 |
$name = '_transient_' . ETSY_SHOP_CACHE_PREFIX . '%'; // do not use user input here |
| 485 |
$transient_list = $wpdb->get_results( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '$name'" ); |
| 486 |
|
| 487 |
// delete all items |
| 488 |
foreach ($transient_list as $item ) { |
| 489 |
$name = $item->option_name; |
| 490 |
$name = str_replace( '_transient_', '', $name ); |
| 491 |
delete_transient( $name ); |
| 492 |
} |
| 493 |
|
| 494 |
echo '<span style="color:green;font-weight:bold;">'. __( 'Cache content deleted', 'etsy-shop' ) . '</span>'; |
| 495 |
wp_die(); |
| 496 |
} |
| 497 |
|
| 498 |
// Options Page Ajax |
| 499 |
add_action( 'admin_enqueue_scripts', 'etsy_shop_enqueue' ); |
| 500 |
|
| 501 |
function etsy_shop_options_page() { |
| 502 |
// did the user is allowed? |
| 503 |
if ( !current_user_can( 'manage_options' ) ) { |
| 504 |
wp_die( __( 'You do not have sufficient permissions to access this page.', 'etsy-shop' ) ); |
| 505 |
} |
| 506 |
|
| 507 |
$updated = false; |
| 508 |
$deleted = false; |
| 509 |
|
| 510 |
if ( isset( $_POST['submit'] ) ) { |
| 511 |
|
| 512 |
// Nonces Verification |
| 513 |
$key = get_option( 'etsy_shop_api_key' ); |
| 514 |
check_admin_referer( 'etsy-shop-update-settings_'.$key ); |
| 515 |
|
| 516 |
// did the user enter an API Key? |
| 517 |
if ( isset( $_POST['etsy_shop_api_key'] ) ) { |
| 518 |
$etsy_shop_api_key = wp_filter_nohtml_kses( preg_replace( '/[^A-Za-z0-9:]/', '', $_POST['etsy_shop_api_key'] ) ); |
| 519 |
update_option( 'etsy_shop_api_key', $etsy_shop_api_key ); |
| 520 |
|
| 521 |
// and remember to note the update to user |
| 522 |
$updated = true; |
| 523 |
} |
| 524 |
|
| 525 |
// did the user enter target new window for links? |
| 526 |
if ( isset( $_POST['etsy_shop_target_blank'] ) ) { |
| 527 |
$etsy_shop_target_blank = wp_filter_nohtml_kses( $_POST['etsy_shop_target_blank'] ); |
| 528 |
update_option( 'etsy_shop_target_blank', $etsy_shop_target_blank ); |
| 529 |
|
| 530 |
// and remember to note the update to user |
| 531 |
$updated = true; |
| 532 |
}else { |
| 533 |
$etsy_shop_target_blank = 0; |
| 534 |
update_option( 'etsy_shop_target_blank', $etsy_shop_target_blank ); |
| 535 |
|
| 536 |
// and remember to note the update to user |
| 537 |
$updated = true; |
| 538 |
} |
| 539 |
|
| 540 |
// did the user enter an Timeout? |
| 541 |
if ( isset( $_POST['etsy_shop_timeout'] ) ) { |
| 542 |
$etsy_shop_timeout = wp_filter_nohtml_kses( preg_replace( '/[^0-9]/', '', $_POST['etsy_shop_timeout'] ) ); |
| 543 |
update_option( 'etsy_shop_timeout', $etsy_shop_timeout ); |
| 544 |
|
| 545 |
// and remember to note the update to user |
| 546 |
$updated = true; |
| 547 |
} |
| 548 |
|
| 549 |
// did the user enter an Cache life? |
| 550 |
if ( isset( $_POST['etsy_shop_cache_life'] ) ) { |
| 551 |
$etsy_shop_cache_life = wp_filter_nohtml_kses( preg_replace( '/[^0-9]/', '', $_POST['etsy_shop_cache_life'] ) ); |
| 552 |
update_option( 'etsy_shop_cache_life', $etsy_shop_cache_life * 3600 ); // update time in hours * seconds |
| 553 |
|
| 554 |
// and remember to note the update to user |
| 555 |
$updated = true; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
$etsy_shop_quickstart_shop_id = null; |
| 560 |
$quick_start_update = false; |
| 561 |
if ( isset( $_POST['submitQuickstart'] ) ) { |
| 562 |
// Nonces Verification |
| 563 |
check_admin_referer( 'etsy-shop-settings-quickstart' ); |
| 564 |
|
| 565 |
// did the user enter an shop name? |
| 566 |
if ( isset( $_POST['etsy_shop_quickstart_shop_id'] ) ) { |
| 567 |
$etsy_shop_quickstart_shop_id = wp_filter_nohtml_kses( preg_replace( '/[^a-zA-Z0-9,]/', '', $_POST['etsy_shop_quickstart_shop_id'] ) ); |
| 568 |
update_option( 'etsy_shop_quickstart_shop_id', $etsy_shop_quickstart_shop_id ); |
| 569 |
$quick_start_update = true; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
// grab the Etsy API key |
| 574 |
if( get_option( 'etsy_shop_api_key' ) ) { |
| 575 |
$etsy_shop_api_key = get_option( 'etsy_shop_api_key' ); |
| 576 |
} else { |
| 577 |
add_option( 'etsy_shop_api_key', '' ); |
| 578 |
} |
| 579 |
|
| 580 |
// grab the Etsy Target for links |
| 581 |
if( get_option( 'etsy_shop_target_blank' ) ) { |
| 582 |
$etsy_shop_target_blank = get_option( 'etsy_shop_target_blank' ); |
| 583 |
} else { |
| 584 |
add_option( 'etsy_shop_target_blank', '0' ); |
| 585 |
} |
| 586 |
|
| 587 |
// grab the Etsy Timeout |
| 588 |
if( get_option( 'etsy_shop_timeout' ) ) { |
| 589 |
$etsy_shop_timeout = get_option( 'etsy_shop_timeout' ); |
| 590 |
} else { |
| 591 |
add_option( 'etsy_shop_timeout', '10' ); |
| 592 |
} |
| 593 |
|
| 594 |
// grab the Etsy Cache life |
| 595 |
if( get_option( 'etsy_shop_cache_life' ) ) { |
| 596 |
$etsy_shop_cache_life = get_option( 'etsy_shop_cache_life' ); |
| 597 |
} else { |
| 598 |
add_option( 'etsy_shop_cache_life', '21600' ); |
| 599 |
} |
| 600 |
|
| 601 |
// create the Quickstart shop name |
| 602 |
if( !get_option( 'etsy_shop_quickstart_shop_id' ) ) { |
| 603 |
add_option( 'etsy_shop_quickstart_shop_id', '' ); |
| 604 |
} |
| 605 |
|
| 606 |
if ( $updated ) { |
| 607 |
echo '<div class="updated fade"><p><strong>'. __( 'Options saved.', 'etsy-shop' ) .'</strong></p></div>'; |
| 608 |
} |
| 609 |
|
| 610 |
$etsy_shop_quickstart_step = 1; |
| 611 |
// print the Options Page |
| 612 |
?> |
| 613 |
<style> |
| 614 |
.etsty-shop-quickstart-step { |
| 615 |
display: inline-block; |
| 616 |
margin: 5px 15px 5px 5px; |
| 617 |
padding: 5px 10px 5px 10px; |
| 618 |
background-color: black; |
| 619 |
font-weight: bold; |
| 620 |
color: white; |
| 621 |
} |
| 622 |
#quickStartButton { |
| 623 |
display: block; |
| 624 |
text-decoration: none; |
| 625 |
margin: 10px 10px 10px 10px; |
| 626 |
} |
| 627 |
#etsy-shop-quick-start-content { |
| 628 |
width: 100%; |
| 629 |
background-color: lightgray; |
| 630 |
margin-top: 20px; |
| 631 |
} |
| 632 |
#etsy-shop-quickstart-sections{ |
| 633 |
padding: 5px 20px 10px 80px; |
| 634 |
} |
| 635 |
#etsy-shop-delete-cache-result { |
| 636 |
display: inline-block; |
| 637 |
padding-top: 5px; |
| 638 |
} |
| 639 |
</style> |
| 640 |
<script> |
| 641 |
function quickStartButton() { |
| 642 |
var x = document.getElementById("etsy-shop-quick-start-content"); |
| 643 |
if (x.style.display === "none") { |
| 644 |
x.style.display = "block"; |
| 645 |
} else { |
| 646 |
x.style.display = "none"; |
| 647 |
} |
| 648 |
} |
| 649 |
</script> |
| 650 |
<div class="wrap"> |
| 651 |
<div id="icon-options-general" class="icon32"><br /></div><h2><?php _e( 'Etsy Shop Options', 'etsy-shop' ); ?></h2> |
| 652 |
<div id="etsy-shop-quick-start" style="margin:10px 0px 10px 0px;padding:0px;border:2px solid #dddddd;"> |
| 653 |
<a id="quickStartButton" href="#" onclick="quickStartButton()"> |
| 654 |
<span class="dashicons dashicons-hammer" style="color:green;"></span> |
| 655 |
<span style="color:green;"><?php _e( 'Click here to start easilly!', 'etsy-shop' ); ?></span> |
| 656 |
<span class="dashicons dashicons-arrow-down-alt2" style="color:green;"></span> |
| 657 |
</a> |
| 658 |
<div id="etsy-shop-quick-start-content" <?php if (!$quick_start_update) { ?>style="display:none;"<?php } ?>> |
| 659 |
<form name="etsy_shop_quickstart_form" method="post" action="<?php echo esc_attr($_SERVER['REQUEST_URI']); ?>"> |
| 660 |
<div class="etsty-shop-quickstart-step"><?php _e( 'STEP 1', 'etsy-shop' ); ?></div><span style="font-weight: bold;"><?php _e( 'Is your Etsy API Key is valid?', 'etsy-shop' ); ?></span> |
| 661 |
<?php if ( !is_wp_error( etsy_shop_testAPIKey()) ) { $etsy_shop_quickstart_step = 2; ?> |
| 662 |
<span id="etsy_shop_api_key_status_qs" style="color:green;font-weight:bold;"><?php _e( 'OK, go to step 2', 'etsy-shop' ); ?></span> |
| 663 |
<?php } elseif ( get_option('etsy_shop_api_key') ) { ?> |
| 664 |
<span id="etsy_shop_api_key_status_qs" style="color:red;font-weight:bold;"><?php _e( 'Please configure an API Key below in this page', 'etsy-shop' ); ?></span> |
| 665 |
<?php } ?> |
| 666 |
<?php if ( $etsy_shop_quickstart_step === 2 ) { ?> |
| 667 |
<br><div class="etsty-shop-quickstart-step"><?php _e( 'STEP 2', 'etsy-shop' ); ?></div><span style="margin-top: 8px;font-weight: bold;"><?php _e( 'What is your Shop name on etsy?', 'etsy-shop' ); ?></span> |
| 668 |
<input id="etsy_shop_quickstart_shop_id" name="etsy_shop_quickstart_shop_id" type="text" size="25" value="<?php echo get_option( 'etsy_shop_quickstart_shop_id' ); ?>" class="regular-text code" /> |
| 669 |
<?php wp_nonce_field( 'etsy-shop-settings-quickstart' ); ?> |
| 670 |
<input type="submit" name="submitQuickstart" id="submitQuickstart" class="button-primary" value="<?php _e( 'Search', 'etsy-shop' ); ?>" /> |
| 671 |
</form> |
| 672 |
<?php } ?> |
| 673 |
<?php if ( $etsy_shop_quickstart_step === 2 && get_option( 'etsy_shop_quickstart_shop_id' ) ) { $etsy_shop_quickstart_sections_list = etsy_shop_generateShopSectionList( get_option( 'etsy_shop_quickstart_shop_id' )); ?> |
| 674 |
<br><div class="etsty-shop-quickstart-step"><?php _e( 'STEP 3', 'etsy-shop' ); ?></div><span style="margin-top: 8px;font-weight: bold;"><?php _e( 'List of sections that you can use, put the short code in your page or post:', 'etsy-shop' ); ?></span> |
| 675 |
<?php if ( substr( $etsy_shop_quickstart_sections_list, 0, 3 ) === "ERR" ) { ?> |
| 676 |
<p style="color:red;font-weight:bold;padding-left:80px;"><?php echo $etsy_shop_quickstart_sections_list; ?></p> |
| 677 |
<?php } else { ?> |
| 678 |
<table id="etsy-shop-quickstart-sections" class="wp-list-table widefat"> |
| 679 |
<thead> |
| 680 |
<tr> |
| 681 |
<th><?php _e('Shop Section Name', 'etsy-shop'); ?></th> |
| 682 |
<th><?php _e('ID', 'etsy-shop'); ?></th> |
| 683 |
<th><?php _e('Active listing', 'etsy-shop'); ?></th> |
| 684 |
<th><?php _e('Short code', 'etsy-shop'); ?></th> |
| 685 |
</tr> |
| 686 |
</thead> |
| 687 |
<?php echo $etsy_shop_quickstart_sections_list; ?> |
| 688 |
</table> |
| 689 |
<?php } ?> |
| 690 |
<?php } ?> |
| 691 |
</div> |
| 692 |
</div> |
| 693 |
<form name="etsy_shop_options_form" method="post" action="<?php echo esc_attr($_SERVER['REQUEST_URI']); ?>"> |
| 694 |
<table class="form-table"> |
| 695 |
<tr valign="top"> |
| 696 |
<th scope="row"> |
| 697 |
<label for="etsy_shop_api_key"></label><?php _e('Etsy API Key', 'etsy-shop'); ?> |
| 698 |
</th> |
| 699 |
<td> |
| 700 |
<input id="etsy_shop_api_key" name="etsy_shop_api_key" type="text" size="25" value="<?php echo get_option( 'etsy_shop_api_key' ); ?>" class="regular-text code" /> |
| 701 |
<?php if ( !is_wp_error( etsy_shop_testAPIKey()) ) { ?> |
| 702 |
<span id="etsy_shop_api_key_status" style="color:green;font-weight:bold;"><?php _e( 'Your API Key is valid', 'etsy-shop' ); ?></span> |
| 703 |
<?php } elseif ( get_option('etsy_shop_api_key') ) { ?> |
| 704 |
<span id="etsy_shop_api_key_status" style="color:red;font-weight:bold;"><?php _e( 'You API Key is invalid', 'etsy-shop' ); ?></span> |
| 705 |
<?php } ?> |
| 706 |
<p class="description"> |
| 707 |
<?php echo _e( 'Use the new format: <b>keystring:secret</b>. Etsy require all API requests to include a shared secret starting on January 18, 2026', 'etsy-shop' ); ?> |
| 708 |
<br><?php echo sprintf( __('You may get an Etsy API Key by <a href="%1$s">Creating a new Etsy App</a>', 'etsy-shop' ), 'http://www.etsy.com/developers/register' ); ?> |
| 709 |
<br><?php if ( is_wp_error( etsy_shop_testAPIKey()) ) { echo '<span id="etsy_shop_api_key_status" style="color:red;font-weight:bold;">'; } ?><?php echo sprintf( __('Make sure that your API Key is approved, not in Pending approval status. Go to <a href="%1$s">Manage your apps</a>', 'etsy-shop' ), 'https://www.etsy.com/developers/your-apps' ); ?><?php if ( is_wp_error( etsy_shop_testAPIKey()) ) { echo '</span>'; } ?></p> |
| 710 |
</td> |
| 711 |
</tr> |
| 712 |
<tr valign="top"> |
| 713 |
<th scope="row"> |
| 714 |
<label for="etsy_shop_target_blank"></label><?php _e('Link to new window', 'etsy-shop'); ?></th> |
| 715 |
<td> |
| 716 |
<input id="etsy_shop_target_blank" name="etsy_shop_target_blank" type="checkbox" value="1" <?php checked( '1', get_option( 'etsy_shop_target_blank' ) ); ?> /> |
| 717 |
<p class="description"> |
| 718 |
<?php echo __( 'If you want your links to open a page in a new window', 'etsy-shop' ); ?> |
| 719 |
</p> |
| 720 |
</td> |
| 721 |
</tr> |
| 722 |
<tr valign="top"> |
| 723 |
<th scope="row"> |
| 724 |
<label for="etsy_shop_timeout"></label><?php _e('Timeout', 'etsy-shop'); ?></th> |
| 725 |
<td> |
| 726 |
<input id="etsy_shop_timeout" name="etsy_shop_timeout" type="text" size="2" class="small-text" value="<?php echo get_option( 'etsy_shop_timeout' ); ?>" class="regular-text code" /> |
| 727 |
<p class="description"> |
| 728 |
<?php echo __( 'Time in seconds until a request times out. Default 10 seconds', 'etsy-shop' ); ?> |
| 729 |
</p> |
| 730 |
</td> |
| 731 |
</tr> |
| 732 |
<tr valign="top"> |
| 733 |
<th scope="row"> |
| 734 |
<label for="etsy_shop_cache_life"></label><?php _e('Cache life', 'etsy-shop'); ?></th> |
| 735 |
<td> |
| 736 |
<input id="etsy_shop_cache_life" name="etsy_shop_cache_life" type="text" size="2" class="small-text" value="<?php echo get_option( 'etsy_shop_cache_life' ) / 3600; ?>" class="regular-text code" /> |
| 737 |
<?php _e('hours', 'etsy-shop'); ?> |
| 738 |
<p class="description"> |
| 739 |
<?php echo __( 'Time before the cache update the listing. Default: 6 hours', 'etsy-shop' ); ?> |
| 740 |
</p> |
| 741 |
</td> |
| 742 |
</tr> |
| 743 |
<tr valign="top"> |
| 744 |
<th scope="row"> |
| 745 |
<label for="etsy_shop_cache_delete"></label><?php _e('Delete cache', 'etsy-shop'); ?></th> |
| 746 |
<td> |
| 747 |
<a id="btn-etsy-shop-delete-cache" class="button-secondary" href="#"> <?php _e('Clear all the cache for this plugin', 'etsy-shop'); ?></a> <span id="etsy-shop-delete-cache-result"></span> |
| 748 |
<p class="description"> |
| 749 |
<?php echo __( 'Delete all cache files for Etsy-Shop Plugin.', 'etsy-shop' ); ?> |
| 750 |
</p> |
| 751 |
</td> |
| 752 |
</tr> |
| 753 |
</table> |
| 754 |
|
| 755 |
<h3 class="title"><?php _e( 'Need help?', 'etsy-shop' ); ?></h3> |
| 756 |
<p><?php echo sprintf( __( 'Please open a <a href="%1$s">new topic</a> on Wordpress.org Forum. This is your only way to let me know!', 'etsy-shop' ), 'http://wordpress.org/support/plugin/etsy-shop' ); ?></p> |
| 757 |
|
| 758 |
<h3 class="title"><?php _e( 'Need more features?', 'etsy-shop' ); ?></h3> |
| 759 |
<p><?php echo sprintf( __( 'Please sponsor a feature go to <a href="%1$s">Donation Page</a>.', 'etsy-shop' ), 'http://fsheedy.wordpress.com/etsy-shop-plugin/donate/' ); ?></p> |
| 760 |
|
| 761 |
<p class="submit"> |
| 762 |
<?php $key = get_option( 'etsy_shop_api_key' ); |
| 763 |
wp_nonce_field( 'etsy-shop-update-settings_'.$key ); ?> |
| 764 |
<input type="submit" name="submit" id="submit" class="button-primary" value="<?php _e( 'Save Changes', 'etsy-shop' ); ?>" /> |
| 765 |
</p> |
| 766 |
|
| 767 |
</form> |
| 768 |
</div> |
| 769 |
<?php |
| 770 |
} |
| 771 |
|
| 772 |
// admin warning |
| 773 |
if ( is_admin() ) { |
| 774 |
etsy_shop_warning(); |
| 775 |
} |
| 776 |
|
| 777 |
function etsy_shop_warning() { |
| 778 |
if ( !get_option( 'etsy_shop_api_key' ) ) { |
| 779 |
function etsy_shop__api_key_warning() { |
| 780 |
echo "<div id='etsy-shop-warning' class='updated fade'><p><strong>".__( 'Etsy Shop is almost ready.', 'etsy-shop' )."</strong> ".sprintf( __( 'You must <a href="%1$s">enter your Etsy API key</a> for it to work.', 'etsy-shop' ), 'options-general.php?page=etsy-shop.php' )."</p></div>"; |
| 781 |
} |
| 782 |
|
| 783 |
add_action( 'admin_notices', 'etsy_shop__api_key_warning' ); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
function etsy_shop_plugin_action_links( $links, $file ) { |
| 788 |
if ( $file == plugin_basename( dirname( __FILE__ ).'/etsy-shop.php' ) ) { |
| 789 |
$links[] = '<a href="' . admin_url( 'options-general.php?page=etsy-shop.php' ) . '">'.__( 'Settings' ).'</a>'; |
| 790 |
} |
| 791 |
|
| 792 |
return $links; |
| 793 |
} |
| 794 |
|