Skip to content

Commit 7327d98

Browse files
committed
Show Ad After 50% of content feature added
1 parent a6255e0 commit 7327d98

File tree

4 files changed

+172
-56
lines changed

4 files changed

+172
-56
lines changed

admin/control-center.php

Lines changed: 83 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function adsforwp_insert_ads( $content ){
147147
if ( $show_ads != 'yes' ) {
148148
return $content ; // Do not show ads and return the content as it is
149149
}
150-
if(function_exists(ampforwp_is_front_page())){
150+
if(function_exists('ampforwp_is_front_page')){
151151
if(!is_singular() && !ampforwp_is_front_page()){
152152
return $content;
153153
}
@@ -163,16 +163,43 @@ function adsforwp_insert_ads( $content ){
163163

164164
}
165165
$selected_ads_for = get_post_meta(get_ad_id(get_the_ID()),'select_ads_for',true);
166+
$incontent_ad_type = get_post_meta(get_ad_id(get_the_ID()),'incontent_ad_type',true);
166167
if('1' === $selected_ads_for){
167-
$cmb2_incontent_options = get_metadata('post',get_ad_id(get_the_ID()), 'incontent_ad_type');
168-
$incontent_visibility = 'ad_visibility_status';
169-
$amp_ad_type = 'ad_type_format';
168+
if($incontent_ad_type == '1'){
169+
$closing_p = '</p>';
170+
$paragraphs = explode( $closing_p, $content );
171+
$paragraph_id = count($paragraphs);
172+
$paragraph_id = $paragraph_id / 2 ;
173+
$paragraph_id = round($paragraph_id);
174+
$cmb2_incontent_options = $paragraph_id;
175+
$incontent_visibility = 'ad_visibility_status';
176+
$amp_ad_type = 'ad_type_format';
177+
}
178+
elseif($incontent_ad_type == '2'){
179+
$cmb2_incontent_options = get_metadata('post',get_ad_id(get_the_ID()), 'incontent_ad_type_paragraph');
180+
$incontent_visibility = 'ad_visibility_status';
181+
$amp_ad_type = 'ad_type_format';
182+
}
170183

171184
}
172-
elseif('2' === $selected_ads_for){
173-
$cmb2_incontent_options = get_metadata('post',get_ad_id(get_the_ID()), '_amp_incontent_ad_type');
174-
$incontent_visibility = '_amp_ad_visibility_status';
175-
$amp_ad_type = '_amp_ad_type_format';
185+
elseif('2' === $selected_ads_for){
186+
$amp_incontent_ad_type = get_post_meta(get_ad_id(get_the_ID()),'_amp_incontent_ad_type',true);
187+
188+
if($amp_incontent_ad_type == '1'){
189+
$closing_p = '</p>';
190+
$paragraphs = explode( $closing_p, $content );
191+
$paragraph_id = count($paragraphs);
192+
$paragraph_id = $paragraph_id / 2 ;
193+
$paragraph_id = round($paragraph_id);
194+
$cmb2_incontent_options = $paragraph_id;
195+
$incontent_visibility = 'ad_visibility_status';
196+
$amp_ad_type = 'ad_type_format';
197+
}
198+
elseif($amp_incontent_ad_type == '2'){
199+
$cmb2_incontent_options = get_metadata('post',get_ad_id(get_the_ID()), '_amp_incontent_ad_type_paragraph');
200+
$incontent_visibility = 'ad_visibility_status';
201+
$amp_ad_type = 'ad_type_format';
202+
}
176203
}
177204

178205
//Get all other adds which are set to inline
@@ -214,10 +241,26 @@ function adsforwp_insert_ads( $content ){
214241
? $adsVisiblityType[$adsPostId]['visibility']
215242
: get_post_meta($adsPostId,'ad_visibility_status',true)
216243
);
244+
245+
$incontent_ad_type = get_post_meta(get_ad_id(get_the_ID()),'incontent_ad_type',true);
246+
247+
if($incontent_ad_type == '1'){
248+
$closing_p = '</p>';
249+
$paragraphs = explode( $closing_p, $content );
250+
$paragraph_id = count($paragraphs)-1;
251+
$paragraph_id = $paragraph_id / 2 ;
252+
$paragraph_id = round($paragraph_id);
253+
254+
$adsparagraphs = ( isset($post_meta[$adsPostId]['paragraph'])
255+
? $post_meta[$adsPostId]['paragraph']
256+
: $paragraph_id );
257+
}
258+
elseif($incontent_ad_type == '2'){
259+
217260
$adsparagraphs = ( isset($post_meta[$adsPostId]['paragraph'])
218261
? $post_meta[$adsPostId]['paragraph']
219-
: get_post_meta($adsPostId,'incontent_ad_type',true) );
220-
262+
: get_post_meta($adsPostId,'incontent_ad_type_paragraph',true) );
263+
}
221264

222265
$ad_vendor = get_post_meta($adsPostId,'ad_vendor',true);
223266
$ad_type = get_post_meta($adsPostId,'ad_type_format',true);
@@ -260,20 +303,40 @@ function adsforwp_insert_ads( $content ){
260303
'paragraph' => $adsparagraphs,
261304
'content'=>$adsContent,
262305
);
306+
263307
}
264308

265309
}
266310
}
267-
if('2' === $selected_ads_for){
311+
if('2' === $selected_ads_for){
268312
if('on' === $non_amp_ads || function_exists('is_amp_endpoint') && is_amp_endpoint()){
269313
$adsVisiblityType = get_post_field('adsforwp_incontent_ads_default', $post_id);
270314
$adsVisiblityType = (isset($adsVisiblityType[$adsPostId]['visibility'])
271315
? $adsVisiblityType[$adsPostId]['visibility']
272316
: get_post_meta($adsPostId,'_amp_ad_visibility_status',true)
273317
);
318+
319+
320+
$amp_incontent_ad_type = get_post_meta(get_ad_id(get_the_ID()),'_amp_incontent_ad_type',true);
321+
322+
if($amp_incontent_ad_type == '1'){
323+
$closing_p = '</p>';
324+
$paragraphs = explode( $closing_p, $content );
325+
$paragraph_id = count($paragraphs)-1;
326+
$paragraph_id = $paragraph_id / 2 ;
327+
$paragraph_id = round($paragraph_id);
328+
329+
$adsparagraphs = ( isset($post_meta[$adsPostId]['paragraph'])
330+
? $post_meta[$adsPostId]['paragraph']
331+
: $paragraph_id );
332+
}
333+
elseif($amp_incontent_ad_type == '2'){
334+
274335
$adsparagraphs = ( isset($post_meta[$adsPostId]['paragraph'])
275336
? $post_meta[$adsPostId]['paragraph']
276-
: get_post_meta($adsPostId,'_amp_incontent_ad_type',true) );
337+
: get_post_meta($adsPostId,'_amp_incontent_ad_type_paragraph',true) );
338+
}
339+
277340
$ad_vendor = get_post_meta($adsPostId,'_amp_ad_vendor',true);
278341
$ad_type = get_post_meta($adsPostId,'_amp_ad_type_format',true);
279342
if('1' === $ad_vendor && '2' === $ad_type){
@@ -354,6 +417,7 @@ function adsforwp_insert_ads( $content ){
354417
}
355418

356419
}
420+
357421
wp_reset_query();
358422
ksort($post_meta);
359423
$isPagesSplits = false;
@@ -369,31 +433,27 @@ function adsforwp_insert_ads( $content ){
369433
$content = preg_split("/\\r\\n|\\r|\\n/", $content);
370434
if(count($post_meta)>0){
371435
foreach ($post_meta as $key => $adsValue) {
436+
372437
if(!empty($adsValue) && $adsValue['visibility']!="show"){
373438
continue;
374439
}
375440
if(isset($adsValue['paragraph']) && isset($adsValue['content'])){
376-
// var_dump(count($content));var_dump($adsValue['paragraph']);
441+
377442
if(count($content) > intval($adsValue['paragraph'])){
378-
379-
$content[$adsValue['paragraph']] .= $adsValue['content'];
380-
}
381-
382-
//array_splice( $content, $adsValue['paragraph'], 0, $adsValue['content'] );
443+
$content[$adsValue['paragraph']-1] .= $adsValue['content'];
444+
}
383445
}
384446

385447
}
386448

387449
$completeContents .= implode(' ', $content);
388-
}
389-
450+
}
390451
//Check for page splits
391452
if($isPagesSplits){
392453
$completeContents .= '!--nextpage--';
393454
}
394455
}
395-
396-
return $completeContents;
456+
return $completeContents;
397457
}
398458

