<?php
/**
 * Provides compatibility with SEOPressor
 * 
 * @package  Page Generator Pro
 * @author   Tim Carr
 * @version  2.9.6
 */
class Page_Generator_Pro_SEOPressor {

    /**
     * Holds the base object.
     *
     * @since   2.9.6
     *
     * @var     object
     */
    public $base;

    /**
     * Constructor
     * 
     * @since   2.9.6
     *
     * @param   object $base    Base Plugin Class
     */
    public function __construct( $base ) {

        // Store base class
        $this->base = $base;

        // Add Overwrite Section if SEOPressor enabled
        add_filter( 'page_generator_pro_common_get_content_overwrite_sections', array( $this, 'add_overwrite_section' ) );

        // Ignore SEOPressor meta keys if overwriting is disabled for SEOPressor
        add_filter( 'page_generator_pro_generate_set_post_meta_ignored_keys', array( $this, 'prevent_post_meta_copy_to_generated_content' ), 10, 4 );
    
    }

    /**
     * Defines available content overwrite sections.
     *
     * @since   2.9.6
     *
     * @param   array   $sections    Content Overwrite Sections
     * @return  array                Content Overwrite Sections
     */
    public function add_overwrite_section( $sections ) {

        // Bail if SEOPressor isn't active
        if ( ! $this->is_active() ) {
            return $sections;
        }

        // Add SEOPressor
        $sections['seopressor'] = __( 'SEOPressor', 'page-generator-pro' );

        // Return
        return $sections;

    }

    /**
     * Adds SEOPressor Post Meta Keys to the array of excluded Post Meta Keys if SEOPressor
     * metadata should not be overwritten on Content Generation
     *
     * @since   2.9.6
     *
     * @param   array   $ignored_keys   Ignored Keys (preg_match() compatible regex expressions are supported)
     * @param   int     $post_id        Generated Post ID
     * @param   array   $settings       Group Settings
     * @param   array   $post_args      wp_insert_post() / wp_update_post() arguments
     * @return  array   $ignored_keys   Ignored Keys
     */
    public function prevent_post_meta_copy_to_generated_content( $ignored_keys, $post_id, $settings, $post_args ) {

        // Determine if we want to create/replace SEOPressor metdata
        $overwrite = ( isset( $post_args['ID'] ) && ! array_key_exists( 'seopressor', $settings['overwrite_sections'] ) ? false : true ); 

        // If overwriting is enabled, no need to exclude anything
        if ( $overwrite ) {
            return $ignored_keys;
        }

        // Add SEOPressor Meta Keys so they are not overwritten on the Generated Post
        return array_merge( $ignored_keys, array(
            "/^_seop_(.*)/i"
        ) );

    }

    /**
     * Checks if the SEOPressor Plugin is active
     *
     * @since   2.9.6
     *
     * @return  bool    SEOPressor Plugin Active
     */
    private function is_active() {

        // Assume SEOPressor isn't active if we can't use WordPress' function
        if ( ! function_exists( 'is_plugin_active' ) ) {
            return false;
        }

        return is_plugin_active( 'seo-pressor/seo-pressor.php' );
        
    }

 }