@@ -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+ }
0 commit comments