<?php
/*
 * Plugin Name:       R2B2 Monetization
 * Description:       Maximize your profits today with programmatic advertising.
 * Version:           1.1.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            R2B2
 * Author URI:        https://r2b2.io/
 * License:           GPL-3.0-only
 * Text Domain:       r2b2-monetization
 * Domain Path:       /languages
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

require_once __DIR__ . '/constants.php';
require_once __DIR__ . '/utils.php';
require_once __DIR__ . '/options.php';
require_once __DIR__ . '/migrations.php';
require_once __DIR__ . '/plugin-uninstall.php';

add_action( 'plugins_loaded', 'r2b2_plugins_loaded' );
function r2b2_plugins_loaded() {
	load_plugin_textdomain( 'r2b2', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/', true );
}

/**
 * Insert placement scripts (sticky and vignette) and css styles into all pages.
 */
function r2b2_enqueue_script() {
	wp_enqueue_style( 'r2b2-styles', plugins_url( '/css/r2b2-styles.css', __FILE__ ), false, R2B2_PLUGIN_VERSION );
	$placements = r2b2_get_placement_data();
	if ( empty( $placements ) ) {
		return;
	}

	$deviceIsMobile = wp_is_mobile();

	foreach ( $placements as $placement ) {
		if ( empty( $placement ) || ! $placement[ R2B2_PL_DATA_DELIVERY ] ) {
			continue;
		}
		if ( in_array( r2b2_get_mediatype( $placement[ R2B2_PL_DATA_DGPM ]['p'] ), R2B2_FORMATS_WITHOUT_CONTAINER ) ) {
			if ( ! ! $deviceIsMobile === ! ! $placement[ R2B2_PL_DATA_DGPM ]['m'] ) {
				$showDemo = $placement[ R2B2_PL_DATA_DEMO ] && current_user_can( 'administrator' );
				r2b2_enqueue_placement( $placement[ R2B2_PL_DATA_DGPM ], $showDemo );
			}
		}
	}
}

add_action( 'wp_enqueue_scripts', 'r2b2_enqueue_script' );

/**
 * Automatic insertion of ads into posts.
 * Ads are placed in between paragraphs.
 */
add_filter( 'the_content', 'r2b2_insert_into_posts' );

function r2b2_insert_into_posts( $content ) {
	if ( ! empty( $content ) ) {
		$placements = r2b2_get_placement_data();
		if ( empty( $placements ) ) {
			return $content;
		}
		$placementsToInsert = [];

		$deviceIsMobile = wp_is_mobile();

		foreach ( $placements as $placement ) {
			if ( empty( $placement ) || ! $placement[ R2B2_PL_DATA_DELIVERY ] ) {
				continue;
			}
			if ( ! in_array( r2b2_get_mediatype( $placement[ R2B2_PL_DATA_DGPM ]['p'] ), R2B2_FORMATS_WITHOUT_CONTAINER ) ) {
				if ( ! ! $deviceIsMobile === ! ! $placement[ R2B2_PL_DATA_DGPM ]['m'] ) {
					if ( is_numeric( $placement[ R2B2_PL_DATA_PARAGRAPH_INDEX ] ) ) {
						$placementsToInsert[ $placement[ R2B2_PL_DATA_PARAGRAPH_INDEX ] ]   = $placementsToInsert[ $placement[ R2B2_PL_DATA_PARAGRAPH_INDEX ] ] ?? [];
						$placementsToInsert[ $placement[ R2B2_PL_DATA_PARAGRAPH_INDEX ] ][] = $placement;
					}
				}
			}
		}

		$paragraphs = explode( "</p>", $content );
		$content    = '';
		foreach ( $paragraphs as $index => $paragraph ) {
			$content .= $paragraph;
			$content .= "</p>";

			if ( ! empty( $placementsToInsert[ $index + 1 ] ) ) {
				$pls = $placementsToInsert[ $index + 1 ];
				foreach ( $pls as $pl ) {
					$placementDiv = 'r2b2-ad--' . preg_replace( '/[\x00-\x1F\x80-\xFF\/\.]/', '', join( '_', $pl[ R2B2_PL_DATA_DGPM ] ) );
					$selfpromo    = $pl[ R2B2_PL_DATA_DEMO ] && current_user_can( 'administrator' );
					$content      .= r2b2_placement_html( r2b2_delivery_url( $pl[ R2B2_PL_DATA_DGPM ], $selfpromo, $placementDiv ), $placementDiv );
				}
			}
		}
	}

	return $content;
}