399459
/*
@@ -418,4 +478,4 @@ function adsforwp_admin_enqueue() {
418478
// Enqueued script with localized data.
419479
wp_enqueue_script( 'adsforwp-admin-js' );
420480

421-
}
481+
}

admin/global-metaboxes.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ function adsforwp_get_meta_post( $value, $post_id = '' ) {
1818
if ( $value === 'adsforwp_incontent_ads_paragraphs') {
1919
$selected_ads_for = get_post_meta(get_ad_id(get_the_ID()),'select_ads_for',true);
2020
if('1' === $selected_ads_for){
21-
$doint = get_post_meta(get_ad_id(get_the_ID()),'incontent_ad_type',true);
21+
$doint = get_post_meta(get_ad_id(get_the_ID()),'incontent_ad_type_paragraph',true);
2222
$default = $doint;
2323
}
2424
elseif('2' === $selected_ads_for){
25-
$default = get_post_meta(get_ad_id(get_the_ID()),'_amp_incontent_ad_type',true);
25+
$default = get_post_meta(get_ad_id(get_the_ID()),'_amp_incontent_ad_type_paragraph',true);
2626

2727
}
2828

@@ -45,9 +45,9 @@ function adsforwp_get_meta_post( $value, $post_id = '' ) {
4545
}
4646
$selected_ads_for = get_post_meta($key,'select_ads_for',true);
4747
if('1' === $selected_ads_for){
48-
$cpt_paragraph = get_post_meta($key,'incontent_ad_type',true);
48+
$cpt_paragraph = get_post_meta($key,'incontent_ad_type_paragraph',true);
4949
}elseif('2' === $selected_ads_for){
50-
$cpt_paragraph = get_post_meta($key,'_amp_incontent_ad_type',true);
50+
$cpt_paragraph = get_post_meta($key,'_amp_incontent_ad_type_paragraph',true);
5151
}
5252
$cpt_paragraph = $cpt_paragraph[0];
5353
$value['post_id'] = get_the_ID();
@@ -185,15 +185,15 @@ function adsforwp_generate_ad_post_type_data(){
185185
$ad_type = get_post_meta($ad->ID, 'ad_visibility_status', true );
186186

187187
}
188-
elseif('1' === $selected_ads_for){
188+
elseif('2' === $selected_ads_for){
189189

190190
$ad_type = get_post_meta($ad->ID, '_amp_ad_visibility_status', true );
191191
}
192192

193193
$ads_post_id = $ad->ID;
194194
$ad_type = adsforwp_get_meta_post( 'adsforwp_ads_position', $ads_post_id );
195195
$visibility = adsforwp_get_meta_post( 'adsforwp_incontent_ads_default', $ads_post_id );
196-
$paragraph = adsforwp_get_meta_post( 'incontent_ad_type', $ads_post_id );
196+
$paragraph = adsforwp_get_meta_post( 'incontent_ad_type_paragraph', $ads_post_id );
197197

198198
if ( 'show' === $ad_type ) {
199199

assets/ads.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ $("#select_ads_for").change(function(){
2525
}).change();
2626

2727
// AMPforWP Ads Options
28-
29-
30-
31-
3228
$("#ad_type_format").change(function(){
3329
$(this).find("option:selected").each(function(){
3430
var optionValue = $(this).attr("value");
@@ -38,13 +34,15 @@ $("#select_ads_for").change(function(){
3834
$("[id=dfp_parallax]").hide();
3935
$("[id=custom_parallax]").hide();
4036
$("[id=medianet_parallax]").hide();
37+
4138
$(".ad-type.ad-type-" + optionValue).show();
4239

4340
if('2' == optionValue){
44-
$("[id=adsense_parallax]").show();
45-
$("[id=dfp_parallax]").show();
46-
$("[id=custom_parallax]").show();
47-
$("[id=medianet_parallax]").show();
41+
$(".incontent-ad-type-2").hide();
42+
$("[id=adsense_parallax]").show();
43+
$("[id=dfp_parallax]").show();
44+
$("[id=custom_parallax]").show();
45+
$("[id=medianet_parallax]").show();
4846
}
4947

5048

@@ -55,7 +53,20 @@ $("#select_ads_for").change(function(){
5553
});
5654
}).change();
5755

56+
// AMPforWP Incontent Ad type
57+
$("#incontent_ad_type").change(function(){
58+
$(this).find("option:selected").each(function(){
59+
var optionValue = $(this).attr("value");
60+
if('2' == optionValue){
61+
$(".incontent-ad-type-2").show();
62+
}
63+
else{
64+
$(".incontent-ad-type-2").hide();
5865

66+
}
67+
});
68+
}).change();
69+
// AMPforWP Incontent Ad type end here
5970

6071
$("#ad_vendor").change(function(){
6172
$(this).find("option:selected").each(function(){
@@ -163,17 +174,33 @@ $("#_amp_ad_type_format").change(function(){
163174
$("[id=_amp_medianet_parallax]").hide();
164175
$(".amp-ad-type.amp-ad-type-" + optionValue).show();
165176
if('2' == optionValue){
166-
$("[id=_amp_adsense_parallax]").show();
167-
$("[id=_amp_dfp_parallax]").show();
168-
$("[id=_amp_custom_parallax]").show();
169-
$("[id=_amp_medianet_parallax]").show();
177+
$(".amp-incontent-ad-type-2").hide();
178+
$("[id=_amp_adsense_parallax]").show();
179+
$("[id=_amp_dfp_parallax]").show();
180+
$("[id=_amp_custom_parallax]").show();
181+
$("[id=_amp_medianet_parallax]").show();
170182
}
171183
} else{
172184
$(".amp-ad-type").hide();
173185
}
174186
});
175187
}).change();
176188

189+
// AMP by Automattic Incontent Ad type
190+
$("#_amp_incontent_ad_type").change(function(){
191+
$(this).find("option:selected").each(function(){
192+
var optionValue = $(this).attr("value");
193+
if('2' == optionValue){
194+
$(".amp-incontent-ad-type-2").show();
195+
}
196+
else{
197+
$(".amp-incontent-ad-type-2").hide();
198+
199+
}
200+
});
201+
}).change();
202+
// AMP by Automattic Incontent Ad type end here
203+
177204
$("#_amp_ad_vendor").change(function(){
178205
$(this).find("option:selected").each(function(){
179206
var optionValue = $(this).attr("value");

0 commit comments

Comments
 (0)