diff --git a/.eslintrc b/.eslintrc index 67f3d60..e1e6334 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,13 +1,13 @@ -eslintConfig: - root: true - -ecmaFeatures: - modules: true - jsx: true - arrowFunctions: true - blockBindings: true - classes: true - defaultParams: true +# eslintConfig: +# root: true +# +# ecmaFeatures: +# modules: true +# jsx: true +# arrowFunctions: true +# blockBindings: true +# classes: true +# defaultParams: true env: amd: true @@ -227,4 +227,4 @@ rules: globals: DeepFramework: true Stripe: true - angular: true \ No newline at end of file + angular: true diff --git a/src/adtechmedia-config.php b/src/adtechmedia-config.php index dc9be43..6b28920 100644 --- a/src/adtechmedia-config.php +++ b/src/adtechmedia-config.php @@ -26,11 +26,12 @@ class Adtechmedia_Config { 'minDelay' => 150000, 'factor' => 1.7, 'atm_js_cache_time' => 86400, - 'template_position' => '{"sticky":true,"width":"600px","offset_top":"20px","offset_left":"-60px","scrolling_offset_top":"100px"}', - 'template_overall_styles_patch' => '@media (max-width: 991px) { .atm-targeted-container { width: 90% !important; left: 5% !important; transform: none !important; } } @media (max-width: 600px) { .atm-targeted-container { top: 0 !important; } }', /* @todo Replace responsive hotfix with smth sustainable and reliable */ - 'template_overall_styles' => '.atm-base-modal {background-color: #ffffff;}.atm-targeted-modal .atm-head-modal .atm-modal-heading {background-color: #ffffff;}.atm-targeted-modal{border: 1px solid #d3d3d3;}.atm-targeted-modal{box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.1);}.atm-base-modal .atm-footer{background-color: #fafafa;}.atm-base-modal .atm-footer{border: 1px solid #e3e3e3;}.atm-targeted-container .mood-block-info,.atm-targeted-modal,.atm-targeted-modal .atm-head-modal .atm-modal-body p,.atm-unlock-line .unlock-btn {font-family: \'Merriweather\', sans-serif;}', - 'template_overall_styles_inputs' => '{"background-color":"#ffffff","border":"1px solid #d3d3d3","font-family":"\'Merriweather\', sans-serif","box-shadow":"0 1px 2px 0 rgba(0, 0, 0, 0.1)","footer-background-color":"#fafafa","footer-border":"1px solid #e3e3e3"}', + 'template_overall_styles_patch' => '@media (max-width: 991px) { .atm-targeted-container { width: 90% !important; left: 5% !important; transform: none !important; } } @media (max-width: 600px) { .atm-targeted-container { top: 0 !important; } }', 'sw_js_url' => 'https://www.adtechmedia.io/atm-admin/atm-build/sw.min.js', + 'tpl_js_url' => 'https://www.adtechmedia.io/atm-core/atm-build/atmTpl.js', + 'tpl_mgmt_js_url' => 'https://www.adtechmedia.io/atm-core/atm-build/atmTplManager.js', + 'terms_url' => 'https://www.adtechmedia.io/terms/dialog.html', + 'register_url_tpl' => 'https://www.adtechmedia.io/admin/accounts/signup/%s', 'price' => 5, 'content_offset' => 2, 'payment_pledged' => 2, @@ -41,6 +42,8 @@ class Adtechmedia_Config { 'content_paywall' => 'transactions', 'content_offset_type' => 'paragraphs', 'country' => 'United States', + 'platform_id' => 'Wordpress', + 'appearance_settings' => '{"model":{"main":{"sticky":true,"width":"600px","offset":{"top":"20px","fromCenter":"-60px","scrollTop":"100"}},"body":{"backgroundColor":"#ffffff","border":"1px solid #d3d3d3","fontFamily":"\'Merriweather\', sans-serif","boxShadow":"0 1px 2px 0 rgba(0, 0, 0, 0.1)"},"footer":{"backgroundColor":"#fafafa","border":"1px solid #e3e3e3"}}}', ]; /** @@ -57,10 +60,14 @@ public static function get( $name ) { /** * Set API end point for localhost */ - public static function set_api_end_point() { + public static function setup_endpoints() { if ( self::is_localhost() ) { - self::$conf['api_end_point'] = 'https://api-dev.adtechmedia.io/v1/'; - self::$conf['sw_js_url'] = 'https://www-dev.adtechmedia.io/atm-admin/atm-build/sw.min.js'; + self::$conf['api_end_point'] = 'https://api-dev.adtechmedia.io/v1/'; + self::$conf['sw_js_url'] = 'https://www-dev.adtechmedia.io/atm-admin/atm-build/sw.min.js'; + self::$conf['register_url_tpl'] = 'https://www-dev.adtechmedia.io/admin/accounts/signup/%s'; + self::$conf['tpl_js_url'] = 'https://www-dev.adtechmedia.io/atm-core/atm-build/atmTpl.js'; + self::$conf['tpl_mgmt_js_url'] = 'https://www-dev.adtechmedia.io/atm-core/atm-build/atmTplManager.js'; + self::$conf['terms_url'] = 'https://www-dev.adtechmedia.io/terms/dialog.html'; } } @@ -77,4 +84,4 @@ public static function is_localhost() { } } -Adtechmedia_Config::set_api_end_point(); +Adtechmedia_Config::setup_endpoints(); diff --git a/src/adtechmedia-lifecycle.php b/src/adtechmedia-lifecycle.php index 3316b59..9c1deae 100644 --- a/src/adtechmedia-lifecycle.php +++ b/src/adtechmedia-lifecycle.php @@ -38,7 +38,7 @@ public function install() { $this->init_options(); // Initialize DB Tables used by the plugin. - $this->install_database_tables(); + $this->ensure_database_tables(); // Other Plugin initialization - for the plugin writer to override as needed. $this->other_install(); @@ -55,7 +55,7 @@ public function install() { */ public function uninstall() { $this->other_uninstall(); - $this->un_install_database_tables(); + $this->un_ensure_database_tables(); $this->delete_saved_options(); $this->mark_as_un_installed(); } @@ -75,6 +75,7 @@ public function upgrade() { * @return void */ public function activate() { + $this->ensure_database_tables(); delete_transient( 'adtechmedia-supported-countries-new' ); $website = get_home_url(); $name = preg_replace( '/https?:\/\//', '', $website ); @@ -96,17 +97,16 @@ public function activate() { $this->add_plugin_option( 'price_currency', Adtechmedia_Config::get( 'price_currency' ) ); $this->add_plugin_option( 'content_paywall', Adtechmedia_Config::get( 'content_paywall' ) ); $this->add_plugin_option( 'content_offset_type', Adtechmedia_Config::get( 'content_offset_type' ) ); - $this->add_plugin_option( 'template_position', Adtechmedia_Config::get( 'template_position' ) ); $this->add_plugin_option( 'template_overall_styles_patch', Adtechmedia_Config::get( 'template_overall_styles_patch' ) ); - $this->add_plugin_option( 'template_overall_styles', Adtechmedia_Config::get( 'template_overall_styles' ) ); - $this->add_plugin_option( 'template_overall_styles_inputs', Adtechmedia_Config::get( 'template_overall_styles_inputs' ) ); - $this->add_plugin_option( 'theme_config_id', 'default' ); - $this->add_plugin_option( 'theme_config_name', '' ); + $this->add_plugin_option( 'appearance_settings', Adtechmedia_Config::get( 'appearance_settings' ) ); try { $this->check_api_key_exists(); $this->check_prop(); - Adtechmedia_ThemeManager::init_theme_config_model(); + if ( ! empty( $this->get_plugin_option( 'key' ) ) ) { + $this->update_prop(); + $this->update_appearance(); + } } catch ( Error $error ) { $this->activation_error = $error->getMessage(); @@ -123,6 +123,40 @@ public function activate() { wp_schedule_event( time(), 'daily', 'adtechmedia_update_event' ); } + /** + * Update appearance settings + */ + public function update_appearance() { + $plugin_dir = plugin_dir_path( __FILE__ ); + $file = $plugin_dir . '/js/atm.min.js'; + // @codingStandardsIgnoreStart + @unlink( $file ); + // @codingStandardsIgnoreEnd + + $appearance_settings = json_decode( $this->get_plugin_option( 'appearance_settings' ), true ); + + $this->add_plugin_option( 'template_overall_styles', $this->get_template_overall_styles( $appearance_settings ) ); + + $data = [ + 'targetModal' => [ + 'targetCb' => $this->get_target_cb_js( $appearance_settings ), + 'toggleCb' => $this->get_toggle_cb_js( $appearance_settings ), + ], + 'styles' => [ + 'main' => base64_encode( + $this->get_plugin_option( 'template_overall_styles' ) . + $this->get_plugin_option( 'template_overall_styles_patch' ) + ), + ], + ]; + Adtechmedia_Request::property_update_config_by_array( + $this->get_plugin_option( 'id' ), + $this->get_plugin_option( 'key' ), + $data + ); + Adtechmedia_ContentManager::clear_all_content(); + } + /** * Show error if activation failed */ @@ -144,12 +178,14 @@ public function activation_error() { public function check_api_key_exists() { $key = $this->get_plugin_option( 'key' ); if ( empty( $key ) ) { - $key = Adtechmedia_Request::api_key_create( + $key_response = Adtechmedia_Request::api_key_create( $this->get_plugin_option( 'support_email' ) ); + $key = $key_response['apiKey']; if ( empty( $key ) ) { return false; } else { + $this->add_plugin_option( 'client-id', $key_response['clientId'] ); $this->add_plugin_option( 'key', $key ); } } @@ -223,7 +259,32 @@ public function add_actions_and_filters() { * * @return void */ - protected function install_database_tables() { + protected function ensure_database_tables() { + global $wpdb; + $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_table_name' ) ); + // @codingStandardsIgnoreStart + $wpdb->query( + "CREATE TABLE IF NOT EXISTS `$table_name` ( + `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + `option_name` VARCHAR(191) NOT NULL DEFAULT '', + `option_value` LONGTEXT NOT NULL , + PRIMARY KEY (`id`), + UNIQUE INDEX `option_name` (`option_name`) + )" + ); + // @codingStandardsIgnoreEnd + $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_cache_table_name' ) ); + // @codingStandardsIgnoreStart + $wpdb->query( + "CREATE TABLE IF NOT EXISTS `$table_name` ( + `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, + `item_id` VARCHAR(191) NOT NULL DEFAULT '', + `value` LONGTEXT NOT NULL , + PRIMARY KEY (`id`), + UNIQUE INDEX `item_id` (`item_id`) + )" + ); + // @codingStandardsIgnoreEnd } /** @@ -232,7 +293,20 @@ protected function install_database_tables() { * * @return void */ - protected function un_install_database_tables() { + protected function un_ensure_database_tables() { + global $wpdb; + $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_table_name' ) ); + // @codingStandardsIgnoreStart + $wpdb->query( + "DROP TABLE IF EXISTS `$table_name`" + ); + // @codingStandardsIgnoreEnd + $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_cache_table_name' ) ); + // @codingStandardsIgnoreStart + $wpdb->query( + "DROP TABLE IF EXISTS `$table_name`" + ); + // @codingStandardsIgnoreEnd } /** diff --git a/src/adtechmedia-optionsmanager.php b/src/adtechmedia-optionsmanager.php index 753cbea..1a47ea8 100644 --- a/src/adtechmedia-optionsmanager.php +++ b/src/adtechmedia-optionsmanager.php @@ -419,24 +419,53 @@ public function update_prop() { $this->get_plugin_option( 'payment_pledged' ), $this->get_plugin_option( 'content_offset_type' ), $this->get_plugin_option( 'price_currency' ), - $this->get_plugin_option( 'content_paywall' ), - $this->get_target_cb_js( json_decode( stripslashes( $this->get_plugin_option( 'template_position' ) ), true ) ), - $this->get_toggle_cb_js( json_decode( stripslashes( $this->get_plugin_option( 'template_position' ) ), true ) ) + $this->get_plugin_option( 'content_paywall' ) ); Adtechmedia_ContentManager::clear_all_content(); } + /** + * Get overall styles + * + * @param array $appearance_settings array of position properties. + * @return string + */ + public function get_template_overall_styles( $appearance_settings ) { + return " + .atm-base-modal { + background-color: {$appearance_settings['model']['body']['backgroundColor']}; + } + .atm-targeted-modal .atm-head-modal .atm-modal-heading { + background-color: {$appearance_settings['model']['body']['backgroundColor']}; + } + .atm-targeted-modal { + position: relative; + border: {$appearance_settings['model']['body']['border']}; + box-shadow: {$appearance_settings['model']['body']['boxShadow']}; + } + .atm-base-modal .atm-footer { + background-color: {$appearance_settings['model']['footer']['backgroundColor']}; + border: {$appearance_settings['model']['footer']['border']}; + } + .atm-targeted-container .mood-block-info,.atm-targeted-modal,.atm-targeted-modal .atm-head-modal .atm-modal-body p,.atm-unlock-line .unlock-btn { + font-family: {$appearance_settings['model']['body']['fontFamily']}; + }"; + } + /** * Get JS to targetCb function * - * @param array $position array of position properties. + * @param array $appearance_settings array of position properties. * @return string */ - public function get_target_cb_js( $position ) { + public function get_target_cb_js( $appearance_settings ) { + $position = $appearance_settings['model']['main']; $width = ! empty( $position['width'] ) ? $position['width'] : '600px'; - $offset_top = ! empty( $position['offset_top'] ) ? $position['offset_top'] : '0px'; - $offset_left = ! empty( $position['offset_left'] ) ? $position['offset_left'] : '0px'; + $offset_top = ! empty( $position['offset']['top'] ) ? $position['offset']['top'] : '0px'; + $offset_left = ! empty( $position['offset']['fromCenter'] ) ? $position['offset']['fromCenter'] : '0px'; + $content = ''; + if ( ! empty( $position['sticky'] ) ) { $content .= "mainModal.rootNode.style.position = 'fixed';\n"; $content .= "mainModal.rootNode.style.top = '$offset_top';\n"; @@ -465,11 +494,13 @@ public function get_target_cb_js( $position ) { /** * Get JS to toggleCb function * - * @param array $position array of position properties. + * @param array $appearance_settings array of position properties. * @return string */ - public function get_toggle_cb_js( $position ) { - $scrolling_offset_top = ! empty( $position['scrolling_offset_top'] ) ? (int) $position['scrolling_offset_top'] : 0; + public function get_toggle_cb_js( $appearance_settings ) { + $position = $appearance_settings['model']['main']; + $scrolling_offset_top = ! empty( $position['offset']['scrollTop'] ) ? (int) $position['offset']['scrollTop'] : 0; + if ( empty( $position['sticky'] ) ) { $scrolling_offset_top = -10; } diff --git a/src/adtechmedia-plugin.php b/src/adtechmedia-plugin.php index a069485..b0ebc8d 100644 --- a/src/adtechmedia-plugin.php +++ b/src/adtechmedia-plugin.php @@ -112,65 +112,6 @@ protected function get_main_plugin_file_name() { return 'adtechmedia.php'; } - /** - * See: http://plugin.michael-simpson.com/?page_id=101 - * Called by install() to create any database tables if needed. - * Best Practice: - * (1) Prefix all table names with $wpdb->prefix - * (2) make table names lower case only - * - * @return void - */ - protected function install_database_tables() { - global $wpdb; - $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_table_name' ) ); - // @codingStandardsIgnoreStart - $wpdb->query( - "CREATE TABLE IF NOT EXISTS `$table_name` ( - `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, - `option_name` VARCHAR(191) NOT NULL DEFAULT '', - `option_value` LONGTEXT NOT NULL , - PRIMARY KEY (`id`), - UNIQUE INDEX `option_name` (`option_name`) - )" - ); - // @codingStandardsIgnoreEnd - $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_cache_table_name' ) ); - // @codingStandardsIgnoreStart - $wpdb->query( - "CREATE TABLE IF NOT EXISTS `$table_name` ( - `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, - `item_id` VARCHAR(191) NOT NULL DEFAULT '', - `value` LONGTEXT NOT NULL , - PRIMARY KEY (`id`), - UNIQUE INDEX `item_id` (`item_id`) - )" - ); - // @codingStandardsIgnoreEnd - } - - /** - * See: http://plugin.michael-simpson.com/?page_id=101 - * Drop plugin-created tables on uninstall. - * - * @return void - */ - protected function un_install_database_tables() { - global $wpdb; - $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_table_name' ) ); - // @codingStandardsIgnoreStart - $wpdb->query( - "DROP TABLE IF EXISTS `$table_name`" - ); - // @codingStandardsIgnoreEnd - $table_name = $this->prefix_table_name( Adtechmedia_Config::get( 'plugin_cache_table_name' ) ); - // @codingStandardsIgnoreStart - $wpdb->query( - "DROP TABLE IF EXISTS `$table_name`" - ); - // @codingStandardsIgnoreEnd - } - /** * Perform actions when upgrading from version X to version Y @@ -256,15 +197,20 @@ public function add_actions_and_filters() { if ( isset( $_GET['atm-token'] ) && ! empty( $_GET['atm-token'] ) ) { $atm_token = sanitize_text_field( wp_unslash( $_GET['atm-token'] ) ); - $key = Adtechmedia_Request::api_token2key( + $key_response = Adtechmedia_Request::api_token2key( $this->get_plugin_option( 'support_email' ), $atm_token ); + $key = $key_response['apiKey']; if ( ! empty( $key ) ) { $this->delete_plugin_option( 'api-token-sent' ); $this->add_plugin_option( 'key', $key ); + $this->add_plugin_option( 'client-id', $key_response['clientId'] ); $this->add_plugin_option( 'admin-redirect', true ); + $this->add_plugin_option( 'force-save-templates', true ); + $this->update_prop(); + $this->update_appearance(); add_action( 'admin_init', array( @@ -337,18 +283,6 @@ public function add_actions_and_filters() { 'ajax_save_template', ) ); - add_action( 'wp_ajax_return_to_default_values', - array( - &$this, - 'ajax_return_to_default_values', - ) - ); - add_action( 'after_switch_theme', - array( - &$this, - 'change_theme_configs', - ) - ); } /** @@ -417,25 +351,19 @@ function write_sw() { // @codingStandardsIgnoreEnd } - /** - * Change theme config. - */ - function change_theme_configs() { - Adtechmedia_ThemeManager::init_theme_config_model(); - } - /** * Save templates action */ public function ajax_save_template() { if ( isset( $_POST['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'adtechmedia-nonce' ) ) { // @codingStandardsIgnoreStart - $plugin_dir = plugin_dir_path( __FILE__ ); - $file = $plugin_dir . '/js/atm.min.js'; - @unlink( $file ); if ( isset( $_POST['revenueMethod'] ) ) { + $plugin_dir = plugin_dir_path( __FILE__ ); + $file = $plugin_dir . '/js/atm.min.js'; + @unlink( $file ); + $revenue_method = $_POST['revenueMethod']; - $this->add_plugin_option( 'revenue_method', $revenue_method ); + $this->update_plugin_option( 'revenue_method', $revenue_method ); Adtechmedia_Request::property_update_config_by_array( $this->get_plugin_option( 'id' ), $this->get_plugin_option( 'key' ), @@ -443,6 +371,7 @@ public function ajax_save_template() { 'revenueMethod' => $revenue_method, ] ); + Adtechmedia_ContentManager::clear_all_content(); } else if ( isset( $_POST['contentConfig'] ) ) { $content_config = json_decode( wp_unslash( $_POST['contentConfig'] ), true ); foreach ( $content_config as $a_option_key => $a_option_meta ) { @@ -451,69 +380,11 @@ public function ajax_save_template() { } } $this->update_prop(); - } else { - $options = [ - 'template_inputs' => 'inputs', - 'template_style_inputs' => 'styleInputs', - 'template_position' => 'position', - 'template_overall_styles' => 'overallStyles', - 'template_overall_styles_inputs' => 'overallStylesInputs', - ]; - $data = []; - foreach ( $options as $db_key => $post_key ) { - $value = ''; - if ( isset ( $_POST[ $post_key ] ) ) { - $value = sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ); - } - $data[ $db_key ] = $value; - $this->add_plugin_option( $db_key, $value ); - } - - $componentsTemplates = []; - $components = $_POST['components']; - $templates = $_POST['templates']; - if ( ! is_array( $components ) ) { - $components = [ $components ]; - } - if ( ! is_array( $templates ) ) { - $templates = [ $templates ]; - } - - if ( ! ( count( $components ) == 1 && array_key_exists( 0, $components ) && $components[0] == '' ) ) { - foreach ( $components as $key => $component ) { - $components[ $key ] = sanitize_text_field( wp_unslash( $component ) ); - $this->add_plugin_option( 'template_' . $components[ $key ], $templates[ $component ] ); - $componentsTemplates[ $components[ $key ] ] = base64_encode( stripslashes( $templates[ $component ] ) ); - } - } - $data = [ - 'templates' => $componentsTemplates, - 'targetModal' => [ - 'targetCb' => $this->get_target_cb_js( json_decode( stripslashes( $data['template_position'] ), true ) ), - 'toggleCb' => $this->get_toggle_cb_js( json_decode( stripslashes( $data['template_position'] ), true ) ), - ], - 'styles' => [ - 'main' => base64_encode( - $data['template_overall_styles'] . - $this->get_plugin_option( 'template_overall_styles_patch' ) - ), - ], - ]; - if ( count( $componentsTemplates ) == 0 ) { - unset( $data['templates'] ); - } - - - Adtechmedia_Request::property_update_config_by_array( - $this->get_plugin_option( 'id' ), - $this->get_plugin_option( 'key' ), - $data - ); - - // @codingStandardsIgnoreEnd + } else if ( isset( $_POST['appearanceSettings'] ) ) { + $this->update_plugin_option( 'appearance_settings', wp_unslash( $_POST['appearanceSettings'] ) ); + $this->update_appearance(); } - - Adtechmedia_ThemeManager::save_current_theme_configs(); + // @codingStandardsIgnoreEnd echo 'ok'; } @@ -547,11 +418,12 @@ public function add_adtechmedia_admin_scripts( $hook ) { [ 'adtechmedia-jquery-noty-js' ] ); wp_enqueue_script( 'jquery-validate', plugins_url( '/js/jquery.validate.min.js', __FILE__ ) ); - wp_enqueue_script( 'adtechmedia-atm-tpl-js', 'https://www.adtechmedia.io/atm-core/atm-build/atmTpl.js', [ 'adtechmedia-jquery-throttle-js' ] ); + wp_enqueue_script( 'adtechmedia-atm-tpl-js', Adtechmedia_Config::get( 'tpl_js_url' ), [ 'adtechmedia-jquery-throttle-js' ] ); + wp_enqueue_script( 'adtechmedia-atm-tpl-mgmt-js', Adtechmedia_Config::get( 'tpl_mgmt_js_url' ), [ 'adtechmedia-atm-tpl-js' ] ); wp_enqueue_script( 'adtechmedia-admin-js', plugins_url( '/js/main.js', __FILE__ ), - [ 'adtechmedia-atm-tpl-js' ] + [ 'adtechmedia-atm-tpl-mgmt-js' ] ); wp_localize_script( 'adtechmedia-admin-js', 'save_template', @@ -587,7 +459,7 @@ public function add_adtechmedia_admin_scripts( $hook ) { * Register atm.js */ public function add_adtechmedia_scripts() { - if ( ! is_single() ) { + if ( ! is_single() || empty( $this->get_plugin_option( 'key' ) ) ) { return; } if ( $script = $this->get_plugin_option( 'BuildPath' ) ) { @@ -645,7 +517,7 @@ public function clear_cache_on_update( $post_id ) { */ public function hide_content( $content ) { - if ( is_single() ) { + if ( is_single() && ! empty( $this->get_plugin_option( 'key' ) ) ) { $id = (string) get_the_ID(); $saved_content = Adtechmedia_ContentManager::get_content( $id ); if ( isset( $saved_content ) && ! empty( $saved_content ) ) { @@ -732,136 +604,4 @@ public function key_not_exists_error() { [ - 'template_position' => Adtechmedia_Config::get( 'template_position' ), - 'template_overall_styles' => Adtechmedia_Config::get( 'template_overall_styles' ), - 'template_overall_styles_inputs' => Adtechmedia_Config::get( 'template_overall_styles_inputs' ), - ], - ]; - } else { - $configs = [ - 'overall-styling-and-position' => [ - 'template_position' => array_key_exists( 'Config', $default_configs ) && array_key_exists( 'template_position', - $default_configs['Config'] - ) ? - $default_configs['Config']['template_position'] : - Adtechmedia_Config::get( 'template_position' ), - 'template_overall_styles' => array_key_exists( 'Config', $default_configs ) && array_key_exists( 'template_position', - $default_configs['Config'] - ) ? - $default_configs['Config']['template_overall_styles'] : - Adtechmedia_Config::get( 'template_overall_styles' ), - 'template_overall_styles_inputs' => array_key_exists( 'Config', $default_configs ) && array_key_exists( 'template_position', - $default_configs['Config'] - ) ? - $default_configs['Config']['template_overall_styles_inputs'] : - Adtechmedia_Config::get( 'template_overall_styles_inputs' ), - ], - ]; - } - - echo wp_json_encode( $configs ); - die(); - } elseif ( isset( $_POST['method'] ) && 'save_default_values' === sanitize_text_field( wp_unslash( $_POST['method'] ) ) ) { - $data = []; - // @codingStandardsIgnoreStart - if ( isset( $_POST['revenue_method'] ) ) { - $revenue_method = sanitize_text_field( wp_unslash( $_POST['revenueMethod'] ) ); - $this->add_plugin_option( 'revenue_method', $revenue_method ); - $data['revenue_method'] = $revenue_method; - } - if ( isset( $_POST['contentConfig'] ) ) { - $content_config = json_decode( sanitize_text_field( wp_unslash( $_POST['contentConfig'] ) ), true ); - foreach ( $content_config as $a_option_key => $a_option_meta ) { - if ( ! empty( $content_config[ $a_option_key ] ) ) { - $this->update_plugin_option( $a_option_key, $content_config[ $a_option_key ] ); - } - } - } - // @codingStandardsIgnoreEnd - - $options = [ - 'template_inputs' => 'inputs', - 'template_style_inputs' => 'styleInputs', - 'template_position' => 'position', - 'template_overall_styles' => 'overallStyles', - 'template_overall_styles_inputs' => 'overallStylesInputs', - ]; - // @codingStandardsIgnoreStart - foreach ( $options as $db_key => $post_key ) { - $value = ''; - if ( isset ( $_POST[ $post_key ] ) ) { - $value = sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ); - } - $data[ $db_key ] = $value; - $this->add_plugin_option( $db_key, $value ); - } - - $components_templates = []; - if ( isset( $_POST['components'] ) ) { - $components = json_decode( wp_unslash( $_POST['components'] ), true ); - } - if ( isset( $_POST['templates'] ) ) { - $templates = json_decode( wp_unslash( $_POST['templates'] ), true ); - } - // @codingStandardsIgnoreEnd - if ( ! is_array( $components ) ) { - $components = [ $components ]; - } - if ( ! is_array( $templates ) ) { - $templates = [ $templates ]; - } - if ( ! ( 1 === count( $components ) && array_key_exists( 0, $components ) || '' === $components[0] ) ) { - foreach ( $components as $key => $component ) { - $components[ $key ] = sanitize_text_field( wp_unslash( $component ) ); - $this->add_plugin_option( 'template_' . $components[ $key ], $templates[ $component ] ); - $components_templates[ $components[ $key ] ] = base64_encode( stripslashes( $templates[ $component ] ) ); - } - } - - $data = array_merge( $data, - [ - 'templates' => $components_templates, - 'targetModal' => [ - 'targetCb' => $this->get_target_cb_js( json_decode( stripslashes( $data['template_position'] ), true ) ), - 'toggleCb' => $this->get_toggle_cb_js( json_decode( stripslashes( $data['template_position'] ), true ) ), - ], - 'styles' => [ - 'main' => base64_encode( - $data['template_overall_styles'] - . $this->get_plugin_option( 'template_overall_styles_patch' ) - ), - ], - ] - ); - if ( 0 === count( $components_templates ) ) { - unset( $data['templates'] ); - } - - Adtechmedia_Request::property_update_config_by_array( - $this->get_plugin_option( 'id' ), - $this->get_plugin_option( 'key' ), - $data - ); - $this->update_prop(); - /* regenerate atm.js and sw.js */ - $this->add_plugin_option( 'atm-js-is-old', '1' ); - echo 'ok'; - die(); - } - } - } } diff --git a/src/adtechmedia-request.php b/src/adtechmedia-request.php index 28a2e71..2919dfa 100644 --- a/src/adtechmedia-request.php +++ b/src/adtechmedia-request.php @@ -162,11 +162,14 @@ public static function api_token2key( $email, $token ) { 'GET', [], $data, - [ 'apiKey' ] + [ 'apiKey', 'clientId' ] ); - if ( $response && isset( $response['apiKey'] ) ) { - return $response['apiKey']; + if ( $response && isset( $response['apiKey'] ) && isset( $response['clientId'] ) ) { + return [ + 'apiKey' => $response['apiKey'], + 'clientId' => $response['clientId'], + ]; } return false; } @@ -191,8 +194,11 @@ public static function api_key_create( $email ) { ); if ( $response ) { - if ( isset( $response['apiKey'] ) ) { - return $response['apiKey']; + if ( isset( $response['apiKey'] ) && isset( $response['clientId'] ) ) { + return [ + 'apiKey' => $response['apiKey'], + 'clientId' => $response['clientId'], + ]; } else if ( isset( $response['errorMessage'] ) ) { $error = json_decode( $response['errorMessage'], true ); if ( preg_match( '/UsernameExistsException/i', $error['errorMessage'] ) ) { @@ -203,6 +209,8 @@ public static function api_key_create( $email ) { } throw new Error( $error['errorMessage'] ); + } else { + throw new Error( 'Missing apiKey or clientId parameters in response' ); } } else { return false; @@ -284,8 +292,6 @@ public static function property_update_config_by_array( $id, $key, $data ) { * @param string $offset_type offset type. * @param string $currency currency. * @param string $pledged_type pledged type. - * @param string $get_target_cb_js target cb js. - * @param string $get_toggle_cb_js toggle cb js. * @return array|bool */ public static function property_update_config( @@ -301,9 +307,7 @@ public static function property_update_config( $payment_pledged, $offset_type, $currency, - $pledged_type, - $get_target_cb_js, - $get_toggle_cb_js + $pledged_type ) { if ( empty( $key ) ) { return false; @@ -335,10 +339,6 @@ public static function property_update_config( 'currency' => $currency, 'pledgedType' => self::get_pledged_type( $pledged_type ), ], - 'targetModal' => [ - 'toggleCb' => $get_toggle_cb_js, - 'targetCb' => $get_target_cb_js, - ], ], ]; $response = self::make( diff --git a/src/adtechmedia-thememanager.php b/src/adtechmedia-thememanager.php deleted file mode 100644 index 016150e..0000000 --- a/src/adtechmedia-thememanager.php +++ /dev/null @@ -1,305 +0,0 @@ -update_plugin_option( 'theme_config_id', 'default' ); - $option_manager->update_plugin_option( 'theme_config_name', '' ); - $option_manager->update_plugin_option( 'template_position', Adtechmedia_Config::get( 'template_position' ) ); - $option_manager->update_plugin_option( 'template_overall_styles', Adtechmedia_Config::get( 'template_overall_styles' ) ); - $option_manager->update_plugin_option( 'template_overall_styles_inputs', Adtechmedia_Config::get( 'template_overall_styles_inputs' ) ); - } else { - $retrieve_config = $is_retrieve['Config'] ? $is_retrieve['Config'] : array(); - if ( array_key_exists( 'Default', $is_retrieve ) && true === $is_retrieve['Default'] ) { - $option_manager->update_plugin_option( 'theme_config_id', 'default' ); - } else { - $option_manager->update_plugin_option( 'theme_config_id', array_key_exists( 'Id', $is_retrieve ) ? $is_retrieve['Id'] : 'default' ); - } - $option_manager->update_plugin_option( 'theme_config_name', array_key_exists( 'ConfigName', $is_retrieve ) ? $is_retrieve['ConfigName'] : '' ); - $option_manager->update_plugin_option( 'template_position', - array_key_exists( 'template_position', $retrieve_config ) ? - $is_retrieve['Config']['template_position'] : - Adtechmedia_Config::get( 'template_position' ) - ); - $option_manager->update_plugin_option( 'template_overall_styles', - array_key_exists( 'template_overall_styles', $retrieve_config ) ? - $is_retrieve['Config']['template_overall_styles'] : - Adtechmedia_Config::get( 'template_overall_styles' ) - ); - $option_manager->update_plugin_option( 'template_overall_styles_inputs', - array_key_exists( 'template_overall_styles_inputs', $retrieve_config ) ? - $is_retrieve['Config']['template_overall_styles_inputs'] : - Adtechmedia_Config::get( 'template_overall_styles_inputs' ) - ); - } - - self::add_current_theme_to_themes_history(); - self::save_template_in_api(); - } - - /** - * Get current theme info. - * - * @return array - */ - public static function get_theme_info() { - $theme = wp_get_theme(); - - return [ - 'ThemeVersion' => $theme->get( 'Version' ), - 'ThemeName' => $theme->get( 'Name' ), - 'ThemeId' => '', - ]; - } - - /** - * Get platform info. - * - * @return array - */ - public static function get_platform_info() { - return [ - 'PlatformId' => 'Wordpress', - 'PlatformVersion' => get_bloginfo( 'version' ), - ]; - } - - /** - * Get current theme config. - * - * @return array - */ - public static function get_current_theme_config() { - $option_manager = new Adtechmedia_OptionsManager(); - - return [ - 'template_position' => $option_manager->get_plugin_option( 'template_position' ), - 'template_overall_styles' => $option_manager->get_plugin_option( 'template_overall_styles' ), - 'template_overall_styles_inputs' => $option_manager->get_plugin_option( 'template_overall_styles_inputs' ), - ]; - } - - /** - * Save current theme config to db and api. - */ - public static function save_current_theme_configs() { - $option_manager = new Adtechmedia_OptionsManager(); - $current_config_id = $option_manager->get_plugin_option( 'theme_config_id' ); - if ( is_null( $current_config_id ) || '' === $current_config_id || 'default' === $current_config_id ) { - self::create_current_theme_configs(); - } else { - self::update_current_theme_configs(); - } - } - - /** - * Create current theme config to db and api. - * - * @return array|bool|mixed|object - */ - public static function create_current_theme_configs() { - $option_manager = new Adtechmedia_OptionsManager(); - - $current_theme_info = self::get_theme_info(); - $current_platform_info = self::get_platform_info(); - $current_theme_configs = self::get_current_theme_config(); - - $data = [ - 'ThemeId' => $current_theme_info['ThemeName'] . '@' . $current_theme_info['ThemeVersion'], - 'PropertyId' => $option_manager->get_plugin_option( 'id' ), - 'ThemeVersion' => $current_theme_info['ThemeVersion'], - 'ThemeName' => $current_theme_info['ThemeName'], - 'PlatformId' => $current_platform_info['PlatformId'], - 'PlatformVersion' => $current_platform_info['PlatformVersion'], - 'ConfigName' => $current_theme_info['ThemeName'] . '@' . $current_theme_info['ThemeVersion'] . '-' . date( 'c' ), - 'Config' => $current_theme_configs, - ]; - - $result = Adtechmedia_Request::theme_config_create( - $data, - $option_manager->get_plugin_option( 'key' ) - ); - - if ( $result ) { - $option_manager->update_plugin_option( 'theme_config_id', $result['Id'] ); - $option_manager->update_plugin_option( 'theme_config_name', $result['ConfigName'] ); - $option_manager->update_plugin_option( 'template_position', - array_key_exists( 'template_position', $result['Config'] ) ? - $result['Config']['template_position'] : - '' - ); - $option_manager->update_plugin_option( 'template_overall_styles', - array_key_exists( 'template_overall_styles', $result['Config'] ) ? - $result['Config']['template_overall_styles'] : - '' - ); - $option_manager->update_plugin_option( 'template_overall_styles_inputs', - array_key_exists( 'template_overall_styles_inputs', $result['Config'] ) ? - $result['Config']['template_overall_styles_inputs'] : - '' - ); - - self::add_current_theme_to_themes_history(); - } - - return $result; - } - - /** - * Retrieve theme config from api. - * - * @return array|bool|mixed|object - */ - public static function retrieve_current_theme_configs() { - $option_manager = new Adtechmedia_OptionsManager(); - - $theme_history = $option_manager->get_plugin_option( 'themes_history' ); - $current_theme_id = ''; - if ( '' !== $theme_history && '{}' !== $theme_history ) { - $theme_history = json_decode( $theme_history, true ); - if ( is_array( $theme_history ) ) { - $theme_info = self::get_theme_info(); - if ( array_key_exists( $theme_info['ThemeName'], $theme_history ) ) { - $current_theme_id = $theme_history[ $theme_info['ThemeName'] ]; - } else { - $current_theme_id = 'default'; - } - } - $option_manager->update_plugin_option( 'theme_config_id', $current_theme_id ); - } - - if ( ! is_null( $current_theme_id ) && 'default' !== $current_theme_id && '' !== $current_theme_id ) { - return Adtechmedia_Request::theme_config_retrieve( - $current_theme_id, - null, - $option_manager->get_plugin_option( 'key' ) - ); - } else { - $current_theme_info = self::get_theme_info(); - - $current_theme_config = Adtechmedia_Request::theme_config_retrieve( - null, - $current_theme_info['ThemeName'] . '@' . $current_theme_info['ThemeVersion'], - $option_manager->get_plugin_option( 'key' ) - ); - if ( $current_theme_config ) { - return $current_theme_config; - } else { - return Adtechmedia_Request::theme_config_retrieve( - null, - $current_theme_info['ThemeName'], - $option_manager->get_plugin_option( 'key' ) - ); - } - } - - } - - /** - * Update current theme config in db and api. - * - * @return array|bool|mixed|object - */ - public static function update_current_theme_configs() { - $option_manager = new Adtechmedia_OptionsManager(); - - $current_theme_id = $option_manager->get_plugin_option( 'theme_config_id' ); - $current_theme_configs = self::get_current_theme_config(); - - $data = [ - 'Id' => $current_theme_id, - 'Config' => $current_theme_configs, - ]; - self::add_current_theme_to_themes_history(); - - return Adtechmedia_Request::theme_config_update( - $data, - $option_manager->get_plugin_option( 'key' ) - ); - } - - /** - * Add current theme to switch history. * - */ - public static function add_current_theme_to_themes_history() { - $need_create = false; - $option_manager = new Adtechmedia_OptionsManager(); - - $themes_history = $option_manager->get_plugin_option( 'themes_history' ); - if ( is_null( $themes_history ) ) { - $themes_history = []; - $need_create = true; - } else { - $themes_history = json_decode( $themes_history, true ); - } - $current_theme_info = self::get_theme_info(); - $current_theme_id = $option_manager->get_plugin_option( 'theme_config_id' ); - if ( ! is_null( $current_theme_id ) ) { - $themes_history[ $current_theme_info['ThemeName'] ] = $current_theme_id; - - if ( $need_create ) { - $option_manager->add_plugin_option( 'themes_history', wp_json_encode( $themes_history ) ); - } else { - $option_manager->update_plugin_option( 'themes_history', wp_json_encode( $themes_history ) ); - } - } - - } - - - /** - * Save themes in template api. - */ - public static function save_template_in_api() { - $adt_plugin = new Adtechmedia_Plugin(); - $option_manager = new Adtechmedia_OptionsManager(); - $adt_plugin->update_prop(); - - $data = [ - 'targetModal' => [ - 'targetCb' => $option_manager->get_target_cb_js( json_decode( stripslashes( $option_manager->get_plugin_option( 'template_position' ) ), true ) ), - 'toggleCb' => $option_manager->get_toggle_cb_js( json_decode( stripslashes( $option_manager->get_plugin_option( 'template_position' ) ), true ) ), - ], - 'styles' => [ - 'main' => base64_encode( - $option_manager->get_plugin_option( 'template_overall_styles' ) - . $option_manager->get_plugin_option( 'template_overall_styles_patch' ) - ), - ], - ]; - - Adtechmedia_Request::property_update_config_by_array( - $option_manager->get_plugin_option( 'id' ), - $option_manager->get_plugin_option( 'key' ), - $data - ); - } - - /** - * Make default config to current theme - */ - public static function make_current_as_default() { - $option_manager = new Adtechmedia_OptionsManager(); - $option_manager->update_plugin_option( 'theme_config_id', 'default' ); - self::add_current_theme_to_themes_history(); - } - -} diff --git a/src/adtechmedia.php b/src/adtechmedia.php index 6a6d622..4a7c2c9 100644 --- a/src/adtechmedia.php +++ b/src/adtechmedia.php @@ -2,7 +2,7 @@ /** * Plugin Name: AdTechMedia * Plugin URI: https://wordpress.org/plugins/adtechmedia/ - * Version: 0.9.1 + * Version: 0.10.1 * Author: AdTechMedia.io * Description: AdTechMedia is an advertising platform with micropayments capabilities for media content monetization. We help publishers and content providers to generate complimentary revenue streams that are immune to ad blocking software (e.g. native advertising or native micropayments). Our solutions are white labeled, data driven and realtime. To learn more, contact hello@adtechmedia.io or visit www.adtechmedia.io. * Text Domain: adtechmedia @@ -73,7 +73,6 @@ function adtechmedia_i18n_init() { include_once( 'adtechmedia-request.php' ); include_once( 'adtechmedia-config.php' ); include_once( 'adtechmedia-contentmanager.php' ); - include_once( 'adtechmedia-thememanager.php' ); include_once( 'lib/autoload.php' ); adtechmedia_init( __FILE__ ); } diff --git a/src/js/main.js b/src/js/main.js index 5d31263..38feece 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -2,6 +2,8 @@ * Created by yama_gs on 21.10.2016. */ +/*eslint no-useless-concat: 0, no-undef: 0, no-unused-expressions: 0*/ + function throttle(func, ms) { var isThrottled = false, savedArgs, @@ -15,7 +17,7 @@ function throttle(func, ms) { } func.apply(this, arguments); isThrottled = true; - setTimeout(function () { + setTimeout(function() { isThrottled = false; if (savedArgs) { wrapper.apply(savedThis, savedArgs); @@ -27,177 +29,44 @@ function throttle(func, ms) { return wrapper; } -var notify = throttle(function (type, text) { +var notify = throttle(function(type, text) { return noty({ - type : type, - text : text, - timeout : 3000, + type: type, + text: text, + timeout: 3000, }); }, 3500); -/*global send_api_token*/ function requestApiToken(event) { event.stopPropagation(); event.preventDefault(); - + event.target.disabled = true; - + jQuery.ajax({ - url : send_api_token.ajax_url, - type : 'post', - data : { - action : 'send_api_token', - nonce : send_api_token.nonce, - return_link_tpl : window.location.toString(), + url: send_api_token.ajax_url, + type: 'post', + data: { + action: 'send_api_token', + nonce: send_api_token.nonce, + return_link_tpl: window.location.toString(), }, - success : function (response) { + success: function(response) { notify('success', 'AdTechMedia api authorization token request has been sent'); }, - error : function (response) { + error: function(response) { notify('error', 'Error requesting AdTechMedia api authorization token. Please try again later...'); }, - complete : function() { + complete: function() { event.target.disabled = false; }, }); - + return false; } window.requestApiToken = requestApiToken; -function getCSSFields(inputs) { - var styles = {}; - - jQuery.each(inputs, function (i, input) { - if (jQuery(input).val() !== '') { - styles[jQuery(input).data('template-css')] = jQuery(input).val(); - } - }); - return styles; -} - -function getInputsData(inputs) { - var styles = {}; - jQuery.each(inputs, function (i, input) { - if (jQuery(input).val() !== '') { - if (jQuery(input).is(':checkbox')) { - styles[jQuery(input).attr('name')] = jQuery(input).prop('checked'); - } else { - styles[jQuery(input).attr('name')] = jQuery(input).val(); - } - - } - }); - return styles; -} - -function getPositionFields() { - var inputs = jQuery('[data-template="position"] input'); - - return getInputsData(inputs); -} - -function getOverallStylingFields() { - var styles = {}, - inputs = jQuery('[data-template="overall-styling"] input'); - jQuery.each(inputs, function (i, input) { - if (jQuery(input).val() !== '') { - styles[jQuery(input).attr('data-template-css')] = jQuery(input).val(); - } - }); - return styles; -} - -function getOverallStyling() { - var css = '', - stylesData = getOverallStylingFields(); - if (stylesData.hasOwnProperty('background-color')) { - css += '.atm-base-modal {background-color: ' + stylesData['background-color'] + ';}' + - '.atm-targeted-modal .atm-head-modal ' + - '.atm-modal-heading {background-color: ' + stylesData['background-color'] + ';}'; - } - jQuery.each(['border', 'box-shadow'], function (i, key) { - if (stylesData.hasOwnProperty(key)) { - css += '.atm-targeted-modal{' + key + ': ' + stylesData[key] + ';}'; - } - }); - if (stylesData.hasOwnProperty('footer-background-color')) { - css += '.atm-base-modal .atm-footer{background-color: ' + stylesData['footer-background-color'] + ';}'; - } - if (stylesData.hasOwnProperty('footer-border')) { - css += '.atm-base-modal .atm-footer{border: ' + stylesData['footer-border'] + ';}'; - } - if (stylesData.hasOwnProperty('font-family')) { - css += '.atm-targeted-container .mood-block-info,' + - '.atm-targeted-modal,' + - '.atm-targeted-modal .atm-head-modal .atm-modal-body p,' + - '.atm-unlock-line .unlock-btn {font-family: ' + stylesData['font-family'] + ';}'; - } - return css; -} -function applayOverallStyling(css) { - var style = jQuery('#overall-template-styling'); - style.html(css); -} -function fillOverallStylesFields() { - /*global templateOverallStylesInputs*/ - var inputs = jQuery('[data-template="overall-styling"] input'); - jQuery.each(inputs, function (i, input) { - var key = jQuery(input).attr('data-template-css'); - if (templateOverallStylesInputs.hasOwnProperty(key)) { - jQuery(input).val(templateOverallStylesInputs[key]) - } - }); -} -function fillPositionFields() { - /*global templatePositionInputs*/ - var inputs = jQuery('[data-template="position"] input'); - jQuery.each(inputs, function (i, input) { - var key = jQuery(input).attr('name'); - if (templatePositionInputs.hasOwnProperty(key)) { - if (jQuery(input).is(':checkbox')) { - //styles[jQuery(input).attr('name')] = jQuery(input).prop('checked'); - jQuery(input).prop('checked', templatePositionInputs[key]); - } else { - jQuery(input).val(templatePositionInputs[key]) - } - } - }); - if (!jQuery('#checkbox-sticky').prop('checked')) { - jQuery('.disable-if-sticky input').attr('disabled', 'disabled'); - } else { - jQuery('.disable-if-sticky input').removeAttr('disabled'); - } -} -function fillCSSFields(key, inputValues, inputFields) { - if (inputValues.hasOwnProperty(key)) { - jQuery.each(inputValues[key], function (name, value) { - inputFields[key].inputs.filter('[data-template-css="' + name + '"]').val(value); - }); - } -} -function inputsToObject(inputs) { - var res = {}; - jQuery.each(inputs, function (key, value) { - res[key] = value.input.val(); - }); - return res; -} -function styleInputsToObject(inputs) { - var res = {}; - jQuery.each(inputs, function (key, value) { - res[key] = getCSSFields(value.inputs); - }); - return res; -} -function getDatatemplate(value) { - if ('auth' === value) { - value = 'pledge'; - } - return '[data-template="' + value + '"]'; -} - function initModal() { // Get the modal. var modal = document.getElementById('terms-modal'); @@ -209,1185 +78,303 @@ function initModal() { var span = document.getElementsByClassName('close')[0]; // When the user clicks the button, open the modal. - btn.onclick = function () { + btn.onclick = function() { modal.style.display = 'block'; }; // When the user clicks on (x), close the modal. - span.onclick = function () { + span.onclick = function() { modal.style.display = 'none'; }; // When the user clicks anywhere outside of the modal, close it. - window.onclick = function (event) { + window.onclick = function(event) { if (event.target === modal) { modal.style.display = 'none'; } } } -jQuery(document).ready(function () { - /*global atmTpl, templateInputs, templateStyleInputs, save_template, noty, return_to_default_values, templateOverallStylesInputsDefault, templatePositionInputs, templateOverallStylesInputs */ - //atmTpl.default.config({revenueMethod : 'micropayments'}); - var atmTemplating = atmTpl.default; - - //atmTpl.config({revenueMethod: 'advertising'}); - fillPositionFields(); - fillOverallStylesFields(); - - var tabs = [ - { - id : 'pledge-salutation', - dataTab : 'salutation', - options : [ - { - name : 'body-welcome', - inputName : 'welcome', - type : 'expanded' - } - ] - }, - { - id : 'pledge-message', - dataTab : 'message', - options : [ - { - name : 'body-msg-mp', - inputName : 'message-expanded', - type : 'expanded' - }, - { - name : 'heading-headline', - inputName : 'message-collapsed', - type : 'collapsed' - } - ] - }, - { - id : 'auth-user', - dataTab : 'user', - component : 'authComponent', - view : ['pledge', 'pay'], - options : [ - { - name : 'logged-headline', - inputName : 'user-logged', - type : 'collapsed' - }, - { - name : 'used-headline', - inputName : 'user-used', - type : 'expanded' - } - ] - }, - { - id : 'pay-salutation', - dataTab : 'salutation', - options : [ - { - name : 'body-salutation', - inputName : 'salutation', - type : 'expanded' - } - ] - }, - { - id : 'pay-message', - dataTab : 'message', - options : [ - { - name : 'body-msg-mp', - inputName : 'message-expanded', - type : 'expanded' - }, - { - name : 'heading-headline-setup', - inputName : 'message-collapsed', - type : 'collapsed' - } - ] - }, - { - id : 'refund-mood-ok', - dataTab : 'mood-ok', - options : [ - { - name : 'body-feeling-ok', - inputName : 'body-feeling-ok', - type : 'expanded' - } - ] - }, - { - id : 'refund-mood', - dataTab : 'mood', - options : [ - { - name : 'body-feeling', - inputName : 'body-feeling', - type : 'expanded' - } - ] - }, { - id : 'refund-mood-happy', - dataTab : 'mood-happy', - options : [ - { - name : 'body-feeling-happy', - inputName : 'body-feeling-happy', - type : 'expanded' - } - ] - }, - { - id : 'refund-mood-not-happy', - dataTab : 'mood-not-happy', - options : [ - { - name : 'body-feeling-not-happy', - inputName : 'body-feeling-not-happy', - type : 'expanded' - } - ] - }, - { - id : 'refund-message', - dataTab : 'message', - options : [ - { - name : 'body-msg', - inputName : 'message-expanded', - type : 'expanded' - }, - { - name : 'heading-headline', - inputName : 'message-collapsed', - type : 'collapsed' - } - ] - }, - { - id : 'refund-share', - dataTab : 'share', - options : [ - { - name : 'body-share-experience', - inputName : 'body-share-experience', - type : 'expanded' - } - ] - } - - ]; - var componentsViews = [ - { - name : 'pledge', - collapsed : '#render-pledge-collapsed', - expanded : '#render-pledge-expanded' - }, - { - name : 'pay', - collapsed : '#render-pay-collapsed', - expanded : '#render-pay-expanded' - }, - { - name : 'refund', - collapsed : '#render-refund-collapsed', - expanded : '#render-refund-expanded' - }, - ] - - var components = [ - { - name : 'pledge', - component : 'pledgeComponent', - dataTab : 'pledge', - view : 'pledge', - sections : [ - 'pledge-salutation', - 'pledge-message', - 'auth-user' - ] - }, - { - name : 'pay', - component : 'payComponent', - dataTab : 'pay', - view : 'pay', - sections : [ - 'pay-salutation', - 'pay-message', - 'auth-user' - ] - }, - { - name : 'refund', - component : 'refundComponent', - dataTab : 'refund', - view : 'refund', - sections : [ - 'refund-mood-ok', - 'refund-mood', - 'refund-mood-happy', - 'refund-mood-not-happy', - 'refund-message', - 'refund-share' - ] - } - ]; - - var inputsVars = { - pledge : { - welcome : { - component : 'pledgeComponent', - body : 'body-welcome' - }, - 'message-expanded' : { - component : 'pledgeComponent', - body : 'body-msg-mp' - }, - 'message-collapsed' : { - component : 'pledgeComponent', - body : 'heading-headline' - }, - 'user-used' : { - component : 'authComponent', - body : 'used-headline' - }, - 'user-logged' : { - component : 'authComponent', - body : 'logged-headline' - } - }, - pay : { - salutation : { - component : 'payComponent', - body : 'body-salutation' - }, - 'message-expanded' : { - component : 'payComponent', - body : 'body-msg-mp' - }, - 'message-collapsed' : { - component : 'payComponent', - body : 'heading-headline-setup' - }, - 'user-used' : { - component : 'authComponent', - body : 'used-headline' - }, - 'user-logged' : { - component : 'authComponent', - body : 'logged-headline' - } - }, - refund : { - 'message-expanded' : { - component : 'refundComponent', - body : 'body-msg' - }, - 'message-collapsed' : { - component : 'refundComponent', - body : 'heading-headline' - } - } - }; - - function fillVarsLabels(stories) { - var inputVars; - for (var tab in inputsVars) { - if (inputsVars.hasOwnProperty(tab)) { - for (var input in inputsVars[tab]) { - if (inputsVars[tab].hasOwnProperty(input)) { - inputVars = stories[inputsVars[tab][input].component][inputsVars[tab][input].body].components; - jQuery('.tooltip__label[data-var="' - + tab - + '-' - + input - + '"]').html('Available variables: {' - + inputVars.join('}, {') - + '}'); - } - } - } - } - } - - (function ($) { - // read available template stories - //atmTpl.default.config({revenueMethod: 'advertising'}); - var stories = atmTemplating.stories(); - - var views = {}; - var tabViews = {}; - var inputs = {}; - var options = {}; - var styling = {}; - var styleInputs = {}; - - fillVarsLabels(stories); +function addLoader(btn) { + var icon = btn.find('i'); + btn.addClass('disabled'); + icon.removeClass('mdi mdi-check'); + icon.addClass('fa fa-spinner fa-spin'); +} - function toggleTemplates() { - var sender = jQuery(jQuery(this.$el).parents('[data-view]')[0]), - viewKey = sender.attr('data-view-key'), - type = sender.attr('data-view'), - typeOther = 'expanded', - small = true, - senderParent = sender.parent(), - senderParentExpaned = senderParent.find('[data-view-text="expanded"]'), - senderParentCollapsed = senderParent.find('[data-view-text="collapsed"]'); - if (type === 'expanded') { - typeOther = 'collapsed'; - small = false; - } - senderParent.find('[data-view="' + typeOther + '"]').attr('data-view', type); - sender.attr('data-view', typeOther); - views[viewKey][typeOther]._watchers['showModalBody'].forEach(function (unwatch) { - return unwatch() - }); - delete views[viewKey][typeOther]._watchers['showModalBody']; - views[viewKey][typeOther].small(small); - views[viewKey][typeOther].watch('showModalBody', toggleTemplates); - var tmp = views[viewKey]['expanded']; - views[viewKey]['expanded'] = views[viewKey]['collapsed']; - views[viewKey]['collapsed'] = tmp; - tmp = senderParentExpaned.html(); - senderParentExpaned.html(senderParentCollapsed.html()); - senderParentCollapsed.html(tmp); - } +function removeLoader(btn) { + var icon = btn.find('i'); + btn.removeClass('disabled'); + icon.removeClass('fa fa-spinner fa-spin'); + icon.addClass('mdi mdi-check'); +} - var invalidVar = ''; - function checkInputVars(input, tabName) { - var inputName = input.attr('name'); - if (inputName !== '') { - var inputWithVars = inputsVars[tabName][inputName]; - if (inputWithVars) { - var inputVars = stories[inputWithVars.component][inputWithVars.body].components; - var inputValue = input.val(); - var reg = /\{(.*?)}/g; - var match; +function showSuccess() { + notify('success', 'AdTechMedia parameters have been saved successfully'); +} - while ((match = reg.exec(inputValue)) !== null) { - if (!inputVars.includes(match[1])) { - invalidVar = match[1]; - notify('error', 'Variable {' + match[1] + '} is not defined.'); +function addValidate(form, rules, messages) { + jQuery.each(rules, function(name, item) { + jQuery('input[name="' + name + '"]').on('focus', function() { + var item = jQuery('input[name="' + name + '"]'); - return false; - } - } + if (jQuery(item).hasClass('invalid')) { + jQuery(item).removeClass('invalid'); + var label = jQuery(item).parents('.custom-label').find('label'); + if (!label[0]) { + label = jQuery(item).parents('.custom-input').find('label'); } + label.removeClass('invalid'); } - - return true; - } - - jQuery.each(components, function (i, template) { - var tab = jQuery(getDatatemplate(template.dataTab)); - options[template.component] = {}; - styling[template.component] = {}; - var viewKey = template.dataTab; - views[viewKey] = {}; - - var viewKeys = []; - var noViewKeys = []; - - viewKeys.push(viewKey); - - var tamplateView = componentsViews.filter(function (view) { - return view.name === template.view; - }); - - if (tamplateView.length === 1) { - tamplateView = tamplateView[0]; - } else { - return false; - } - - - jQuery.each(template.sections, function (j, section) { - var viewTab = tabs.filter(function (tab) { - return tab.id === section; - }); - - if (viewTab.length === 1) { - section = viewTab[0]; - } else { - return false; - } - - var componentName = template.component; - if (section.hasOwnProperty('component')) { - componentName = section.component; - viewKey = section.dataTab; - views[viewKey] = {}; - noViewKeys.push(viewKey); - - } else { - viewKey = template.dataTab; - } - views[viewKey]['component'] = componentName; - tabViews[viewKey] = tamplateView.name; - if (section.hasOwnProperty('view')) { - tabViews[viewKey] = section.view; - } - - var sectionTab = tab.find(getDatatemplate(section.dataTab)); - var styleInputsKey = viewKey + section.dataTab + 'style'; - styleInputs[styleInputsKey] = { - inputs : sectionTab.find(getDatatemplate('style') + ' input, ' + getDatatemplate('style') + ' select') - }; - /*eslint complexity: ["error", 10]*/ - jQuery.each(section.options, function (j, option) { - var inputsKey = viewKey + section.dataTab + option.type; - var componentSelector = '[data-template="' + template.dataTab + '"] ' - + '[data-template="' + section.dataTab + '"] ' - + 'input[name="' + option.inputName + '"]'; - inputs[inputsKey] = { - input : sectionTab.find('input[name="' + option.inputName + '"]'), - optionName : option.name, - type : option.type, - tabSelector : '', - componentSelector : componentSelector - }; - - if (section.hasOwnProperty('component')) { - inputs[inputsKey]['tabSelector'] = '[data-template="' - + section.dataTab - + '"] input[name="' - + option.inputName - + '"]'; - } - - if (templateInputs.hasOwnProperty(inputsKey)) { - // inputs[inputsKey].input.val(templateInputs[inputsKey]); - if (inputs[inputsKey].tabSelector === '') { - jQuery(inputs[inputsKey].componentSelector).val(templateInputs[inputsKey]); - } else { - jQuery(inputs[inputsKey].tabSelector).val(templateInputs[inputsKey]); - } - - if (!options.hasOwnProperty(componentName)) { - options[componentName] = {}; - } - if (!styling.hasOwnProperty(componentName)) { - styling[componentName] = {}; - } - options[componentName][option.name] = templateInputs[inputsKey]; - styling[componentName][option.name] = templateStyleInputs[styleInputsKey]; - } else { - - if (stories.hasOwnProperty(componentName) && stories[componentName].hasOwnProperty(option.name)) { - var val = stories[componentName][option.name].content; - // inputs[inputsKey].input.val(val); - if (inputs[inputsKey].tabSelector === '') { - jQuery(inputs[inputsKey].componentSelector).val(val); - } else { - jQuery(inputs[inputsKey].tabSelector).val(val); - } - options[componentName] = {}; - options[componentName][option.name] = val; - } - } - - }); - fillCSSFields(styleInputsKey, templateStyleInputs, styleInputs); - }); - - - jQuery.each(viewKeys, function (j, viewKeyItem) { - if (!views[viewKeyItem].hasOwnProperty('component')) { - views[viewKeyItem]['component'] = template.component; - } - - views[viewKeyItem]['expanded'] = atmTemplating.render(template.name, tamplateView.expanded); - views[viewKeyItem]['expanded'].small(false); - views[viewKeyItem]['collapsed'] = atmTemplating.render(template.name, tamplateView.collapsed); - jQuery(tamplateView.expanded).attr('data-view-key', viewKeyItem); - jQuery(tamplateView.collapsed).attr('data-view-key', viewKeyItem); - atmTemplating.updateTemplate( - views[viewKeyItem]['component'], - options[views[viewKeyItem]['component']], - styling[views[viewKeyItem]['component']] - ); - views[viewKeyItem].expanded.redraw(); - views[viewKeyItem].collapsed.redraw(); - - views[viewKeyItem].expanded.watch('showModalBody', toggleTemplates); - views[viewKeyItem].collapsed.watch('showModalBody', toggleTemplates); - - }); }); + }); - var varError = false; - var throttledSync = jQuery.throttle(200, function (e) { - var thisValue = $(this).val(); - var dataTemplateCss = $(this).attr('data-template-css'); - var viewKey = jQuery(jQuery(this).parents('[data-template]')[2]).data('template'); - var tabKey = jQuery(jQuery(this).parents('[data-template]')[1]).data('template'); - varError = false; - jQuery.each(['expanded', 'collapsed'], function (i, type) { - var inputKey = viewKey + tabKey; - - if (inputs.hasOwnProperty(inputKey + type)) { - if (!checkInputVars(inputs[inputKey + type].input, viewKey)) { - varError = true; - return false; - } - options[views[viewKey].component][inputs[inputKey + type].optionName] = inputs[inputKey + type].input.val(); - styling[views[viewKey].component][inputs[inputKey + type].optionName] = - getCSSFields(styleInputs[inputKey + 'style'].inputs); - - } - else { - inputKey = tabKey + tabKey; - if (inputs.hasOwnProperty(inputKey + type)) { - var inputSelector = inputs[inputKey + type].tabSelector; - - var oldValue = options[views[tabKey].component][inputs[inputKey + type].optionName]; - var newValue = inputs[inputKey + type].input.val(); - if (!styling.hasOwnProperty(views[tabKey].component)) { - styling[views[tabKey].component] = {}; - } - jQuery.each(jQuery(inputSelector), function (i, item) { - if ($(item).val() !== oldValue) { - newValue = $(item).val(); - } - }); - - jQuery.each(jQuery(inputSelector), function (i, item) { - if ($(item).val() !== newValue) { - $(item).val(newValue); - } - }); - - var styleSelector = '[data-template="' + tabKey + '"] [data-template-css="' + dataTemplateCss + '"]'; - var newStyle = thisValue; - jQuery.each(jQuery(styleSelector), function (i, item) { - if (jQuery(item).val() !== newStyle) { - jQuery(item).val(newStyle); - } - }); - options[views[tabKey].component][inputs[inputKey + type].optionName] = newValue; - styling[views[tabKey].component][inputs[inputKey + type].optionName] = - getCSSFields(styleInputs[inputKey + 'style'].inputs); - if (!checkInputVars(inputs[inputKey + type].input, viewKey)) { - varError = true; - return false; - } + return form.validate({ + rules: rules, + errorClass: 'invalid', + onclick: false, + onkeyup: false, + onfocusout: false, + showErrors: function(errorMap, errorList) { + jQuery.each(errorList, function(i, item) { + if (!jQuery(item.element).hasClass('invalid')) { + jQuery(item.element).addClass('invalid'); + var label = jQuery(item.element).parents('.custom-label').find('label'); + if (!label[0]) { + label = jQuery(item.element).parents('.custom-input').find('label'); } + label.addClass('invalid'); } }); - if (!varError) { - var needToRedraw = []; - if (tabViews.hasOwnProperty(tabKey)) { - needToRedraw = tabViews[tabKey]; - - atmTemplating.updateTemplate( - views[tabKey].component, - options[views[tabKey].component], - styling[views[tabKey].component] - ); - } else { - needToRedraw = tabViews[viewKey]; - } - - if (!Array.isArray(needToRedraw)) { - needToRedraw = [needToRedraw]; - } - jQuery.each(needToRedraw, function (i, type) { - // update template - - atmTemplating.updateTemplate( - views[type].component, - options[views[type].component], - styling[views[type].component] - ); - // redraw the view - views[type].expanded.redraw(); - views[type].collapsed.redraw(); - views[type].expanded.watch('showModalBody', toggleTemplates); - views[type].collapsed.watch('showModalBody', toggleTemplates); - - }); + var errorsMeassge = ''; + jQuery.each(errorMap, function(i, item) { + errorsMeassge += '
' + item; + }); + if (errorsMeassge !== '') { + return notify('error', errorsMeassge); } - }); - - var $form = $('section.views-tabs'); - var $inputs = $form.find('input'); - var $selects = $form.find('select'); - var $colorInputs = $form.find('input[type="color"]'); - - $inputs.bind('keyup', throttledSync); - $colorInputs.bind('change', throttledSync); - $selects.bind('change', throttledSync); - - var overallSync = jQuery.throttle(200, function () { - applayOverallStyling(getOverallStyling()); - }); - jQuery('[data-template="overall-styling"] input').bind('keyup', overallSync); - jQuery('[data-template="overall-styling"] input[type="color"]').bind('change', overallSync); - - function addLoader(btn) { - var icon = btn.find('i'); - btn.addClass('disabled'); - icon.removeClass('mdi mdi-check'); - icon.addClass('fa fa-spinner fa-spin'); - } - - function removeLoader(btn) { - var icon = btn.find('i'); - btn.removeClass('disabled'); - icon.removeClass('fa fa-spinner fa-spin'); - icon.addClass('mdi mdi-check'); - } - - function showSuccess() { - notify('success', 'AdTechMedia parameters have been saved successfully'); - } - - function addValidate(form, rules, messages) { - jQuery.each(rules, function (name, item) { - jQuery('input[name="' + name + '"]').on('focus', function () { - var item = jQuery('input[name="' + name + '"]'); + }, + messages: messages + }); +} - if (jQuery(item).hasClass('invalid')) { - jQuery(item).removeClass('invalid'); - var label = jQuery(item).parents('.custom-label').find('label'); - if (!label[0]) { - label = jQuery(item).parents('.custom-input').find('label'); - } - label.removeClass('invalid'); - } - }); - }); - - return form.validate({ - rules : rules, - errorClass : 'invalid', - onclick : false, - onkeyup : false, - onfocusout : false, - showErrors : function (errorMap, errorList) { - jQuery.each(errorList, function (i, item) { - if (!jQuery(item.element).hasClass('invalid')) { - jQuery(item.element).addClass('invalid'); - var label = jQuery(item.element).parents('.custom-label').find('label'); - if (!label[0]) { - label = jQuery(item.element).parents('.custom-input').find('label'); - } - label.addClass('invalid'); - } - }); +function showError() { + notify( + 'error', + 'AdTechMedia parameters failed to save. Please retry or contact plugin support team.' + ); +} - var errorsMeassge = ''; - jQuery.each(errorMap, function (i, item) { - errorsMeassge += '
' + item; - }); - if (errorsMeassge !== '') { - return notify('error', errorsMeassge); - } - }, - messages : messages - }); - } +function getInputsData(inputs) { + var styles = {}; + jQuery.each(inputs, function (i, input) { + if (jQuery(input).val() !== '') { + if (jQuery(input).is(':checkbox')) { + styles[jQuery(input).attr('name')] = jQuery(input).prop('checked'); + } else { + styles[jQuery(input).attr('name')] = jQuery(input).val(); + } - function showError() { - notify( - 'error', - 'AdTechMedia parameters failed to save. Please retry or contact plugin support team.' - ); } + }); + return styles; +} - jQuery('.save-templates').bind('click', function (e) { - - if (!varError) { - var btn = jQuery(this); - var viewKey = jQuery(btn.parents('[data-template]')[0]).data('template'); - - //get compnents in this view - var viewComponents = {}; - jQuery.each(components, function (i, template) { - if (template.hasOwnProperty('view')) { - var templateView = template.view; - if (!Array.isArray(templateView)) { - templateView = [templateView]; - } - jQuery.each(templateView, function (i, view) { - if (view === viewKey) { - viewComponents[template.component] = atmTemplating.templateRendition(template.component).render( - options[template.component], - styling[template.component] - ); - } - }); - } - }); - - jQuery.each(tabs, function (i, template) { - if (template.hasOwnProperty('view')) { - var templateView = template.view; - if (!Array.isArray(templateView)) { - templateView = [templateView]; - } - jQuery.each(templateView, function (i, view) { - if (view === viewKey) { - viewComponents[template.component] = atmTemplating.templateRendition(template.component).render( - options[template.component], - styling[template.component] - ); - } - }); - } - }); - - var valid = addValidate(jQuery('#overall-styling-and-position'), { - width : { - required : true, - cssSize : true - }, - offset_top : { - required : true, - cssSize : true - }, - offset_left : { - required : true, - cssSize : true - }, - scrolling_offset_top : { - required : true, - cssSize : true - }, - border : { - required : true - }, - font_family : { - required : true - }, - box_shadow : { - required : true - }, - footer_border : { - required : true - } - }, { - width : { - required : 'The field \'Width\' is required.', - cssSize : 'The field \'Width\' must be valid CSS size.' - }, - offset_top : { - required : 'The field \'Offset top\' is required.', - cssSize : 'The field \'Offset top\' must be valid CSS size.' - }, - offset_left : { - required : 'The field \'Offset from center\' is required.', - cssSize : 'The field \'Offset from center\' must be valid CSS size.' - }, - scrolling_offset_top : { - required : 'The field \'Scrolling offset top\' is required.', - cssSize : 'The field \'Scrolling offset top\' must be valid CSS size.' - }, - border : { - required : 'The field \'Border\' is required.' - }, - font_family : { - required : 'The field \'Font Family\' is required.' - }, - box_shadow : { - required : 'The field \'Box Shadow\' is required.' - }, - footer_border : { - required : 'The field \'Footer Border\' is required.' - } - }); +function checkBtnRegister(checkbox) { + var button = jQuery('#btn-register'); + if (jQuery(checkbox).prop('checked')) { + jQuery(button).removeAttr('disabled'); + } else { + jQuery(button).attr('disabled', 'true'); + } +} - if (valid.form()) { - addLoader(btn); - jQuery.ajax({ - url : save_template.ajax_url, - type : 'post', - data : { - action : 'save_template', - nonce : save_template.nonce, - inputs : JSON.stringify(inputsToObject(inputs)), - styleInputs : JSON.stringify(styleInputsToObject(styleInputs)), - position : JSON.stringify(getPositionFields()), - overallStyles : getOverallStyling(), - overallStylesInputs : JSON.stringify(getOverallStylingFields()), - components : Object.keys(viewComponents), - templates : viewComponents - }, - success : function (response) { - removeLoader(btn); - showSuccess(); - }, - error : function (response) { - removeLoader(btn); - showError(); - } - }); - } - } else { - notify('error', 'Variable {' + invalidVar + '} is not defined.'); +jQuery().ready(function() { + jQuery('#save-revenue-model').bind('click', function(e) { + var btn = jQuery(this); + var form = jQuery('#save-revenue-model').parents('form'); + var valid = addValidate(jQuery(form), { + email: { + required: true, + email: true } - }); - - jQuery('#save-revenue-model').bind('click', function (e) { - var btn = jQuery(this); - var form = jQuery('#save-revenue-model').parents('form'); - var valid = addValidate(jQuery(form), { - email : { - required : true, - email : true - } - }, { - email : { - required : 'The field \'Email address\' is required.', - email : 'Your email address must be in the format of name@domain.com.' - } - }); - if (valid.form()) { - addLoader(btn); - jQuery.ajax({ - url : save_template.ajax_url, - type : 'post', - data : { - action : 'save_template', - nonce : save_template.nonce, - revenueMethod : jQuery('select[name="revenue_method"]').val() - }, - success : function (response) { - removeLoader(btn); - showSuccess(); - }, - error : function (response) { - removeLoader(btn); - showError(); - } - }); + }, { + email: { + required: 'The field \'Email address\' is required.', + email: 'Your email address must be in the format of name@domain.com.' } }); - jQuery('#country').bind('change', function (e) { - var country = jQuery(this); - var method = jQuery('#revenue_method'); - method.empty(); - $.each(country.find(':selected').data('methods'), function (key, value) { - method.append($('') - .attr('value', value).text(value)); - }); - }); - jQuery('#content-config button').bind('click', function (e) { - var btn = jQuery(this); - - var valid = addValidate(jQuery('#content-config'), { - price : { - required : true, - digits : true, - min : 1 - }, - payment_pledged : { - required : true, - digits : true - }, - content_offset : { - required : true, - digits : true, - min : 1 - }, - ads_video : { - required : false, - url : true - } - }, { - price : { - required : 'The field \'Content pricing\' is required.', - digits : 'The field \'Content pricing\' must be a number.' - }, - payment_pledged : { - required : 'The field \'Content paywall\' is required.', - digits : 'The field \'Content paywall\' must be a number.' + if (valid.form()) { + addLoader(btn); + jQuery.ajax({ + url: save_template.ajax_url, + type: 'post', + data: { + action: 'save_template', + nonce: save_template.nonce, + revenueMethod: jQuery('select[name="revenue_method"]').val() }, - content_offset : { - required : 'The field \'Content preview\' is required.', - digits : 'The field \'Content preview\' must be a number.' + success: function(response) { + removeLoader(btn); + showSuccess(); }, - ads_video : { - required : false, - url : 'The field \'Content preview\' must be a valid url.' + error: function(response) { + removeLoader(btn); + showError(); } }); - if (valid.form()) { - addLoader(btn); - jQuery.ajax({ - url : save_template.ajax_url, - type : 'post', - data : { - action : 'save_template', - nonce : save_template.nonce, - contentConfig : JSON.stringify(getInputsData( - jQuery('#content-config .content input,#content-config .content select') - )) - }, - success : function (response) { - removeLoader(btn); - showSuccess(); - }, - error : function (response) { - removeLoader(btn); - showError(); - } - }); + } + }); + jQuery('#country').bind('change', function(e) { + var country = jQuery(this); + var method = jQuery('#revenue_method'); + method.empty(); + jQuery.each(country.find(':selected').data('methods'), function(key, value) { + method.append(jQuery('') + .attr('value', value).text(value)); + }); + }); + jQuery('#content-config button').bind('click', function(e) { + var btn = jQuery(this); + + var valid = addValidate(jQuery('#content-config'), { + price: { + required: true, + digits: true, + min: 1 + }, + payment_pledged: { + required: true, + digits: true + }, + content_offset: { + required: true, + digits: true, + min: 1 + }, + ads_video: { + required: false, + url: true + } + }, { + price: { + required: 'The field \'Content pricing\' is required.', + digits: 'The field \'Content pricing\' must be a number.' + }, + payment_pledged: { + required: 'The field \'Content paywall\' is required.', + digits: 'The field \'Content paywall\' must be a number.' + }, + content_offset: { + required: 'The field \'Content preview\' is required.', + digits: 'The field \'Content preview\' must be a number.' + }, + ads_video: { + required: false, + url: 'The field \'Content preview\' must be a valid url.' } }); - - jQuery('.return-to-default-values').bind('click', function (e) { - jQuery.each(jQuery('button.btn'), function (i, button) { - addLoader(jQuery(button)); - }); - - - // addLoader(btn); + if (valid.form()) { + addLoader(btn); jQuery.ajax({ - url : return_to_default_values.ajax_url, - type : 'post', - data : { - action : 'return_to_default_values', - method : 'get_default_values', - nonce : save_template.nonce + url: save_template.ajax_url, + type: 'post', + data: { + action: 'save_template', + nonce: save_template.nonce, + contentConfig: JSON.stringify(getInputsData( + jQuery('#content-config .content input,#content-config .content select') + )) }, - success : function (response) { - response = jQuery.parseJSON(response); - //restore styling and positions - jQuery.each(response, function (form, values) { - jQuery.each(values, function (name, value) { - jQuery('#' + form + ' [name="' + name + '"]').val(value); - }); - if (form === 'overall-styling-and-position') { - window.templatePositionInputs = jQuery.parseJSON(values['template_position']); - fillPositionFields(); - window.templateOverallStylesInputs = templateOverallStylesInputsDefault; - fillOverallStylesFields(); - applayOverallStyling(values['template_overall_styles']); - } - }); - - jQuery.each(jQuery('.views-tabs input[data-template-css]'), function (i, input) { - jQuery(input).val(jQuery(input).attr('placeholder')); - }); - - jQuery.each(jQuery('.views-tabs select[data-template-css]'), function (i, select) { - var options = jQuery(select).find('option'); - jQuery(select).val(options[0].value); - }); - - jQuery.each(components, function (i, template) { - var viewKey = template.dataTab; - jQuery.each(template.sections, function (j, section) { - var viewTab = tabs.filter(function (tab) { - return tab.id === section; - }); - if (viewTab.length === 1) { - section = viewTab[0]; - } else { - return false; - } - var componentName = template.component; - if (section.hasOwnProperty('component')) { - componentName = section.component; - viewKey = section.dataTab; - } else { - viewKey = template.dataTab; - } - jQuery.each(section.options, function (j, option) { - var inputsKey = viewKey + section.dataTab + option.type; - if (stories.hasOwnProperty(componentName) && stories[componentName].hasOwnProperty(option.name)) { - var val = stories[componentName][option.name].content; - if (inputs[inputsKey].tabSelector === '') { - jQuery(inputs[inputsKey].componentSelector).val(val); - } else { - jQuery(inputs[inputsKey].tabSelector).val(val); - } - options[componentName][option.name] = val; - styling[componentName][inputs[inputsKey].optionName] = - getCSSFields(styleInputs[viewKey + section.dataTab + 'style'].inputs); - } - }); - }); - }); - - // render - var needToRender = []; - jQuery.each(views, function (i, view) { - atmTemplating.updateTemplate( - view.component, - options[view.component], - styling[view.component] - ); - - if (view.hasOwnProperty('expanded') && view.hasOwnProperty('collapsed')) { - needToRender.push(view); - } - }); - jQuery.each(needToRender, function (i, view) { - view.expanded.redraw(); - view.collapsed.redraw(); - }); - -//get compnents in this view - var viewComponents = {}; - jQuery.each(components, function (i, template) { - if (template.hasOwnProperty('view')) { - var templateView = template.view; - if (!Array.isArray(templateView)) { - templateView = [templateView]; - } - jQuery.each(templateView, function (i, view) { - viewComponents[template.component] = atmTemplating.templateRendition(template.component).render( - options[template.component], - styling[template.component] - ); - }); - } - }); - - jQuery.each(tabs, function (i, template) { - if (template.hasOwnProperty('view')) { - var templateView = template.view; - if (!Array.isArray(templateView)) { - templateView = [templateView]; - } - jQuery.each(templateView, function (i, view) { - viewComponents[template.component] = atmTemplating.templateRendition(template.component).render( - options[template.component], - styling[template.component] - ); - - }); - } - }); - - jQuery.ajax({ - url : return_to_default_values.ajax_url, - type : 'post', - data : { - action : 'return_to_default_values', - method : 'save_default_values', - nonce : save_template.nonce, - revenueMethod : jQuery('select[name="revenue_method"]').val(), - contentConfig : JSON.stringify(getInputsData( - jQuery('#content-config .content input,#content-config .content select') - )), - inputs : JSON.stringify(inputsToObject(inputs)), - styleInputs : JSON.stringify(styleInputsToObject(styleInputs)), - position : JSON.stringify(getPositionFields()), - overallStyles : getOverallStyling(), - overallStylesInputs : JSON.stringify(getOverallStylingFields()), - components : JSON.stringify(Object.keys(viewComponents)), - templates : JSON.stringify(viewComponents) - }, - success : function (response) { - notify('success', 'AdTechMedia parameters have been return to default values'); - - jQuery.each(jQuery('button.btn'), function (i, button) { - removeLoader(jQuery(button)); - }); - }, - error : function (response) { - jQuery.each(jQuery('button.btn'), function (i, button) { - removeLoader(jQuery(button)); - }); - showError(); - } - }); - + success: function(response) { + removeLoader(btn); + showSuccess(); }, - error : function (response) { - jQuery.each(jQuery('button.btn'), function (i, button) { - removeLoader(jQuery(button)); - }); + error: function(response) { + removeLoader(btn); showError(); } }); - }); - - - var generalConfValid = false; - var formGeneralConfig = jQuery('#general-config'); - jQuery('#btn-register').on('click', function (e) { - if (generalConfValid) { - formGeneralConfig.submit(); - } else { - e.preventDefault(); - - var valid = addValidate(formGeneralConfig, { - support_email : { - required : true, - email : true - } - }, { - support_email : { - required : 'The field \'Email address\' is required.', - email : 'Your email address must be in the format of name@domain.com.' - } - }); - - if (valid.form()) { - generalConfValid = true; - this.click(); - } - } - }); - })(jQuery); - - jQuery('#checkbox-sticky').on('change', function () { - if (!jQuery(this).prop('checked')) { - jQuery('.disable-if-sticky input').attr('disabled', 'disabled'); - } else { - jQuery('.disable-if-sticky input').removeAttr('disabled'); } }); - - function checkBtnRegister(checkbox) { - var button = jQuery('#btn-register'); - if (jQuery(checkbox).prop('checked')) { - jQuery(button).removeAttr('disabled'); - } else { - jQuery(button).attr('disabled', 'true'); - } - } - + var terms = jQuery('#terms'); - terms.on('change', function () { + + terms.on('change', function() { checkBtnRegister(this); }); - checkBtnRegister(terms); - - initModal(); - jQuery('#modal-content').load('https://www.adtechmedia.io/terms/dialog.html'); - - function firstSynch() { - jQuery('[data-template="user"] input[name]').trigger('keyup'); - jQuery('[data-template="user"] input[data-template-css]').trigger('keyup'); - jQuery('[data-template="user"] select[data-template-css]').trigger('change'); - jQuery('[data-template="user"] input[type="color"][data-template-css]').trigger('change'); - } - - firstSynch(); - - jQuery.get('https://www.adtechmedia.io/terms/dialog.html').done(function (data) { + jQuery.get(window.termsUrl).done(function(data) { jQuery('#modal-content').append(data); - }).fail(function () { - var str = 'https://www.adtechmedia.io/terms/dialog.html'; + }).fail(function() { + var str = '' + window.termsUrl + ''; jQuery('#modal-content').append(str); }); - - jQuery.validator.methods.cssSize = function (value, element) { - return this.optional(element) || /(auto|0)$|^[+-]?[0-9]+.?([0-9]+)?(px|em|ex|%|in|cm|mm|pt|pc)/.test(value); - } - - + + checkBtnRegister(terms); + initModal(); + + const saveTemplatesBtn = jQuery('#save-templates-config'); + const tplManager = atmTplManager(isLocalhost ? 'dev' : 'prod'); + const runtime = tplManager.rendition().render('#template-editor'); + + runtime.showSettings = true; + tplManager.client.bindLoader(runtime); + tplManager.generalSettings = appearanceSettings; + + tplManager + .authorizeAndSetup(apiKey, propertyId) + .then(function(exists) { + return exists + ? tplManager.fetch() + : tplManager.createDefaults( + propertyId, themeId, platformId, + themeVersion, platformVersion + ); + }) + .then(function() { + return tplManager.syncConfig(runtime); + }) + .then(function() { + function syncTemplates(notify) { + addLoader(saveTemplatesBtn); + tplManager.waitConfig(runtime) + .then(function() { + return tplManager.updateAll(); + }) + .then(function() { + jQuery.ajax({ + url: save_template.ajax_url, + type: 'post', + data: { + action: 'save_template', + nonce: save_template.nonce, + appearanceSettings: JSON.stringify(tplManager.generalSettings), + }, + success: function(response) { + removeLoader(saveTemplatesBtn); + notify && showSuccess(); + }, + error: function(response) { + removeLoader(saveTemplatesBtn); + showError(); + } + }); + }) + .catch(function(error) { + removeLoader(saveTemplatesBtn); + showError(); + }); + } + + saveTemplatesBtn.on('click', function() { + syncTemplates(true); + }); + + if (forceSaveTemplates) { + syncTemplates(); + } + }); }); diff --git a/src/lib/mozilla/wp-sw-manager/lib/js/sw-base.js b/src/lib/mozilla/wp-sw-manager/lib/js/sw-base.js index c2691b9..b9ad696 100644 --- a/src/lib/mozilla/wp-sw-manager/lib/js/sw-base.js +++ b/src/lib/mozilla/wp-sw-manager/lib/js/sw-base.js @@ -12,25 +12,25 @@ } else { return this.getLastCheck() - .then(function (timestamp) { - timestamp = timestamp || 0; - var now = Date.now(); - if (now - timestamp > this.ONE_DAY) { - return Promise.all([ - this.setLastCheck(now), - fetch(self.registration.active.scriptURL) - ]) - .then(function (results) { - var response = results[1]; - return response.text() - .then(function (contents) { - // In WP, '0' means no action or no callback found. Just what we want. - return Promise.resolve(contents === '0'); - }); - }.bind(this)); - } - return Promise.resolve(false); - }.bind(this)); + .then(function (timestamp) { + timestamp = timestamp || 0; + var now = Date.now(); + if (now - timestamp > this.ONE_DAY) { + return Promise.all([ + this.setLastCheck(now), + fetch(self.registration.active.scriptURL) + ]) + .then(function (results) { + var response = results[1]; + return response.text() + .then(function (contents) { + // In WP, '0' means no action or no callback found. Just what we want. + return Promise.resolve(contents === '0'); + }); + }); + } + return Promise.resolve(false); + }.bind(this)); } }, @@ -43,7 +43,7 @@ setLastCheck: function (value) { return this.storage.setItem('lastCheck', value) - .then(function() { this._lastCheck = Promise.resolve(value); }.bind(this)); + .then(function() { this._lastCheck = Promise.resolve(value); }.bind(this)); }, onFetch: function (event) { diff --git a/src/readme.txt b/src/readme.txt index c0f634a..7b0b327 100644 --- a/src/readme.txt +++ b/src/readme.txt @@ -8,8 +8,8 @@ Tags: adtech, advertising, micropayments, media, revenue License: MIT License URI: https://opensource.org/licenses/MIT Requires at least: 3.6 -Tested up to: 4.7.4 -Stable tag: 0.9.1 +Tested up to: 4.8 +Stable tag: 0.10.1 == Description == @@ -38,8 +38,11 @@ Meanwhile, frequently asked questions will be compiled and published as we evolv == Changelog == -= 0.9.1 = -- Fix hardcoded urls += 0.10 = +- Integrated improved authentication process +- Integrated template management widget +- Removed hard coded links +- Fixed code pipeline process = 0.9 = - Refactoring caching diff --git a/src/views/admin.php b/src/views/admin.php index 54034c5..790bca9 100644 --- a/src/views/admin.php +++ b/src/views/admin.php @@ -33,34 +33,25 @@ ]; $content_offset_types = [ 'paragraphs', 'words' ]; -echo '' . PHP_EOL; // @codingStandardsIgnoreEnd -echo ''; +if ( ! empty( $this->get_plugin_option( 'force-save-templates' ) ) ) { + $this->delete_plugin_option( 'force-save-templates' ); +} ?>
@@ -209,12 +186,7 @@
- create_form_control( - 'support_email', - $main_data['support_email'], - $this->get_plugin_option( 'support_email' ), - 'e.g. john.smith@mail.com' - ); ?> +
@@ -228,7 +200,7 @@ @@ -476,140 +448,13 @@

