', $attributes['id'], $attributes['id']),
'widget_call' => ''
);
}
function _ecwid_get_single_product_widget_parts_v2($attributes) {
$price_location_attributes = ' data-spw-price-location="button"';
$bordered_class = ' ecwid-SingleProduct-v2-bordered';
if ($attributes['show_border'] == 0) {
$bordered_class = '';
}
if ($attributes['show_price_on_button'] == 0) {
$price_location_attributes = '';
}
return array(
'display_items' => array(
'picture' => '
', $attributes['id'], $attributes['id']),
'widget_call' => ''
);
}
function ecwid_shortcode($attributes)
{
$custom_renderer = apply_filters( 'ecwid_shortcode_custom_renderer', null );
if ( $custom_renderer ) {
$result = call_user_func( $custom_renderer, array( 'attributes' => $attributes ) );
if ( $result ) {
return $result;
}
}
$defaults = ecwid_get_default_pb_size();
$attributes = shortcode_atts(
array(
'widgets' => 'productbrowser',
'categories_per_row' => '3',
'category_view' => 'grid',
'search_view' => 'grid',
'grid' => $defaults['grid_rows'] . ',' . $defaults['grid_columns'],
'list' => $defaults['list_rows'],
'table' => $defaults['table_rows'],
'minicart_layout' => 'MiniAttachToProductBrowser',
'default_category_id' => 0,
'default_product_id' => 0,
'lang' => '',
'no_html_catalog' => 0,
'default_page' => ''
)
, $attributes
);
$allowed_widgets = array('productbrowser', 'search', 'categories', 'minicart');
$widgets = preg_split('![^0-9^a-z^A-Z^-^_]!', $attributes['widgets']);
foreach ($widgets as $key => $widget) {
if (!in_array($widget, $allowed_widgets)) {
unset($widgets[$key]);
}
}
if (empty($widgets)) {
$widgets = array('productbrowser');
}
$attributes['layout'] = $attributes['minicart_layout'];
$attributes['is_ecwid_shortcode'] = true;
if( !empty($attributes['default_page']) ) {
$attributes['no_html_catalog'] = 1;
}
$result = '';
$widgets_order = array('minicart', 'search', 'categories', 'productbrowser');
foreach ($widgets_order as $widget) {
if (in_array($widget, $widgets)) {
if ( class_exists( 'Ecwid_Shortcode_' . $widget ) ) {
$class = 'Ecwid_Shortcode_' . $widget;
$shortcode = new $class($attributes);
$result .= $shortcode->render();
} else {
$result .= call_user_func_array( 'ecwid_' . $widget . '_shortcode', array( $attributes ) );
}
}
}
update_option('ecwid_store_shortcode_used', time());
return $result;
}
function ecwid_parse_escaped_fragment($escaped_fragment) {
static $parsed = array();
if (empty($parsed[$escaped_fragment])) {
$fragment = urldecode( $escaped_fragment );
$return = array();
if ( preg_match( '/^(\/~\/)([a-z]+)\/(.*)$/', $fragment, $matches ) ) {
parse_str( $matches[3], $return );
$return['mode'] = $matches[2];
} elseif ( preg_match( '!.*/(p|c)/([0-9]*)!', $fragment, $matches ) ) {
if ( count( $matches ) == 3 && in_array( $matches[1], array( 'p', 'c' ) ) ) {
$return = array(
'mode' => 'p' == $matches[1] ? 'product' : 'category',
'id' => $matches[2]
);
}
}
$parsed[$escaped_fragment] = $return;
}
return $parsed[$escaped_fragment];
}
function ecwid_ajax_get_product_info() {
$id = sanitize_text_field( $_GET['id'] );
$product = Ecwid_Product::get_by_id($id);
echo json_encode($product);
exit();
}
function ecwid_store_activate() {
Ecwid_Config::load_from_ini();
$my_post = array();
$defaults = ecwid_get_default_pb_size();
$shortcode = Ecwid_Shortcode_Base::get_current_store_shortcode_name();
$content = <<
$content
EOT;
add_option("ecwid_store_page_id", '', '', 'yes');
add_option("ecwid_store_id", ecwid_get_demo_store_id(), '', 'yes');
add_option("ecwid_enable_minicart", 'Y', '', 'yes');
add_option("ecwid_show_categories", '', '', 'yes');
add_option("ecwid_show_search_box", '', '', 'yes');
add_option("ecwid_pb_categoriesperrow", '3', '', 'yes');
add_option("ecwid_pb_productspercolumn_grid", $defaults['grid_rows'], '', 'yes');
add_option("ecwid_pb_productsperrow_grid", $defaults['grid_columns'], '', 'yes');
add_option("ecwid_pb_productsperpage_list", $defaults['list_rows'], '', 'yes');
add_option("ecwid_pb_productsperpage_table", $defaults['table_rows'], '', 'yes');
add_option("ecwid_pb_defaultview", 'grid', '', 'yes');
add_option("ecwid_pb_searchview", 'list', '', 'yes');
add_option("ecwid_mobile_catalog_link", '', '', 'yes');
add_option("ecwid_default_category_id", '', '', 'yes');
add_option('ecwid_show_vote_message', true);
add_option("ecwid_sso_secret_key", '', '', 'yes');
add_option("ecwid_installation_date", time());
/* All new options should go to check_version thing */
require_once ECWID_PLUGIN_DIR . 'includes/class-ecwid-nav-menus.php';
$id = get_option("ecwid_store_page_id");
$_tmp_page = null;
if (!empty($id) and ($id > 0)) {
$_tmp_page = get_post($id);
}
if ( is_null( $_tmp_page ) && get_option( Ecwid_Store_Page::OPTION_LAST_STORE_PAGE_ID ) ) {
$id = get_option( Ecwid_Store_Page::OPTION_LAST_STORE_PAGE_ID );
if (
Ecwid_Store_Page::post_content_has_productbrowser($id)
&& get_post_status($id) == 'draft') {
$_tmp_page = get_post($id);
}
}
if (is_null($_tmp_page)) {
$id = get_option('ecwid_store_page_id_auto');
if (!empty($id) and ($id > 0)) {
$_tmp_page = get_post($id);
}
}
if ($_tmp_page !== null) {
$my_post = array();
$my_post['ID'] = $id;
$my_post['post_status'] = 'publish';
wp_update_post( $my_post );
if ($id == get_option('ecwid_store_page_id_auto')) {
update_option('ecwid_store_page_id', $id);
}
} else {
ecwid_load_textdomain();
$my_post['post_title'] = __('Store', 'ecwid-shopping-cart');
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
$my_post['post_type'] = 'page';
$my_post['comment_status'] = 'closed';
$id = wp_insert_post( $my_post );
update_option('ecwid_store_page_id', $id);
Ecwid_Nav_Menus::replace_auto_added_menu();
if (ecwid_get_theme_identification() == 'responsive') {
update_post_meta($id, '_wp_page_template', 'full-width-page.php');
update_option("ecwid_show_search_box", 'Y');
}
}
Ecwid_Nav_Menus::add_menu_on_activate();
$p = new Ecwid_Products();
$p->enable_all_products();
Ecwid_Message_Manager::enable_message('on_activate');
Ecwid_Config::load_from_ini();
}
add_action( 'activated_plugin', 'ecwid_plugin_activation_redirect' );
function ecwid_plugin_activation_redirect( $plugin ) {
$is_bulk_activation = isset($_POST['action'])
&& $_POST['action'] == 'activate-selected'
&& isset($_POST['checked'])
&& count($_POST['checked']) > 1;
$is_cli_running = Ecwid_Config::is_cli_running();
$is_newbie = ecwid_is_demo_store();
if( !$is_cli_running && !$is_bulk_activation && $is_newbie && $plugin == plugin_basename( __FILE__ ) ) {
exit( wp_safe_redirect( Ecwid_Admin::get_dashboard_url() ) );
}
}
add_action('in_admin_header', 'ecwid_disable_other_notices');
function ecwid_disable_other_notices() {
$is_admin_subpage = strpos(get_current_screen()->base, Ecwid_Admin::ADMIN_SLUG) !== false;
if (!$is_admin_subpage) return;
global $wp_filter;
if (!$wp_filter || !isset($wp_filter['admin_notices']) || !class_exists('WP_Hook') || ! ( $wp_filter['admin_notices'] instanceof WP_Hook) ) {
return;
}
foreach ($wp_filter['admin_notices']->callbacks as $priority => $collection) {
foreach ($collection as $name => $item) {
if ($name != 'ecwid_show_admin_messages') {
remove_action('admin_notices', $item['function'], $priority);
}
}
}
}
function ecwid_show_admin_messages() {
if (is_admin()) {
global $wp_filter;
if ( $wp_filter && isset($wp_filter['admin_notices']) && class_exists('WP_Hook') ){
foreach ($wp_filter['admin_notices']->callbacks as $priority => $collection) {
foreach ($collection as $name => $item) {
if( is_array($item) && is_array($item['function']) && is_object($item['function'][0]) && get_class($item['function'][0]) == 'Storefront_NUX_Admin' ){
remove_action('admin_notices', $item['function'], $priority);
}
}
}
}
Ecwid_Message_Manager::show_messages();
}
}
function ecwid_show_admin_message($message) {
$class = version_compare(get_bloginfo('version'), '3.0') < 0 ? "updated fade" : "update-nag";
echo sprintf('%s
', $class, $message);
}
function ecwid_store_deactivate() {
$ecwid_page_id = get_option("ecwid_store_page_id");
$_tmp_page = null;
if (!empty($ecwid_page_id) and ($ecwid_page_id > 0)) {
$_tmp_page = get_page($ecwid_page_id);
if ($_tmp_page !== null) {
$my_post = array();
$my_post['ID'] = $ecwid_page_id;
$my_post['post_status'] = 'draft';
update_option(Ecwid_Store_Page::OPTION_LAST_STORE_PAGE_ID, $ecwid_page_id);
wp_update_post( $my_post );
} else {
update_option('ecwid_store_page_id', '');
}
}
$p = new Ecwid_Products();
$p->disable_all_products();
}
function ecwid_uninstall() {
delete_option("ecwid_store_page_id_auto");
delete_option("ecwid_store_id");
delete_option("ecwid_enable_minicart");
delete_option("ecwid_show_categories");
delete_option("ecwid_show_search_box");
delete_option("ecwid_pb_categoriesperrow");
delete_option("ecwid_pb_productspercolumn_grid");
delete_option("ecwid_pb_productsperrow_grid");
delete_option("ecwid_pb_productsperpage_list");
delete_option("ecwid_pb_productsperpage_table");
delete_option("ecwid_pb_defaultview");
delete_option("ecwid_pb_searchview");
delete_option("ecwid_mobile_catalog_link");
delete_option("ecwid_default_category_id");
delete_option('ecwid_show_vote_message');
delete_option("ecwid_sso_secret_key");
delete_option("ecwid_installation_date");
delete_option('ecwid_hide_appearance_menu');
delete_option("ecwid_plugin_version");
delete_option("ecwid_use_chameleon");
delete_option(Ecwid_Api_V3::TOKEN_OPTION_NAME);
EcwidPlatform::cache_reset('need_add_rewrite');
Ecwid_Store_Page::delete_page_from_nav_menus();
}
function ecwid_abs_intval($value) {
if (!is_null($value))
return abs(intval($value));
else
return null;
}
function ecwid_get_update_params_options() {
$options = array(
'ecwid_store_id' => array(
'type' => 'string'
),
'ecwid_store_page_id' => array(
'type' => 'string'
),
'ecwid_ajax_defer_rendering' => array(
'values' => array(
'on',
'off',
'auto'
)
),
'ecwid_disable_dashboard' => array(
'values' => array(
'on',
'off',
''
)
),
'ecwid_disable_pb_url' => array(
'type' => 'bool'
),
Ecwid_Nav_Menus::OPTION_USE_JS_API_FOR_CATS_MENU => array(
'values' => array(
Ecwid_Nav_Menus::OPTVAL_USE_JS_API_FOR_CATS_MENU_TRUE,
Ecwid_Nav_Menus::OPTVAL_USE_JS_API_FOR_CATS_MENU_FALSE,
Ecwid_Nav_Menus::OPTVAL_USE_JS_API_FOR_CATS_MENU_AUTO
)
),
Ecwid_Widget_Floating_Shopping_Cart::OPTION_MOVE_INTO_BODY => array(
'type' => 'bool',
),
'ecwid_historyjs_html4mode' => array(
'type' => 'bool'
),
'ecwid_plugin_migration_since_version' => array(
'type' => 'string'
),
'ecwid_seo_links_enabled' => array(
'type' => 'bool'
),
Ecwid_Admin::OPTION_ENABLE_AUTO_MENUS => array(
'values' => array(
Ecwid_Admin::OPTION_ENABLE_AUTO_MENUS_ON,
Ecwid_Admin::OPTION_ENABLE_AUTO_MENUS_OFF,
Ecwid_Admin::OPTION_ENABLE_AUTO_MENUS_AUTO
)
),
'ecwid_print_html_catalog' => array(
'type' => 'bool'
),
'ecwid_hide_prefetch' => array(
'values' => array(
'on',
'off',
'auto'
)
),
Ecwid_Static_Page::OPTION_IS_ENABLED => array(
'values' => array(
Ecwid_Static_Page::OPTION_VALUE_AUTO,
Ecwid_Static_Page::OPTION_VALUE_ENABLED,
Ecwid_Static_Page::OPTION_VALUE_DISABLED
)
),
'ecwid_api_status' => array(
'type' => 'string'
),
'ecwid_hide_canonical' => array(
'values' => array(
'',
'hide'
)
),
Ecwid_Store_Page::OPTION_REPLACE_TITLE => array(
'values' => array(
'',
'Y'
)
),
Ecwid_Products::OPTION_ENABLED => array(
'values' => array(
'',
'1'
)
),
);
if( class_exists('woocommerce') && class_exists('Ecwid_Importer') ) {
$options[ Ecwid_Importer::OPTIONS_SEPARATE_IMAGE_LOADING ] = array(
'values' => array(
'',
'Y'
)
);
}
return $options;
}
function ecwid_get_update_params_action() {
return 'ecwid-update-params';
}
function ecwid_params_do_page() {
include ECWID_PLUGIN_DIR . 'templates/admin-params.php';
}
add_action('admin_post_' . ecwid_get_update_params_action(), 'ecwid_update_plugin_params');
function ecwid_update_plugin_params()
{
if ( !current_user_can('administrator') ) {
header('403 Access Denied');
die();
}
if ( !wp_verify_nonce(@$_POST['nonce'], ecwid_get_update_params_action() ) ) {
header('403 Access Denied');
die();
}
$options = ecwid_get_update_params_options();
$options4update = array();
foreach ( $options as $key => $option ) {
if ( isset($option['type']) && $option['type'] == 'html' ) {
$options4update[$key] = html_entity_decode( @$_POST['option'][$key] );
} else {
$options4update[$key] = @$_POST['option'][$key];
}
}
foreach ($options4update as $name => $value) {
update_option($name, $value);
}
wp_safe_redirect('admin.php?page=ec-params');
exit();
}
function ecwid_get_clear_all_cache_action() {
return 'ec-clear-all-cache';
}
add_action('plugins_loaded', 'ecwid_clear_all_cache');
function ecwid_clear_all_cache()
{
if ( array_key_exists( ecwid_get_clear_all_cache_action(), $_GET ) ) {
ecwid_full_cache_reset();
if ( array_key_exists( 'redirect_back', $_GET ) ) {
wp_safe_redirect ( 'admin.php?page=ec-params' );
exit();
}
}
}
function ecwid_sync_do_page() {
require_once ECWID_PLUGIN_DIR . 'includes/class-ecwid-products.php';
$prods = new Ecwid_Products();
$estimation = $prods->estimate_sync();
require_once ECWID_PLUGIN_DIR . 'templates/sync.php';
}
function ecwid_reset_categories_cache()
{
if (!current_user_can('manage_options')) {
return;
}
EcwidPlatform::cache_reset( 'nav_categories_posts' );
EcwidPlatform::cache_reset( 'all_categories' );
EcwidPlatform::invalidate_categories_cache_from();
}
add_action( 'tool_box', 'ecwid_add_toolbox' );
function ecwid_add_toolbox() {
require ECWID_PLUGIN_DIR . 'templates/wp-toolbox.tpl.php';
}
function ecwid_register_admin_styles($hook_suffix) {
wp_enqueue_style('ecwid-admin-css', ECWID_PLUGIN_URL . 'css/admin.css', array(), get_option('ecwid_plugin_version'));
wp_enqueue_style('ecwid-fonts-css', ECWID_PLUGIN_URL . 'css/fonts.css', array(), get_option('ecwid_plugin_version'));
wp_enqueue_style('ecwid-opensans', 'https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,300', array(), get_option('ecwid_plugin_version'));
if (isset($_GET['page']) && strpos($_GET['page'], 'ec-store') === 0) {
$is_reconnect = isset($_GET['page']) && $_GET['page'] == Ecwid_Admin::ADMIN_SLUG && isset($_GET['reconnect']);
$is_connection_error = Ecwid_Admin_Main_Page::is_connection_error();
// Can't really remember why it checks against the raw version, not the sanitized one; consider refactoring
if ( ecwid_is_demo_store( get_option( 'ecwid_store_id' ) ) || !get_option( 'ecwid_store_id' ) || $is_reconnect || $is_connection_error || Ecwid_Api_V3::get_token() == false ) {
if( class_exists('Ecwid_Admin') && isset($_GET['page']) && $_GET['page'] != Ecwid_Admin::ADMIN_SLUG ) {
return;
}
wp_enqueue_script('ecwid-welcome-page-js', ECWID_PLUGIN_URL . 'js/welcome-page.js', array(), get_option('ecwid_plugin_version'));
wp_localize_script('ecwid-welcome-page-js', 'ecwidParams', array(
'registerLink' => ecwid_get_register_link(),
'isWL' => Ecwid_Config::is_wl()
)
);
wp_enqueue_style('ecwid-welcome-page-css', ECWID_PLUGIN_URL . 'css/welcome-page.css', array(), get_option('ecwid_plugin_version'), 'all');
} else {
// We already connected and disconnected the store, no need for fancy landing
wp_enqueue_script('ecwid-connect-js', ECWID_PLUGIN_URL . 'js/dashboard.js', array(), get_option('ecwid_plugin_version'));
}
}
}
function ecwid_register_settings_styles($hook_suffix) {
if ( ($hook_suffix != 'post.php' && $hook_suffix != 'post-new.php') && strpos( $hook_suffix, Ecwid_Admin::ADMIN_SLUG ) === false) return;
wp_enqueue_style('ecwid-settings-css', ECWID_PLUGIN_URL . 'css/settings.css', array(), get_option('ecwid_plugin_version'), 'all');
if (version_compare(get_bloginfo('version'), '3.8-beta') > 0) {
wp_enqueue_style('ecwid-settings38-css', ECWID_PLUGIN_URL . 'css/settings.3.8.css', array('ecwid-settings-css'), '', 'all');
}
}
function ecwid_plugin_actions($links) {
$settings_link = ""
. ( ecwid_is_demo_store() ? __('Setup', 'ecwid-shopping-cart') : __('Settings') )
. "";
array_unshift( $links, $settings_link );
return $links;
}
function ecwid_settings_api_init() {
if ( isset( $_POST['settings_section'] ) ) {
switch ( $_POST['settings_section'] ) {
case 'general':
register_setting( 'ecwid_options_page', 'ecwid_store_id', 'ecwid_abs_intval' );
if ( isset( $_POST['ecwid_store_id'] ) && intval( $_POST['ecwid_store_id'] ) == 0 ) {
Ecwid_Message_Manager::reset_hidden_messages();
}
break;
case 'advanced':
register_setting( 'ecwid_options_page', 'ecwid_default_category_id', 'ecwid_abs_intval' );
register_setting( 'ecwid_options_page', 'ecwid_sso_secret_key' );
register_setting( 'ecwid_options_page', 'ecwid_is_sso_enabled' );
break;
}
if ($_POST['settings_section'] == 'advanced' && isset($_POST[Ecwid_Products::OPTION_ENABLED]) && !Ecwid_Products::is_enabled()) {
Ecwid_Products::enable();
} else if ($_POST['settings_section'] == 'advanced' && !isset($_POST[Ecwid_Products::OPTION_ENABLED]) && Ecwid_Products::is_enabled()) {
Ecwid_Products::disable();
}
if ($_POST['settings_section'] == 'advanced' && !@$_POST['ecwid_is_sso_enabled']) {
update_option('ecwid_sso_secret_key', '');
}
}
if ( isset( $_POST['ecwid_store_id'] ) ) {
ecwid_update_store_id( $_POST['ecwid_store_id'] );
update_option('ecwid_last_oauth_fail_time', 0);
update_option('ecwid_connected_via_legacy_page_time', time());
}
}
function ecwid_common_admin_scripts() {
wp_enqueue_script('ecwid-admin-js', ECWID_PLUGIN_URL . 'js/admin.js', array(), get_option('ecwid_plugin_version'));
wp_enqueue_script('ecwid-modernizr-js', ECWID_PLUGIN_URL . 'js/modernizr.js', array(), get_option('ecwid_plugin_version'));
wp_localize_script('ecwid-admin-js', 'ecwid_params', array(
'dashboard' => __('Dashboard', 'ecwid-shopping-cart'),
'dashboard_url' => Ecwid_Admin::get_relative_dashboard_url(),
'products' => __('Products', 'ecwid-shopping-cart'),
'products_url' => Ecwid_Admin::get_relative_dashboard_url() . '-admin-products',
'orders' => __('Orders', 'ecwid-shopping-cart'),
'orders_url' => Ecwid_Admin::get_relative_dashboard_url() . '-admin-orders',
'reset_cats_cache' => __('Refresh categories list', 'ecwid-shopping-cart'),
'cache_updated' => __('Done', 'ecwid-shopping-cart'),
'reset_cache_message' => __('The store top-level categories are automatically added to this drop-down menu', 'ecwid-shopping-cart'),
'store_shortcodes' => Ecwid_Shortcode_Base::get_store_shortcode_names(),
'store_shortcode' => Ecwid_Shortcode_Base::get_current_store_shortcode_name(),
'product_shortcode' => Ecwid_Shortcode_Product::get_shortcode_name(),
'legacy_appearance' => ecwid_is_legacy_appearance_used(),
'is_demo_store' => ecwid_is_demo_store() || is_admin() && isset($_GET['reconnect'])
));
}
function ecwid_is_legacy_appearance_used() {
$api = new Ecwid_Api_V3();
return Ecwid_Api_V3::is_available() && !ecwid_is_demo_store() && !$api->is_store_feature_enabled( Ecwid_Api_V3::FEATURE_NEW_PRODUCT_LIST );
}
function ecwid_get_register_link()
{
$link = Ecwid_Config::get_registration_url();
if ( empty( $link ) ) {
$link = 'https://' . Ecwid_Config::get_cpanel_domain();
}
if ( strpos($link, '?') ) {
$link .= '&';
} else {
$link .= '?';
}
$link .= 'partner='
. Ecwid_Config::get_channel_id()
. '%s#register';
$user_data = '';
/*
$current_user = wp_get_current_user();
if ($current_user->ID && function_exists('get_user_meta')) {
$meta = get_user_meta($current_user->ID);
$data = array(
'name' => get_user_meta($current_user->ID, 'first_name', true) . ' ' . get_user_meta($current_user->ID, 'last_name', true),
'nickname' => $current_user->display_name,
'email' => $current_user->user_email
);
foreach ($data as $key => $value) {
if (trim($value) == '') {
unset($data[$key]);
}
}
$user_data = '&' . build_query($data);
}
*/
$link = sprintf($link, $user_data);
return $link;
}
function ecwid_is_demo_store( $store_id = null ) {
if ( is_null( $store_id ) ) {
$store_id = get_ecwid_store_id();
}
$config_id = Ecwid_Config::get_demo_store_id();
if ( $config_id == $store_id ) return $config_id;
return in_array( $store_id, ecwid_get_demo_stores() );
}
function ecwid_get_demo_store_id() {
$config_id = Ecwid_Config::get_demo_store_id();
if ( $config_id ) return $config_id;
$demo_stores = ecwid_get_demo_stores();
$locale = get_locale();
if ( strpos( $locale, 'ru' ) === 0 ) {
return $demo_stores['locale_ru'];
} else {
return $demo_stores['locale_other'];
}
}
function ecwid_get_demo_stores() {
return $demo_stores = array(
'legacy' => 1003,
'locale_ru' => 13437191,
'locale_other' => 13433173
);
}
function ecwid_get_demo_store_public_key() {
$public_keys = array(
13437191 => 'public_LRaZBiDigZCMJquHkRWDpdAt4HPmb4zH',
13433173 => 'public_9EYLuZ15kfKEHdpsuKMsqp9MZ2Umxtcp'
);
$store_id = ecwid_get_demo_store_id();
if( isset($public_keys[$store_id]) ) {
return $public_keys[$store_id];
}
return false;
}
function ecwid_create_store() {
$api = new Ecwid_Api_V3();
$result = $api->create_store();
if ( is_array( $result ) && $result['response']['code'] == 200 ) {
$data = json_decode( $result['body'] );
ecwid_update_store_id($data->id);
$api->save_token( $data->token );
update_option( 'ecwid_oauth_scope', 'read_profile ' . Ecwid_OAuth::SCOPE_READ_CATALOG . ' create_catalog update_catalog allow_sso create_customers public_storefront' );
header( 'HTTP/1.1 200 OK' );
} else {
if( is_wp_error( $result ) ) {
header( 'HTTP/1.1 409 Error' );
die();
}
header( 'HTTP/1.1 ' . $result['response']['code'] . ' ' . $result['response']['message'] );
die();
}
}
add_action('admin_post_ecwid-do-sso', 'ecwid_do_sso_redirect');
function ecwid_do_sso_redirect() {
if ( !current_user_can('manage_options') ) {
die();
}
$url = ecwid_get_admin_sso_url( time() );
wp_redirect( $url );
exit();
}
function ecwid_get_admin_sso_url( $time, $page = '' ) {
$oauth = new Ecwid_Oauth();
if ( !Ecwid_Api_V3::get_token() ) {
return false;
}
$lang = ecwid_get_current_user_locale();
return sprintf(
'https://' . Ecwid_Config::get_api_domain() . '/api/v3/%s/sso?token=%s×tamp=%s&signature=%s&place=%s&lang=%s',
get_ecwid_store_id(),
Ecwid_Api_V3::get_token(),
$time,
hash( 'sha256', get_ecwid_store_id() . Ecwid_Api_V3::get_token() . $time . Ecwid_Config::get_oauth_appsecret() ),
urlencode( $page ),
substr( $lang, 0, 2 )
);
}
function ecwid_get_iframe_src($time, $page)
{
$url = ecwid_get_admin_sso_url($time, $page);
if ($url) {
$url .= '&inline&min-height=700';
if ( Ecwid_Admin::are_auto_menus_enabled() ) {
$url .= '&hide_vertical_navigation_menu=true';
}
$url .= '&hide_staff_accounts_header_menu=true';
$url .= '&hide_header=true';
$url .= '&set_dashboard_website_section_type=wordpress';
$url .= '&website_manage_url=' . urlencode( admin_url( 'admin.php?page=ec-storefront-settings' ) );
return $url;
} else {
return false;
}
}
function ecwid_admin_products_do_page() {
Ecwid_Admin_Main_Page::do_integrated_admin_page(
Ecwid_Admin_Main_Page::PAGE_HASH_PRODUCTS
);
}
function ecwid_admin_orders_do_page() {
Ecwid_Admin_Main_Page::do_integrated_admin_page(
Ecwid_Admin_Main_Page::PAGE_HASH_ORDERS
);
}
function ecwid_admin_mobile_do_page() {
Ecwid_Admin_Main_Page::do_integrated_admin_page(
Ecwid_Admin_Main_Page::PAGE_HASH_MOBILE
);
}
function ecwid_help_do_page() {
$help = new Ecwid_Help_Page();
$faqs = $help->get_faqs();
wp_enqueue_style('ecwid-help', ECWID_PLUGIN_URL . 'css/help.css',array(), get_option('ecwid_plugin_version'));
$col_size = 6;
require_once ECWID_PLUGIN_DIR . 'templates/help.php';
}
function ecwid_process_oauth_params() {
if (strtoupper($_SERVER['REQUEST_METHOD']) != 'GET' || !isset($_GET['page'])) {
return false;
}
$is_dashboard = $_GET['page'] == Ecwid_Admin::ADMIN_SLUG;
if (!$is_dashboard) {
return false;
}
global $ecwid_oauth;
$is_connect = ecwid_is_demo_store() && !isset($_GET['connection_error']);
$is_reconnect = isset($_GET['reconnect']) && !isset($_GET['connection_error']);
if ($is_connect) {
$ecwid_oauth->update_state( array( 'mode' => 'connect' ) );
}
if ($is_reconnect && !isset($_GET['api_v3_sso'])) {
$ecwid_oauth->update_state( array(
'mode' => 'reconnect',
// explicitly set to empty array if not available to reset current state
'scope' => isset($_GET['scope']) ? $_GET['scope'] : array(),
// explicitly set to empty string if not available to reset current state
'return_url' => isset($_GET['return-url']) ? $_GET['return-url'] : '',
'reason' => isset($_GET['reason']) ? $_GET['reason'] : ''
));
if ( @$_GET['do_reconnect'] ) {
wp_redirect( $ecwid_oauth->get_auth_dialog_url() );
}
}
return true;
}
function ecwid_admin_post_connect()
{
if (!current_user_can('manage_options')) {
return;
}
if (isset($_GET['force_store_id'])) {
update_option('ecwid_store_id', $_GET['force_store_id']);
update_option('ecwid_api_check_retry_after', 0);
update_option('ecwid_last_oauth_fail_time', 1);
wp_safe_redirect( Ecwid_Admin::get_dashboard_url() );
exit();
}
global $ecwid_oauth;
if (ecwid_test_oauth(true)) {
if (@isset($_GET['api_v3_sso'])) {
$ecwid_oauth->update_state(array('mode' => 'reconnect', 'return_url' => Ecwid_Admin::get_dashboard_url() . '-advanced' ));
wp_redirect($ecwid_oauth->get_sso_reconnect_dialog_url());
} else {
wp_redirect( $ecwid_oauth->get_auth_dialog_url() );
}
} else if (!isset($_GET['reconnect'])) {
wp_safe_redirect(Ecwid_Admin::get_dashboard_url() . '&oauth=no');
} else {
wp_safe_redirect(Ecwid_Admin::get_dashboard_url() . '&reconnect&connection_error');
}
exit();
}
function ecwid_test_oauth($force = false)
{
global $ecwid_oauth;
$last_fail = get_option('ecwid_last_oauth_fail_time');
if ( ($last_fail > 0 && $last_fail + 60*60*24 < time()) || $force) {
$result = $ecwid_oauth->test_post();
if ($result) {
update_option('ecwid_last_oauth_fail_time', $last_fail = 0);
} else {
update_option('ecwid_last_oauth_fail_time', $last_fail = time());
}
}
return $last_fail == 0;
}
function ecwid_get_categories_for_selector() {
$cached = EcwidPlatform::get_from_categories_cache( 'ecwid_categories_for_selector' );
if ( $cached ) {
return $cached;
}
$query_params = array(
'hidden_categories' => true
);
if( ecwid_is_demo_store() ) {
$query_params['parent'] = 0;
}
$api = new Ecwid_Api_V3();
$categories = $api->get_categories( $query_params );
$all_categories = array();
if ( !$categories || @$categories->count == 0 ) {
return array();
}
foreach ( $categories->items as $category ) {
$all_categories[$category->id] = $category;
}
if ( $categories->total > $categories->count ) {
$offset = 100;
$page = 0;
while ( $categories->count + $offset * $page < $categories->total ) {
$page++;
$query_params['offset'] = $offset * $page;
$categories = $api->get_categories( $query_params );
foreach ( $categories->items as $category ) {
$all_categories[$category->id] = $category;
}
}
}
$parents = array();
$result = array();
foreach ( $all_categories as $category ) {
if( !isset( $category->name ) ) {
continue;
}
$result[$category->id] = $category;
if ( !isset($category->parentId) ) {
$result[$category->id]->path = $category->name;
} else {
$current_parent_id = $category->parentId;
$path = $category->name;
while ( $current_parent_id != 0 ) {
$parent = $all_categories[$current_parent_id];
$path = $parent->name . ' > ' . $path;
$current_parent_id = isset( $parent->parentId ) ? $parent->parentId : 0;
}
$result[$category->id]->path = $path;
}
}
if (!function_exists('_ecwid_compare_categories')) {
function _ecwid_compare_categories($cat1, $cat2) {
return strcasecmp( $cat1->path, $cat2->path );
}
}
usort( $result, '_ecwid_compare_categories' );
EcwidPlatform::store_in_categories_cache( 'ecwid_categories_for_selector', $result );
return $result;
}
function ecwid_advanced_settings_do_page() {
$is_sso_enabled = ecwid_is_sso_enabled();
global $ecwid_oauth;
$has_create_customers_scope = $ecwid_oauth->has_scope('create_customers');
$key = get_option('ecwid_sso_secret_key');
$is_sso_checkbox_disabled = !$is_sso_enabled && !$has_create_customers_scope && empty($key);
if (!ecwid_is_paid_account()) {
$is_sso_checkbox_disabled = true;
}
$reconnect_link = get_reconnect_link();
require_once ECWID_PLUGIN_DIR . 'templates/advanced-settings.php';
}
function get_reconnect_link() {
return admin_url('admin-post.php?action=ec_connect&reconnect&api_v3_sso');
}
function ecwid_debug_do_page() {
if ( array_key_exists( 'reset_cache', $_GET ) ) {
ecwid_invalidate_cache(true );
}
$api_v3_profile_results = wp_remote_get( 'https://app.ecwid.com/api/v3/' . get_ecwid_store_id() . '/profile?token=' . Ecwid_Api_V3::get_token() );
global $ecwid_oauth;
require_once ECWID_PLUGIN_DIR . 'templates/cache_log.php';
require_once ECWID_PLUGIN_DIR . 'templates/debug.php';
}
function ecwid_get_debug_file() {
if (!current_user_can('manage_options')) {
return;
}
header('Content-Disposition: attachment;filename=ecwid-plugin-log.html');
ecwid_debug_do_page();
die();
}
function get_ecwid_store_id() {
require_once ECWID_PLUGIN_DIR . 'includes/class-ecwid-config.php';
$config_value = Ecwid_Config::get_store_id();
if ($config_value) return $config_value;
$store_id = get_option('ecwid_store_id');
if (empty($store_id)) {
$store_id = ecwid_get_demo_store_id();
}
return $store_id;
}
function ecwid_sync_products() {
set_time_limit(3600);
if (!defined('DOING_AJAX')) {
echo 'Lets begin
';
flush();
}
$p = new Ecwid_Products();
$p->sync();
if (defined('DOING_AJAX') && DOING_AJAX) {
echo 'OK';
wp_die();
} else {
wp_safe_redirect(Ecwid_Admin::get_dashboard_url() . '-advanced');
}
}
function ecwid_sync_progress_callback($status) {
if (!@$status['event']) {
$status['event'] = 'progress';
}
echo 'event: ' . $status['event'] . "\n";
echo 'data: ' . json_encode($status) . "\n\n";
flush();
}
add_action('admin_post_ecwid_sync_sse', 'ecwid_sync_products_sse');
function ecwid_sync_products_sse() {
set_time_limit(0);
header("Content-Type: text/event-stream\n\n");
Ecwid_Products::enable();
$p = new Ecwid_Products();
$p->set_sync_progress_callback('ecwid_sync_progress_callback');
$p->sync();
ecwid_sync_progress_callback(
array(
'event' => 'completed',
'last_update' => ecwid_format_date( $p->get_last_sync_time() )
)
);
}
function ecwid_format_date( $unixtime ) {
return date_i18n( get_option('date_format') . ' ' . get_option('time_format'), $unixtime + get_option( 'gmt_offset' ) * 60 * 60 );
}
function ecwid_slow_sync_progress($status) {
global $ecwid_sync_status;
if (!Ecwid_Products::is_enabled()) {
Ecwid_Products::enable();
}
if (!isset($ecwid_sync_status)) {
$ecwid_sync_status = array(
'limit' => -1,
'offset' => -1,
'total' => -1,
'count' => -1,
'updated' => 0,
'deleted_disabled' => 0,
'created' => 0,
'deleted' => 0,
'skipped_deleted' => 0
);
}
if ($status['event'] == 'fetching_products' || $status['event'] == 'fetching_deleted_product_ids') {
$ecwid_sync_status['offset'] = $status['offset'];
$ecwid_sync_status['limit'] = $status['limit'];
} else if ($status['event'] == 'found_updated' || $status['event'] == 'found_deleted') {
$ecwid_sync_status['total'] = $status['total'];
$ecwid_sync_status['count'] = $status['count'];
} else if ($status['event'] == 'created_product') {
$ecwid_sync_status['created']++;
} else if ($status['event'] == 'updated_product') {
$ecwid_sync_status['updated']++;
} else if ($status['event'] == 'deleted_disabled_product') {
$ecwid_sync_status['deleted_disabled']++;
} else if ($status['event'] == 'deleted_product') {
$ecwid_sync_status['deleted'] ++;
} else if ($status['event'] == 'skipped_deleted') {
$ecwid_sync_status['skipped_deleted']++;
}
}
add_action('admin_post_ecwid_sync_reset', 'ecwid_sync_reset');
function ecwid_sync_reset()
{
EcwidPlatform::set(Ecwid_Products_Sync_Status::OPTION_UPDATE_TIME, 0);
EcwidPlatform::set(Ecwid_Products_Sync_Status::OPTION_LAST_PRODUCT_UPDATE_TIME, 0);
EcwidPlatform::set(Ecwid_Products_Sync_Status::OPTION_LAST_PRODUCT_DELETE_TIME, 0);
wp_safe_redirect( Ecwid_Admin::get_dashboard_url() . '-advanced' );
exit();
}
add_action('admin_post_ecwid_sync_no_sse', 'ecwid_sync_products_no_sse');
function ecwid_sync_products_no_sse() {
$p = new Ecwid_Products();
$p->set_sync_progress_callback('ecwid_slow_sync_progress');
$over = $p->sync(array(
'mode' => $_GET['mode'] == 'deleted' ? 'deleted' : 'updated',
'offset' => intval($_GET['offset']),
'one_at_a_time' => true,
'from' => $_GET['time']
));
global $ecwid_sync_status;
if (!$over) {
echo json_encode($ecwid_sync_status);
} else {
echo json_encode(array_merge($ecwid_sync_status, array('status' => 'complete', 'last_update' => ecwid_format_date( $p->get_last_sync_time() ))));
}
}
add_action('admin_post_ecwid_tick', 'ecwid_tick');
function ecwid_tick() {
var_dump(ini_get('max_execution_time'));
set_time_limit(12345);
var_dump(ini_get('max_execution_time'));
error_log('tick');
header("Content-Type: text/event-stream\n\n");
for ($i = 0; $i < 30; $i++) {
echo "data: $i \n\n";
flush();
sleep(2);
//usleep(2000);
}
die();
}
function ecwid_dashboard_widget_function() {
if (!is_ssl()) {
require_once ECWID_PLUGIN_DIR . 'templates/wp-dashboard-widget.php';
}
}
function ecwid_add_dashboard_widgets() {
if (current_user_can('manage_options')) {
wp_add_dashboard_widget('ecwid_dashboard_widget', __('Recommendations for Your Online Store', 'ecwid-shopping-cart'), 'ecwid_dashboard_widget_function');
}
}
function ecwid_get_store_page_url()
{
static $link = null;
if (is_null($link)) {
$link = get_permalink( Ecwid_Store_Page::get_current_store_page_id() );
}
return $link;
}
function ecwid_is_store_page_available()
{
return Ecwid_Store_Page::get_current_store_page_id() != false;
}
function ecwid_get_product_url($product)
{
return ecwid_get_entity_url($product, 'p');
}
function ecwid_get_category_url($category)
{
return ecwid_get_entity_url($category, 'c');
}
function ecwid_get_entity_url($entity, $type) {
if ( Ecwid_Store_page::is_store_page() ) {
$link = get_permalink();
} else {
$link = Ecwid_Store_Page::get_store_url();
}
if ( is_object( $entity ) ) {
// If a newer object is passed, fall back to default hash url
$entity = $entity->id;
}
if (is_numeric($entity)) {
return $link . '#!/' . $type . '/' . $entity;
} elseif (is_array($entity) && isset($entity['url'])) {
$link .= substr($entity['url'], strpos($entity['url'], '#'));
}
return $link;
}
function ecwid_get_product_browser_url_script()
{
if ( get_option('ecwid_disable_pb_url' ) ) {
return;
}
$str = '';
if (ecwid_is_store_page_available() && !Ecwid_Store_Page::is_store_page()) {
$url = esc_js( Ecwid_Store_Page::get_store_url() );
$str = <<
window.ec = window.ec || Object();
window.ec.config = window.ec.config || Object();
window.ec.config.store_main_page_url = '$url';
HTML;
}
return $str;
}
function ecwid_get_wp_install_date( ) {
global $wpdb;
$wp_date = get_option( 'ecwid_wp_install_date' );
if ( ! $wp_date ) {
global $wpdb;
$oldest_user = strtotime( $wpdb->get_var( "SELECT min(`user_registered`) FROM {$wpdb->users}" ) );
$oldest_post = strtotime( $wpdb->get_var( "SELECT min(`post_date`) FROM {$wpdb->posts}" ) );
$wpconfig_create = @filectime( ABSPATH . '/wp-config.php' );
$wp_date = min( $oldest_user, $oldest_post, $wpconfig_create );
update_option( 'ecwid_wp_install_date', $wp_date );
}
return $wp_date;
}
function ecwid_check_for_remote_connection_errors()
{
global $ecwid_oauth;
$results = array();
$results['https_post_error'] = wp_remote_post($ecwid_oauth->get_test_post_url());
foreach ($results as $type => $value) {
if (is_wp_error($value)) {
$results[$type] = $value->get_error_message();
} else {
unset($results[$type]);
}
}
return $results;
}
function ecwid_is_sso_enabled() {
global $ecwid_oauth;
$is_sso_enabled = false;
$is_apiv3_sso = ecwid_is_paid_account() && get_option('ecwid_is_sso_enabled') && $ecwid_oauth && $ecwid_oauth->has_scope('create_customers');
$is_apiv1_sso = ecwid_is_paid_account() && get_option('ecwid_sso_secret_key');
$is_sso_enabled = $is_apiv3_sso || $is_apiv1_sso;
return $is_sso_enabled;
}
add_action( 'send_headers', 'ecwid_add_headers' );
function ecwid_add_headers() {
if ( wp_get_current_user()->ID && ecwid_is_sso_enabled() ) {
header("Cache-Control: private, must-revalidate, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
}
}
function ecwid_sso() {
global $ecwid_sso_script;
if ( !ecwid_is_sso_enabled() ) return;
if ( !empty($ecwid_sso_script) ) return;
$current_user = wp_get_current_user();
$signin_url = wp_login_url(Ecwid_Store_Page::get_store_url());
$signout_url = wp_logout_url(Ecwid_Store_Page::get_store_url());
$sign_in_out_urls = <<ID) {
$meta = get_user_meta($current_user->ID);
$name = $meta['first_name'][0] . ' ' . $meta['last_name'][0];
if ($name == ' ') {
$name = $meta['nickname'][0];
}
$user_data = array(
'userId' => "{$current_user->ID}",
'profile' => array(
'email' => $current_user->user_email,
'billingPerson' => array(
'name' => $name
)
)
);
global $ecwid_oauth;
if ($ecwid_oauth->has_scope('create_customers')) {
$key = Ecwid_Config::get_oauth_appsecret();
$user_data['appClientId'] = Ecwid_Config::get_oauth_appid();
} else {
$key = get_option('ecwid_sso_secret_key');
$user_data['appId'] = "wp_" . get_ecwid_store_id();
}
$user_data_encoded = base64_encode(json_encode($user_data));
$time = time();
$hmac = ecwid_hmacsha1("$user_data_encoded $time", $key);
$ecwid_sso_profile = "$user_data_encoded $hmac $time";
}
$ecwid_sso_script = <<
var ecwid_sso_profile='$ecwid_sso_profile';
$sign_in_out_urls
jQuery(document).ready(function() {
if (typeof Ecwid == 'undefined') return;
Ecwid.OnPageLoad.add(function(page) {
if (page.type == 'SIGN_IN' && ecwid_sso_profile == '') {
location.href = '$signin_url';
}
})
}
);
HTML;
return $ecwid_sso_script;
}
// from: http://www.php.net/manual/en/function.sha1.php#39492
function ecwid_hmacsha1($data, $key) {
if (function_exists("hash_hmac")) {
return hash_hmac('sha1', $data, $key);
} else {
$blocksize=64;
$hashfunc='sha1';
if (strlen($key)>$blocksize)
$key=pack('H*', $hashfunc($key));
$key=str_pad($key,$blocksize,chr(0x00));
$ipad=str_repeat(chr(0x36),$blocksize);
$opad=str_repeat(chr(0x5c),$blocksize);
$hmac = pack(
'H*',$hashfunc(
($key^$opad).pack(
'H*',$hashfunc(
($key^$ipad).$data
)
)
)
);
return bin2hex($hmac);
}
}
function ecwid_should_display_escaped_fragment_catalog()
{
if (!isset($_GET['_escaped_fragment_'])) return;
if ( Ecwid_Api_V3::is_available()) {
return !ecwid_is_store_closed();
}
return false;
}
function ecwid_get_default_pb_size() {
return array(
'grid_rows' => 20,
'grid_columns' => 3,
'list_rows' => 60,
'table_rows' => 60
);
}
function ecwid_update_store_id( $new_store_id ) {
EcwidPlatform::cache_reset( 'nav_categories_posts' );
update_option( 'ecwid_store_id', $new_store_id );
update_option( 'ecwid_api_check_retry_after', 0 );
ecwid_invalidate_cache( true );
EcwidPlatform::cache_reset('all_categories');
do_action('ecwid_update_store_id', $new_store_id);
}
function ecwid_is_paid_account()
{
if ( Ecwid_Api_V3::is_available() ) {
$api = new Ecwid_Api_V3();
$profile = $api->get_store_profile();
return $api->is_store_feature_available('PREMIUM');
}
return false;
}
function ecwid_embed_svg($name) {
$path = ECWID_PLUGIN_DIR . 'images/' . $name . '.svg';
if( file_exists( $path ) ) {
$code = file_get_contents( $path );
echo $code;
}
}
/*
* Basically a copy of has_shortcode that returns the matched shortcode
*/
function ecwid_find_shortcodes( $content, $tag ) {
if ( false === strpos( $content, '[' ) ) {
return false;
}
if ( shortcode_exists( $tag ) ) {
preg_match_all( '/' . ecwid_get_shortcode_regex() . '/s', $content, $matches, PREG_SET_ORDER );
if ( empty( $matches ) )
return false;
$result = array();
foreach ( $matches as $shortcode ) {
if ( $tag === $shortcode[2] ) {
$result[] = $shortcode;
} elseif ( !empty($shortcode[5]) && $found = ecwid_find_shortcodes( $shortcode[5], $tag ) ) {
$result = array_merge($result, $found);
}
}
if (empty($result)) {
$result = false;
}
return $result;
}
return false;
}
// Since we use shortcode regex in our own functions, we need it to be persistent
function ecwid_get_shortcode_regex() {
global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
// Also, see shortcode_unautop() and shortcode.js.
return
'\\[' // Opening bracket
. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
. "($tagregexp)" // 2: Shortcode name
. '(?![\\w-])' // Not followed by word character or hyphen
. '(' // 3: Unroll the loop: Inside the opening shortcode tag
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. '(?:'
. '\\/(?!\\])' // A forward slash not followed by a closing bracket
. '[^\\]\\/]*' // Not a closing bracket or forward slash
. ')*?'
. ')'
. '(?:'
. '(\\/)' // 4: Self closing tag ...
. '\\]' // ... and closing bracket
. '|'
. '\\]' // Closing bracket
. '(?:'
. '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
. '[^\\[]*+' // Not an opening bracket
. '(?:'
. '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
. '[^\\[]*+' // Not an opening bracket
. ')*+'
. ')'
. '\\[\\/\\2\\]' // Closing shortcode tag
. ')?'
. ')'
. '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
}
?>