Skip to content

Commit

Permalink
Merge pull request #213 from AdTechMedia/dev
Browse files Browse the repository at this point in the history
New version to master branch
  • Loading branch information
AlexanderC authored Aug 22, 2017
2 parents 94b1292 + c310e46 commit b0094bf
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/adtechmedia-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Adtechmedia_Config {
'sw_js_url' => 'https://manage.adtechmedia.io/atm-admin/atm-build/sw.min.js',
'tpl_js_url' => 'https://manage.adtechmedia.io/atm-admin/atm-build/atmTpl.js',
'tpl_mgmt_js_url' => 'https://manage.adtechmedia.io/atm-admin/atm-build/atmTplManager.js',
'terms_url' => 'https://www.adtechmedia.io/terms/dialog.html',
'terms_url' => 'https://manage.adtechmedia.io/deep-account/terms/dialog.html',
'register_url_tpl' => 'https://manage.adtechmedia.io/accounts/signup/%s',
'price' => 0.05,
'content_offset' => 2,
Expand Down Expand Up @@ -72,7 +72,7 @@ public static function setup_endpoints() {
self::$conf['tpl_js_url'] = 'https://manage-test.adtechmedia.io/atm-admin/atm-build/atmTpl.js';
self::$conf['tpl_mgmt_js_url'] = 'https://manage-test.adtechmedia.io/atm-admin/atm-build/atmTplManager.js';
self::$conf['register_url_tpl'] = 'https://manage-test.adtechmedia.io/accounts/signup/%s';
self::$conf['terms_url'] = 'https://www-test.adtechmedia.io/terms/dialog.html';
self::$conf['terms_url'] = 'https://manage-test.adtechmedia.io/deep-account/terms/dialog.html';
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/adtechmedia-optionsmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,11 @@ public function settings_page() {
* @param mixed $a_option_meta meta-data for $aOptionKey (either a string display-name or an array(display-name, option1, option2, ...).
* @param string $saved_option_value current value for $a_option_key.
* @param string $placeholder placeholder to field.
* @param string $type input type.
* @param string $input_options input options.
* @return void
*/
protected function create_form_control( $a_option_key, $a_option_meta, $saved_option_value, $placeholder = '' ) {
protected function create_form_control( $a_option_key, $a_option_meta, $saved_option_value, $placeholder = '', $type = 'text', $input_options = '' ) {
if ( is_array( $a_option_meta ) && count( $a_option_meta ) >= 2 ) { // Drop-down list.
$choices = array_slice( $a_option_meta, 1 );
?>
Expand All @@ -566,7 +568,9 @@ protected function create_form_control( $a_option_key, $a_option_meta, $saved_op

} else { // Simple input field.
?>
<input type="text" placeholder="<?php echo esc_html( $placeholder ) ?>"
<input <?php echo esc_html( $input_options ) ?>
type="<?php echo esc_html( $type ) ?>"
placeholder="<?php echo esc_html( $placeholder ) ?>"
name="<?php echo esc_html( $a_option_key ) ?>"
id="<?php echo esc_html( $a_option_key ) ?>"
value="<?php echo esc_attr( $saved_option_value ) ?>" size="100"/>
Expand Down
166 changes: 128 additions & 38 deletions src/adtechmedia-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
*/
class Adtechmedia_Plugin extends Adtechmedia_LifeCycle {

/**
* Variable indicate AMP view
*
* @var boolean $is_amp first value is false.
*/
protected $is_amp = false;

/**
* Variable indicate that ATM enabled
*
* @var boolean $enabled first value is null.
*/
public $enabled = null;

/**
* See: http://plugin.michael-simpson.com/?page_id=31
*
Expand Down Expand Up @@ -146,6 +160,7 @@ public function get_plugin_meta_data() {
'price_currency' => array( __( 'price.currency', 'adtechmedia-plugin' ) ),
'content_paywall' => array( __( 'content.paywall', 'adtechmedia-plugin' ) ),
'content_offset_type' => array( __( 'Offset type', 'adtechmedia-plugin' ) ),
'ab_percentage' => array( __( 'A/B target audience', 'adtechmedia-plugin' ) ),
);
}

Expand Down Expand Up @@ -291,6 +306,12 @@ public function add_actions_and_filters() {
'init_adtechmedia_AB',
)
);
add_filter( 'pre_amp_render_post',
array(
&$this,
'init_AMP',
)
);
add_filter( 'the_content',
array(
&$this,
Expand Down Expand Up @@ -339,6 +360,12 @@ public function add_actions_and_filters() {
);
}

/**
* Page is AMP
*/
public function init_AMP() {
$this->is_amp = true;
}
/**
* The first init function Adtechmedia_AB
*/
Expand Down Expand Up @@ -452,22 +479,12 @@ public function ajax_save_template() {
$revenue_method = sanitize_text_field( wp_unslash( $_POST['revenueMethod'] ) );
$this->update_plugin_option( 'revenue_method', $revenue_method );

$ab_percentage = (int) sanitize_text_field( wp_unslash( $_POST['abPercentage'] ) );
$this->update_plugin_option( 'ab_percentage', $ab_percentage );

$country = sanitize_text_field( wp_unslash( $_POST['country'] ) );
$this->update_plugin_option( 'country', $country );

$currency = sanitize_text_field( wp_unslash( $_POST['currency'] ) );
$this->update_plugin_option( 'price_currency', $currency );

// Adtechmedia_Request::property_update_config_by_array(
// $this->get_plugin_option( 'id' ),
// $this->get_plugin_option( 'key' ),
// [
// 'revenueMethod' => $revenue_method,
// ]
// );
Adtechmedia_Request::property_update(
$this->get_plugin_option( 'id' ),
$this->get_plugin_option( 'support_email' ),
Expand All @@ -478,8 +495,14 @@ public function ajax_save_template() {
// Adtechmedia_ContentManager::clear_all_content();
} else if ( isset( $_POST['contentConfig'] ) ) {
$content_config = json_decode( wp_unslash( $_POST['contentConfig'] ), true );

if ( isset( $content_config['ab_percentage'] ) ) {
$this->update_plugin_option( 'ab_percentage', (int) $content_config['ab_percentage'] );
unset($content_config['ab_percentage']);
}

foreach ( $content_config as $a_option_key => $a_option_meta ) {
if ( ! empty( $content_config[ $a_option_key ] ) ) {
if ( isset( $content_config[ $a_option_key ] ) || $content_config[ $a_option_key ] ) {
$this->update_plugin_option( $a_option_key, $content_config[ $a_option_key ] );
}
}
Expand Down Expand Up @@ -636,40 +659,45 @@ public function clear_cache_on_update( $post_id ) {
* @return bool
*/
public function is_enabled() {
if ( ! isset( $this->ab ) ) {
$percentage = (int) $this->get_plugin_option( 'ab_percentage', Adtechmedia_AB::DEFAULT_PERCENTAGE );
if ( null === $this->enabled ) {
if ( ! isset( $this->ab ) ) {
$percentage = (int) $this->get_plugin_option( 'ab_percentage', Adtechmedia_AB::DEFAULT_PERCENTAGE );

if ( $percentage <= 0 ) {
return false;
}
if ( $percentage <= 0 ) {
$this->is_enabled = false;
return $this->enabled;
}

$this->ab = Adtechmedia_AB::instance()->set_percentage( $percentage )->start();
}
$this->ab = Adtechmedia_AB::instance()->set_percentage( $percentage )->start();
}

$is_enabled = Adtechmedia_AB::SHOW === $this->ab->variant
&& is_single() && ! empty( $this->get_plugin_option( 'key' ) );
$is_enabled = Adtechmedia_AB::SHOW === $this->ab->variant
&& is_single() && ! empty( $this->get_plugin_option( 'key' ) );

if ( ! $is_enabled ) {
return false;
}
// @codingStandardsIgnoreStart
$data = array(
'time' => get_post_time( 'U', true ),
'url' => get_permalink(),
'categories' => join( ',', array_map( function ( $category ) {
if ( ! $is_enabled ) {
$this->is_enabled = false;
return $this->enabled;
}
// @codingStandardsIgnoreStart
$data = array(
'time' => get_post_time( 'U', true ),
'url' => get_permalink(),
'categories' => join( ',', array_map( function ( $category ) {
return $category->name;
}, get_the_category() ? get_the_category() : array() ) ),
'tags' => join( ',', array_map( function( $tag ) {
'tags' => join( ',', array_map( function( $tag ) {
return $tag->name;
}, get_the_tags() ? get_the_tags() : array() ) )
);
// @codingStandardsIgnoreEnd
return Adtechmedia_Request::br_decide_show(
$this->get_plugin_option( 'Id' ),
'load',
$data,
$this->get_plugin_option( 'key' )
);
);
// @codingStandardsIgnoreEnd
$this->enabled = Adtechmedia_Request::br_decide_show(
$this->get_plugin_option( 'Id' ),
'load',
$data,
$this->get_plugin_option( 'key' )
);
} // End if().
return $this->enabled;
}

/**
Expand All @@ -683,7 +711,15 @@ public function hide_content( $content ) {
if ( $this->is_enabled() ) {
$id = (string) get_the_ID();
$saved_content = Adtechmedia_ContentManager::get_content( $id );
if ( isset( $saved_content ) && ! empty( $saved_content ) ) {
if ( $this->is_amp ) {
add_action( 'amp_post_template_css',
array(
&$this,
'xyz_amp_my_additional_css_styles',
)
);
return $this->amp_content( $content , $id );
} else if ( isset( $saved_content ) && ! empty( $saved_content ) ) {
return $this->content_wrapper( $saved_content );
} else {
Adtechmedia_Request::content_create(
Expand Down Expand Up @@ -748,6 +784,60 @@ public function content_wrapper( $content ) {
return "<span id='content-for-atm-modal'>&nbsp;</span><span id='content-for-atm'>$content</span>" . $script;
}

/**
* AMP one paragraf
*
* @param string $content content of post.
* @param integer $id id of post.
*
* @return string
*/
public function amp_content( $content, $id ) {
$dom = new DOMDocument();
$dom->loadHTML( '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $content . '</body></html>' );
$result = $dom->getElementsByTagName( 'p' );
$html = '';
// @codingStandardsIgnoreStart
foreach ( $result as $paragraf ) {
if ( $paragraf->nodeValue ) {
$html .= $paragraf->ownerDocument->saveHTML( $paragraf );
break;
} else {
$html .= $paragraf->ownerDocument->saveHTML( $paragraf );
}

}
$html.= '<div class="atm-unlock-line"><a href="'. get_page_link($id) .'">Get full content</a></div>';
// @codingStandardsIgnoreEnd
return $html;
}

/**
* AMP button view
*/
public function xyz_amp_my_additional_css_styles() {
$css = '.atm-unlock-line{
text-align:center;
}
.atm-unlock-line a{
color: #fff;
background: #00a7f7;
font-size: 11px;
width: 190px;
display: block;
line-height: 35px;
text-transform: uppercase;
text-decoration: none;
margin: 0 auto;
border-radius: 2px;
font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, sans-serif;
}
';
// @codingStandardsIgnoreStart
echo $css;
// @codingStandardsIgnoreEnd
}

/**
* Show error if Property Id not exists
*/
Expand Down
8 changes: 0 additions & 8 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,6 @@ ul li {
float: right;
}

.radio {
cursor: pointer;
display: inline-block;
line-height: 22px;
padding: 10px 50px 0 0;
text-transform: uppercase;
}

.radio:hover .inner {
opacity: .5;
transform: scale(0.5);
Expand Down
5 changes: 3 additions & 2 deletions src/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ jQuery().ready(function() {
action: 'save_template',
nonce: save_template.nonce,
revenueMethod: jQuery('select[name="revenue_method"]').val(),
abPercentage: jQuery('input[name="ab_percentage"]').val(),
country: jQuery('select[name="country"]').val(),
currency: jQuery('#price_currency').val()
},
Expand Down Expand Up @@ -326,7 +325,7 @@ jQuery().ready(function() {
content_offset: {
required: true,
digits: true,
min: 1
min: 0
},
ads_video: {
required: false,
Expand Down Expand Up @@ -399,6 +398,8 @@ jQuery().ready(function() {
tplManager.client.bindLoader(runtime);
tplManager.generalSettings = appearanceSettings;

if (!apiKey) { return false }

tplManager
.authorizeAndSetup(apiKey, propertyId)
.then(function(exists) {
Expand Down
Loading

0 comments on commit b0094bf

Please sign in to comment.