Templates management -
- +
+

- -
- -
-
-
Overall styling and position -
-
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
- Sticky -
- - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
-
-
-
-
-
-
-
-
- - -
-
+ +
+
@@ -620,5 +465,4 @@ class="mdi mdi-check"> Save -
diff --git a/src/views/template.php b/src/views/template.php deleted file mode 100644 index b488658..0000000 --- a/src/views/template.php +++ /dev/null @@ -1,2366 +0,0 @@ -
- - - - - - - -
-
-
pledge template -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
- -
-
- - -
-
-
-
- -
-
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
- -
-
- - -
-
- -
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
pay template
-
-
-
-
- Expanded view -
-
- -
- Collapsed view -
-
-
-
-
- - -
-
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
- -
-
- - -
-
- -
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
- -
-
- - -
-
- -
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
refund template
-
-
-
-
- Expanded view -
-
- -
- Collapsed view -
-
-
-
-
- - -
-
-
- -
-
- - -
-
- -
-
- -
-
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
- -
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
- -
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- - - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
-
-
-
- -
-
-
-
- -
-
-
other templates unlock view -
-
-
-
-
-
-
-

- It is a long established fact that a reader will be - distracted - by - the readable content of a page when looking at its layout. - The point of using Lorem Ipsum is that it has a more-or-less - normal - distribution of letters, as opposed to using 'Content here, - content here', - making it look like readable English. - It is a long established fact that a reader will be - distracted - by - the readable content of a page when looking at its layout. - The point of using Lorem Ipsum is that it has a more-or-less - normal - distribution of letters, as opposed to using 'Content here, - content here', - making it look like readable English. -

- -
-
-
-
-
-
- - -
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
-
-
-
-
-
- -
-
other templates price view -
-
-
-
-
-
-

Dear reader,

-
- Please support quality journalism - 5ยข to continue reading? - lease support quality journalism lease support quality journalism lease support quality journalism -
-
-
- - Already used us before? Connect here - -
-
-
-
-
-
-
- - -
-
- -
- - -
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
- -
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
- -
- - -
-
-
-
-
-
-
-
-
-
-
- -
-
-
\ No newline at end of file