<?php

// WordPress function - registers the image size for the newsletter header image - 660px x 200px
add_image_size( 'newsletter-header', 660, 200, true );


/**
 * Replaces merge tags in newsletter content
 *
 * This function replaces merge tags that are specified in the function. The 
 * merge tags are replaced with the relevant content, see example. Merge tags 
 * are properly escaped. There is currently a different function to replace 
 * merge tags in the main website content.
 *
 * @function ow_newsletter_merge_tags()
 *
 * @uses get_field(), ow_global_var()
 *
 * @example '/\%\%tel\%\%/' => ow_global_var('accountant_phone'),
 *
 * @param (string) $input WordPress content from the newsletter
 *
 * @return (string) WordPress newsletter content
 *
 * @since 1.0.0 
 *
 */
function ow_newsletter_merge_tags( $input ) {

    $merge_tags_list = array(
        '/\%\%date\%\%/' => ow_tax_news_date( 'F' ),
        '/\%\%title\%\%/' => get_field( 'newsletter_title' ),
        '/\%\%company\%\%/' => ow_global_var( 'accountant_name' ),
        '/\%\%tel\%\%/' => ow_global_var( 'accountant_phone' ),
        '/\%\%email\%\%/' => '<a href="mailto:' . ow_global_var( 'accountant_email' ) . '">' . ow_global_var( 'accountant_email' ) . '</a>',
        '/\%\%website\%\%/' => '<a href="http://www.' . ow_global_var( 'accountant_website' ) . '">' . ow_global_var( 'accountant_website' ) . '</a>',
        '/\%\%area\%\%/' => ow_global_var( 'accountant_main_area' ),
    );

    foreach ( $merge_tags_list as $tag => $value ) {

        $input = preg_replace( $tag, $value, $input );
    }
    return $input;
}


/**
 * Returns if the website package includes the newsletter
 *
 * This function returns either true or false if the website package includes
 * the newsletter.
 *
 * @function has_newsletter()
 *
 * @uses is_basic_package()
 *
 * @param N/A
 *
 * @return (boolean) if the website package is basic
 *
 * @since 1.0.0 
 *
 */
function has_newsletter() {

    if ( is_package_1() || is_package_2() ) {

        return false;
    }
    if ( opt_newsletter() ) {
        return false;
    }
    return true;
}

/**
 * Redirects newsletter page to 404 page
 *
 * This function redirects the newsletter page to the 404 page if the website
 * package doesn't include the newsletter, This is to prevent the wrong packages
 * being able to access the newsletter.
 *
 * @function check_newsletter_status()
 *
 * @uses wp_redirect(), home_url()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.1.8 
 *
 */
function check_newsletter_status() {

    // This is a WP Super Cache constant to prevent the newsletter page being cached.
    define( 'DONOTCACHEPAGE', true );
    // This is a Litespeed Cache constant to prevent the newsletter page being cached.
    define( 'LSCACHE_NO_CACHE', true );

    if ( !has_newsletter() ) {

        wp_redirect( home_url( '404' ) );
        exit;
    }
}


/**
 * Removes the newsletter/tax tips page from WP admin
 *
 * This function removes the newsletter edit page and the Tax Tips & News edit
 * page from WordPress admin if the website package doesn't include the
 * newsletter. This is to prevent the wrong packages being able to access the 
 * newsletter.
 *
 * @function exclude_newsletter_from_admin()
 *
 * @uses has_newsletter()
 *
 * @param (object) $query the WordPress query object
 *
 * @return (object) the altered object
 *
 * @since 1.0.12 
 *
 */
function exclude_newsletter_from_admin( $query ) {

    if ( !has_newsletter() ) {

        $query->query_vars[ 'post__not_in' ] = array( 58, 397 ); // tax tips, newsletter
        return $query;
    }
}
add_filter( 'parse_query', 'exclude_newsletter_from_admin' );


/**
 * Publish/unpublish the newsletter
 *
 * This function will, depending on the website package, publish or unpublish
 * the newsletter. This is to prevent the wrong packages being able to access 
 * the newsletter. For whatever reason, likely the order or events on the 
 * WordPress query, this function wouldn't work using has_newsletter() function 
 * so the website package has to be coded here too.
 *
 * @function update_newsletter_status()
 *
 * @uses wp_update_post()
 *
 * @param (string) $status the website package title
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function update_newsletter_status( $status ) {

    if ( $status === PACKAGE_1 || $status === PACKAGE_2 ) {
        $postType = 'page';
        $leadTitle = 'Archived Tax Tips and News';
        global $wpdb;
        $query = $wpdb->get_row( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = "' . $leadTitle . '" AND post_type = "' . $postType . '"' );
        
            wp_update_post( array( 'ID' => $query->ID, 'post_status' => 'draft' ) );
        
        wp_update_post( array( 'ID' => 397, 'post_status' => 'draft' ) );
    } else {
        $postType = 'page';
        $leadTitle = 'Archived Tax Tips and News';
        global $wpdb;
        $query = $wpdb->get_row( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = "' . $leadTitle . '" AND post_type = "' . $postType . '"' );
        
            wp_update_post( array( 'ID' => $query->ID, 'post_status' => 'publish' ) );
        
        wp_update_post( array( 'ID' => 397, 'post_status' => 'publish' ) );
    }
}

function update_opt_newsletter_status() {

    if ( !has_newsletter() ) {
        $postType = 'page';
        $leadTitle = 'Archived Tax Tips and News';
        global $wpdb;
        $query = $wpdb->get_row( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = "' . $leadTitle . '" AND post_type = "' . $postType . '"' );
            wp_update_post( array( 'ID' => $query->ID, 'post_status' => 'draft' ) );
        
        wp_update_post( array( 'ID' => 397, 'post_status' => 'draft' ) );
return '<ul id=""site-map"">' . wp_list_pages( array( 'title_li' => '', 'depth' => 4, 'echo' => false, 'exclude' => 58, 397, $query->ID ) ) . '</ul>';
    } else if ( has_newsletter() ) {
        $postType = 'page';
        $leadTitle = 'Archived Tax Tips and News';
        global $wpdb;
        $query = $wpdb->get_row( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_title = "' . $leadTitle . '" AND post_type = "' . $postType . '"' );
       
            wp_update_post( array( 'ID' => $query->ID, 'post_status' => 'publish' ) );
        
        wp_update_post( array( 'ID' => 397, 'post_status' => 'publish' ) );
        
    }
}
/**
 * Newsletter PDF download button
 *
 * This function will, depending on the website package, 
 * show the option to download the newsletter as PDF for the client.
 *
 * @function newsletter_template()
 *
 * @uses template_redirect()
 *
 * @returns template from template folder
 *
 * @since 1.5.4
 *
 */
