| 1 |
<?php |
| 2 |
/* Begin Add Assets */ |
| 3 |
add_action( 'wp_insert_modules_js', 'wp_insert_module_inpostads_js', 0 ); |
| 4 |
function wp_insert_module_inpostads_js() { |
| 5 |
wp_register_script( 'wp-insert-module-inpostads-js', WP_INSERT_URL . 'includes/modules/general/inpost/js/module.js', [ 'wp-insert-js' ], WP_INSERT_VERSION . ( ( WP_INSERT_DEBUG ) ? wp_rand( 0, 9999 ) : '' ), true ); |
| 6 |
wp_enqueue_script( 'wp-insert-module-inpostads-js' ); |
| 7 |
} |
| 8 |
/* End Add Assets */ |
| 9 |
|
| 10 |
/* Begin UI Functions */ |
| 11 |
add_action( 'wp_insert_plugin_card', 'wp_insert_inpostads_plugin_card', 20 ); |
| 12 |
function wp_insert_inpostads_plugin_card() { |
| 13 |
wp_insert_get_plugin_card( |
| 14 |
'In-Post Ads', |
| 15 |
'<p>Ads shown within the post content.<br />You can choose different locations to insert ads from Above / Below / Inside / Middle Of / To the Left / To the Right of post content</p>', |
| 16 |
'inpostads', |
| 17 |
'In-Post Ad' |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
add_action( 'wp_ajax_wp_insert_inpostads_get_ad_form', 'wp_insert_inpostads_get_ad_form' ); |
| 22 |
function wp_insert_inpostads_get_ad_form() { |
| 23 |
wp_insert_get_ad_form( 'wp_insert_inpostads_primary_ad_code_type_change("###IDENTIFIER###"); wp_insert_inpostads_primary_ad_code_location_change_action("###IDENTIFIER###"); wp_insert_inpostads_vi_customize_adcode();' ); |
| 24 |
} |
| 25 |
add_action( 'wp_ajax_wp_insert_inpostads_save_ad_data', 'wp_insert_save_ad_data' ); |
| 26 |
add_action( 'wp_ajax_wp_insert_inpostads_delete_ad_data', 'wp_insert_delete_ad_data' ); |
| 27 |
|
| 28 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_location', 10, 3 ); |
| 29 |
function wp_insert_inpostads_form_accordion_tabs_location( $control, $identifier, $location ) { |
| 30 |
echo '<h3>Location</h3>'; |
| 31 |
echo '<div>'; |
| 32 |
$paragraphPositioningOptions = [ |
| 33 |
[ |
| 34 |
'text' => '1st', |
| 35 |
'value' => '1', |
| 36 |
], |
| 37 |
[ |
| 38 |
'text' => '2nd', |
| 39 |
'value' => '2', |
| 40 |
], |
| 41 |
[ |
| 42 |
'text' => '3rd', |
| 43 |
'value' => '3', |
| 44 |
], |
| 45 |
[ |
| 46 |
'text' => '4th', |
| 47 |
'value' => '4', |
| 48 |
], |
| 49 |
[ |
| 50 |
'text' => '5th', |
| 51 |
'value' => '5', |
| 52 |
], |
| 53 |
[ |
| 54 |
'text' => '6th', |
| 55 |
'value' => '6', |
| 56 |
], |
| 57 |
[ |
| 58 |
'text' => '7th', |
| 59 |
'value' => '7', |
| 60 |
], |
| 61 |
[ |
| 62 |
'text' => '8th', |
| 63 |
'value' => '8', |
| 64 |
], |
| 65 |
[ |
| 66 |
'text' => '9th', |
| 67 |
'value' => '9', |
| 68 |
], |
| 69 |
[ |
| 70 |
'text' => '10th', |
| 71 |
'value' => '10', |
| 72 |
], |
| 73 |
]; |
| 74 |
$control->add_control( |
| 75 |
[ |
| 76 |
'type' => 'select', |
| 77 |
'className' => 'input', |
| 78 |
'style' => 'display: inline;', |
| 79 |
'useParagraph' => false, |
| 80 |
'optionName' => 'paragraphtopposition', |
| 81 |
'options' => $paragraphPositioningOptions, |
| 82 |
] |
| 83 |
); |
| 84 |
$nthParagraphTopControl = $control->HTML; |
| 85 |
$control->clear_controls(); |
| 86 |
$control->add_control( |
| 87 |
[ |
| 88 |
'type' => 'select', |
| 89 |
'className' => 'input', |
| 90 |
'style' => 'display: inline;', |
| 91 |
'useParagraph' => false, |
| 92 |
'optionName' => 'paragraphbottomposition', |
| 93 |
'options' => $paragraphPositioningOptions, |
| 94 |
] |
| 95 |
); |
| 96 |
$nthParagraphBottomControl = $control->HTML; |
| 97 |
$control->clear_controls(); |
| 98 |
|
| 99 |
$location = ''; |
| 100 |
if ( ! isset( $control->values['location'] ) ) { |
| 101 |
switch ( $identifier ) { |
| 102 |
case 'above': |
| 103 |
$location = 'above'; |
| 104 |
break; |
| 105 |
case 'middle': |
| 106 |
$location = 'middle'; |
| 107 |
break; |
| 108 |
case 'below': |
| 109 |
$location = 'below'; |
| 110 |
break; |
| 111 |
case 'left': |
| 112 |
$location = 'left'; |
| 113 |
break; |
| 114 |
case 'right': |
| 115 |
$location = 'right'; |
| 116 |
break; |
| 117 |
default: |
| 118 |
$location = 'above'; |
| 119 |
break; |
| 120 |
} |
| 121 |
} else { |
| 122 |
$location = $control->values['location']; |
| 123 |
} |
| 124 |
$locations = [ |
| 125 |
[ |
| 126 |
'text' => 'Above Post Content', |
| 127 |
'value' => 'above', |
| 128 |
], |
| 129 |
[ |
| 130 |
'text' => 'Middle of Post Content', |
| 131 |
'value' => 'middle', |
| 132 |
], |
| 133 |
[ |
| 134 |
'text' => 'Below Post Content', |
| 135 |
'value' => 'below', |
| 136 |
], |
| 137 |
[ |
| 138 |
'text' => 'To the Left of Post Content', |
| 139 |
'value' => 'left', |
| 140 |
], |
| 141 |
[ |
| 142 |
'text' => 'To the Right of Post Content', |
| 143 |
'value' => 'right', |
| 144 |
], |
| 145 |
[ |
| 146 |
'text' => 'After ' . $nthParagraphTopControl . ' Paragraph in Post Content (From the Top)', |
| 147 |
'value' => 'paragraphtop', |
| 148 |
], |
| 149 |
[ |
| 150 |
'text' => 'After ' . $nthParagraphBottomControl . ' Paragraph in Post Content (From the Bottom)', |
| 151 |
'value' => 'paragraphbottom', |
| 152 |
], |
| 153 |
]; |
| 154 |
$control->add_control( |
| 155 |
[ |
| 156 |
'type' => 'radio-group', |
| 157 |
'style' => 'line-height: 40px; margin-top: 3px;', |
| 158 |
'optionName' => 'location', |
| 159 |
'options' => $locations, |
| 160 |
'value' => $location, |
| 161 |
] |
| 162 |
); |
| 163 |
$control->create_section( 'Location' ); |
| 164 |
wp_insert_echo_html( $control->HTML ); |
| 165 |
$control->clear_controls(); |
| 166 |
echo '</div>'; |
| 167 |
return $control; |
| 168 |
} |
| 169 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_adcode', 20, 3 ); |
| 170 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_rules', 30, 3 ); |
| 171 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_geo_targeting', 40, 3 ); |
| 172 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_devices_styles', 50, 3 ); |
| 173 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_form_accordion_tabs_notes', 60, 3 ); |
| 174 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_manual_override', 70, 3 ); |
| 175 |
add_filter( 'wp_insert_inpostads_form_accordion_tabs', 'wp_insert_inpostads_form_accordion_tabs_positioning', 80, 3 ); |
| 176 |
|
| 177 |
function wp_insert_inpostads_form_accordion_tabs_manual_override( $control, $identifier, $location ) { |
| 178 |
echo '<h3 class="wp_insert_inpostads_location_manual_override_panel">Manual Override</h3>'; |
| 179 |
echo '<div>'; |
| 180 |
$control->set_HTML( '<p class="codeSnippet"><code>[wpinsertinpostad id="' . $identifier . '"]</code></p><p>For those extreme cases when auto positioning just doesnt work out the way you want, use the shortcode above to precisely position your ad unit inside your post content.<br>Gutenberg users can utilize "Wp-Insert" blocks for manual positioning within post content.</p>' ); |
| 181 |
$control->create_section( 'Code to add to your post/page content' ); |
| 182 |
wp_insert_echo_html( $control->HTML ); |
| 183 |
$control->clear_controls(); |
| 184 |
echo '</div>'; |
| 185 |
return $control; |
| 186 |
} |
| 187 |
|
| 188 |
function wp_insert_inpostads_form_accordion_tabs_positioning( $control, $identifier, $location ) { |
| 189 |
echo '<h3 class="wp_insert_inpostads_location_middle_panel">Positioning</h3>'; |
| 190 |
echo '<div>'; |
| 191 |
$control->add_control( |
| 192 |
[ |
| 193 |
'type' => 'text', |
| 194 |
'label' => 'Minimum Character Count', |
| 195 |
'optionName' => 'minimum_character_count', |
| 196 |
'helpText' => 'Show the ad only if the Content meets the minimum character count. If this parameter is set to 0 (or empty) minimum character count check will be deactivated.', |
| 197 |
] |
| 198 |
); |
| 199 |
$control->add_control( |
| 200 |
[ |
| 201 |
'type' => 'text', |
| 202 |
'label' => 'Paragraph Buffer Count', |
| 203 |
'optionName' => 'paragraph_buffer_count', |
| 204 |
'helpText' => 'Shows the ad after X number of Paragraphs. If this parameter is set to 0 (or empty) the ad will appear in the middle of the content.', |
| 205 |
] |
| 206 |
); |
| 207 |
$control->create_section( 'Positioning' ); |
| 208 |
wp_insert_echo_html( $control->HTML ); |
| 209 |
$control->clear_controls(); |
| 210 |
echo '</div>'; |
| 211 |
return $control; |
| 212 |
} |
| 213 |
/* End UI Functions */ |
| 214 |
|
| 215 |
/* Begin In-Post Ads Ad Insertion */ |
| 216 |
add_filter( 'the_content', 'wp_insert_inpostads_the_content', 100 ); |
| 217 |
function wp_insert_inpostads_the_content( $content ) { |
| 218 |
global $post; |
| 219 |
if ( ! is_feed() && is_main_query() ) { |
| 220 |
$inpostads = get_option( 'wp_insert_inpostads' ); |
| 221 |
if ( isset( $inpostads ) && is_array( $inpostads ) ) { |
| 222 |
$shortcodeInsertions = []; |
| 223 |
if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches ) && array_key_exists( 2, $matches ) && in_array( 'wpinsertinpostad', $matches[2] ) ) { |
| 224 |
foreach ( $matches[2] as $key => $value ) { |
| 225 |
if ( 'wpinsertinpostad' === $value ) { |
| 226 |
$shortcodeAtts = shortcode_parse_atts( $matches[3][ $key ] ); |
| 227 |
if ( isset( $shortcodeAtts ) && isset( $shortcodeAtts['id'] ) && ( $shortcodeAtts['id'] != '' ) ) { |
| 228 |
$shortcodeInsertions[] = $shortcodeAtts['id']; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
$paragraphCount = wp_insert_inpostads_get_paragraph_count( $content ); |
| 235 |
foreach ( $inpostads as $key => $inpostad ) { |
| 236 |
if ( ! in_array( $key, $shortcodeInsertions ) ) { |
| 237 |
if ( ! isset( $inpostad['location'] ) ) { //Get the location value from the key for old users who doesnt have a location saved. |
| 238 |
switch ( $key ) { |
| 239 |
case 'above': |
| 240 |
$inpostad['location'] = 'above'; |
| 241 |
break; |
| 242 |
case 'middle': |
| 243 |
$inpostad['location'] = 'middle'; |
| 244 |
break; |
| 245 |
case 'below': |
| 246 |
$inpostad['location'] = 'below'; |
| 247 |
break; |
| 248 |
case 'left': |
| 249 |
$inpostad['location'] = 'left'; |
| 250 |
break; |
| 251 |
case 'right': |
| 252 |
$inpostad['location'] = 'right'; |
| 253 |
break; |
| 254 |
default: |
| 255 |
$inpostad['location'] = 'above'; |
| 256 |
break; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if ( wp_insert_get_ad_status( $inpostad ) ) { |
| 261 |
switch ( $inpostad['location'] ) { |
| 262 |
case 'above': |
| 263 |
$content = wp_insert_get_ad_unit( $inpostad ) . $content; |
| 264 |
break; |
| 265 |
case 'middle': |
| 266 |
if ( $paragraphCount > 1 ) { |
| 267 |
$paragraphBufferCount = $inpostad['paragraph_buffer_count'] ?? ''; |
| 268 |
$minimumCharacterCount = $inpostad['minimum_character_count'] ?? ''; |
| 269 |
if ( ( $paragraphBufferCount == 0 ) || ( $paragraphBufferCount == '' ) ) { |
| 270 |
$position = wp_insert_inpostads_get_insertion_position( '/p>', $content, round( $paragraphCount / 2 ) ); |
| 271 |
} else { |
| 272 |
$position = wp_insert_inpostads_get_insertion_position( '/p>', $content, $paragraphBufferCount ); |
| 273 |
} |
| 274 |
if ( $position ) { |
| 275 |
if ( ( $minimumCharacterCount == 0 ) || ( $minimumCharacterCount == '' ) ) { |
| 276 |
$content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); |
| 277 |
} elseif ( strlen( wp_strip_all_tags( $content ) ) > $minimumCharacterCount ) { |
| 278 |
$content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
break; |
| 283 |
case 'below': |
| 284 |
$content = $content . wp_insert_get_ad_unit( $inpostad ); |
| 285 |
break; |
| 286 |
case 'left': |
| 287 |
$content = wp_insert_get_ad_unit( $inpostad, 'float: left;' ) . $content; |
| 288 |
break; |
| 289 |
case 'right': |
| 290 |
$content = wp_insert_get_ad_unit( $inpostad, 'float: right;' ) . $content; |
| 291 |
break; |
| 292 |
case 'paragraphtop': |
| 293 |
if ( $paragraphCount > 1 ) { |
| 294 |
$position = wp_insert_inpostads_get_insertion_position( '/p>', $content, ( $inpostad['paragraphtopposition'] ?? 0 ) ); |
| 295 |
if ( $position ) { |
| 296 |
$content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); |
| 297 |
} |
| 298 |
} |
| 299 |
break; |
| 300 |
case 'paragraphbottom': |
| 301 |
if ( $paragraphCount > 1 ) { |
| 302 |
$paragraphbottomposition = ( $paragraphCount - (int) ( $inpostad['paragraphbottomposition'] ?? 0 ) ); |
| 303 |
if ( ( $paragraphbottomposition > 0 ) && ( $paragraphbottomposition < $paragraphCount ) ) { |
| 304 |
$position = wp_insert_inpostads_get_insertion_position( '/p>', $content, $paragraphbottomposition ); |
| 305 |
if ( $position ) { |
| 306 |
$content = substr_replace( $content, '/p>' . wp_insert_get_ad_unit( $inpostad ), $position, 3 ); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
return $content; |
| 318 |
} |
| 319 |
|
| 320 |
function wp_insert_inpostads_get_paragraph_count( $content ) { |
| 321 |
$paragraphs = explode( '/p>', $content ); |
| 322 |
$paragraphCount = 0; |
| 323 |
if ( is_array( $paragraphs ) ) { |
| 324 |
foreach ( $paragraphs as $paragraph ) { |
| 325 |
if ( strlen( $paragraph ) > 1 ) { |
| 326 |
++$paragraphCount; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
return $paragraphCount; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Byte offset of the $offset-th occurrence of $search within $content. |
| 335 |
* |
| 336 |
* @param string $search Delimiter to count (e.g. '/p>'). |
| 337 |
* @param string $content Content to scan. |
| 338 |
* @param int $offset 1-based occurrence to locate. |
| 339 |
* @return int|false Offset, or false when out of range. |
| 340 |
*/ |
| 341 |
function wp_insert_inpostads_get_insertion_position( $search, $content, $offset ) { |
| 342 |
$offset = (int) $offset; |
| 343 |
$segments = explode( $search, $content ); |
| 344 |
if ( ( $offset <= 0 ) || ( $offset > max( array_keys( $segments ) ) ) ) { |
| 345 |
return false; |
| 346 |
} |
| 347 |
return strlen( implode( $search, array_slice( $segments, 0, $offset ) ) ); |
| 348 |
} |
| 349 |
/* End In-Post Ads Ad Insertion */ |
| 350 |
|
| 351 |
/* Begin In-Post Ads Shortcode Ad Insertion */ |
| 352 |
add_shortcode( 'wpinsertinpostad', 'wp_insert_inpostads_get_shortcode' ); |
| 353 |
function wp_insert_inpostads_get_shortcode( $atts ) { |
| 354 |
$atts = shortcode_atts( [ 'id' => '' ], $atts, 'wpinsertinpostad' ); |
| 355 |
if ( isset( $atts['id'] ) && ( $atts['id'] != '' ) ) { |
| 356 |
$adunit = get_option( 'wp_insert_inpostads' ); |
| 357 |
if ( isset( $adunit ) && isset( $adunit[ $atts['id'] ] ) && is_array( $adunit[ $atts['id'] ] ) && wp_insert_get_ad_status( $adunit[ $atts['id'] ] ) ) { |
| 358 |
return wp_insert_get_ad_unit( $adunit[ $atts['id'] ] ); |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
/* End In-Post Ads Shortcode Ad Insertion */ |
| 363 |
|