add_action( "template_redirect", 'newsletter_template' );

function newsletter_template() {
    global $wp;
    global $post;
    $plugindir = dirname( __FILE__ );
    if ( $post->post_name == 'newsletter' ) {
        $templatefilename = 'newsletter.php';

        $return_template = $plugindir . '/template/' . $templatefilename;
        //use $return_template = TEMPLATEPATH . '/page-' . $templatefilename; on functions.php inside templates folder when overwriting this function to show custom theme template for newsletter.
        do_theme_redirect( $return_template );
    }
}

function create_acf_fields_for_newsletter() {

    $group_title = 'Newsletter Colour Scheme';

    // Check if the field group exists
    $field_groups = acf_get_field_groups();

    // Loop through field groups to find a match based on the title
    foreach ( $field_groups as $field_group ) {
        if ( $field_group[ 'title' ] === $group_title ) {
            return; // Field group already exists, no need to create it again
        }
    }
    if ( function_exists( 'acf_add_local_field_group' ) ):
        acf_add_local_field_group( array(
            'key' => 'group_5b9fef27iris',
            'title' => $group_title,
            'fields' => array(
                array(
                    'key' => 'field_head_bg',
                    'label' => 'Head Background',
                    'name' => 'head_bg',
                    'type' => 'color_picker',
                    'default_value' => '#ffffff',
                ),
                array(
                    'key' => 'field_nav_bg',
                    'label' => 'Nav Background',
                    'name' => 'nav_bg',
                    'type' => 'color_picker',
                    'default_value' => '#a0a0a0',
                ),
                array(
                    'key' => 'field_title_bg',
                    'label' => 'Title Background',
                    'name' => 'title_bg',
                    'type' => 'color_picker',
                    'default_value' => '#a0a0a0',
                ),
                array(
                    'key' => 'field_item_bg',
                    'label' => 'Item Background',
                    'name' => 'item_bg',
                    'type' => 'color_picker',
                    'default_value' => '#f2f2f2',
                ),
                array(
                    'key' => 'field_footer_bg',
                    'label' => 'Footer Background',
                    'name' => 'footer_bg',
                    'type' => 'color_picker',
                    'default_value' => '#000000',
                ),
                array(
                    'key' => 'field_main_title_text',
                    'label' => 'Main Title Text',
                    'name' => 'main_title_text',
                    'type' => 'color_picker',
                    'default_value' => '#535353',
                ),
                array(
                    'key' => 'field_head_links',
                    'label' => 'Head Links',
                    'name' => 'head_links',
                    'type' => 'color_picker',
                    'default_value' => '#535353',
                ),
                array(
                    'key' => 'field_nav_text',
                    'label' => 'Nav Text',
                    'name' => 'nav_text',
                    'type' => 'color_picker',
                    'default_value' => '#ffffff',
                ),
                array(
                    'key' => 'field_title_text',
                    'label' => 'Title Text',
                    'name' => 'title_text',
                    'type' => 'color_picker',
                    'default_value' => '#ffffff',
                ),
                array(
                    'key' => 'field_main_text',
                    'label' => 'Main Text',
                    'name' => 'main_text',
                    'type' => 'color_picker',
                    'default_value' => '#666666',
                ),
                array(
                    'key' => 'field_link_text',
                    'label' => 'Link Text',
                    'name' => 'link_text',
                    'type' => 'color_picker',
                    'default_value' => '#000000',
                ),
                array(
                    'key' => 'field_footer_text',
                    'label' => 'Footer Text',
                    'name' => 'footer_text',
                    'type' => 'color_picker',
                    'default_value' => '#ffffff',
                ),
            ),
            'location' => array(
                array(
                    array(
                        'param' => 'page',
                        'operator' => '==',
                        'value' => '397',
                    ),
                ),
                array(
                    array(
                        'param' => 'page_parent',
                        'operator' => '==',
                        'value' => '397',
                    ),
                ),
            ),
            'menu_order' => 0,
            'position' => 'acf_after_title',
            'style' => 'default',
            'label_placement' => 'top',
            'instruction_placement' => 'label',
            'hide_on_screen' => array(
                0 => 'permalink',
                1 => 'the_content',
                2 => 'excerpt',
                3 => 'custom_fields',
                4 => 'discussion',
                5 => 'comments',
                6 => 'revisions',
                7 => 'slug',
                8 => 'author',
                9 => 'format',
                10 => 'featured_image',
                11 => 'categories',
                12 => 'tags',
                13 => 'send-trackbacks',
            ),
            'active' => true,
            'description' => '',
            'show_in_rest' => false,
        ) );
    endif;


    // Define the fields


}

// Run the function
add_action( 'acf/init', 'create_acf_fields_for_newsletter' );