<?php

// Start Codestar Page Options
// Control core classes for avoid errors
if ( class_exists( 'CSF' ) ) {
    add_action(
        'csf_fscymu_options_save_after',
        'fscymu_clear_cache_on_save',
        10,
        2
    );
    //add_filter( 'csf_fscymu_options_save', 'fscymu_clear_cache_on_save', 10, 2 );
    // Set a unique slug-like ID
    $prefix = 'fscymu_options';
    // Create options
    CSF::createOptions( $prefix, array(
        'framework_title' => esc_html__( 'Dynamic Copyright Year', 'dynamic-copyright-year' ) . ' <small>' . esc_html__( 'by 5 Star Plugins', 'dynamic-copyright-year' ) . '</small>',
        'framework_class' => '',
        'show_bar_menu'   => false,
        'show_search'     => false,
        'menu_capability' => 'manage_options',
        'menu_position'   => null,
        'menu_type'       => 'menu',
        'menu_parent'     => '',
        'menu_title'      => esc_html__( 'Dynamic Copyright', 'dynamic-copyright-year' ),
        'menu_slug'       => 'fscymu-options',
        'menu_icon'       => 'dashicons-chart-pie',
        'footer_text'     => esc_html__( 'Clear page cache and refresh browser after saving.', 'dynamic-copyright-year' ),
        'footer_after'    => include plugin_dir_path( __FILE__ ) . 'plugin_footer.php',
        'footer_credit'   => 'Copyright ' . date( "Y" ) . ' 5 Star Plugins - Dynamic Copyright Year v' . $fscymu_version,
    ) );
    // Create a top-tab
    CSF::createSection( $prefix, array(
        'id'    => 'primary_tab',
        'title' => esc_html__( 'Options', 'dynamic-copyright-year' ),
    ) );
    // Create a sub-tab
    CSF::createSection( $prefix, array(
        'parent' => 'primary_tab',
        'title'  => esc_html__( 'Dynamic Year', 'dynamic-copyright-year' ),
        'fields' => array(
            // Heading
            array(
                'type'    => 'heading',
                'content' => esc_html__( 'Standard Settings', 'dynamic-copyright-year' ),
            ),
            // Enabled On/Off Activation
            array(
                'id'      => 'fscymu-enabled-1',
                'type'    => 'switcher',
                'title'   => esc_html__( 'Update My Footer Year', 'dynamic-copyright-year' ),
                'label'   => esc_html__( 'Activate automatic updating?', 'dynamic-copyright-year' ),
                'help'    => esc_html__( 'Turn on to activate automatic year updates and show/hide policy links.', 'dynamic-copyright-year' ),
                'default' => false,
            ),
            // Before You Get Started Accordian
            array(
                'id'         => 'opt-accordion-1',
                'type'       => 'accordion',
                'title'      => esc_html__( 'Before you get started', 'dynamic-copyright-year' ),
                'accordions' => array(array(
                    'title'  => esc_html__( 'Is copyright year currently in your footer?', 'dynamic-copyright-year' ),
                    'icon'   => 'fa fa-plus',
                    'fields' => array(
                        // Content Field
                        array(
                            'type'    => 'content',
                            'content' => '<b>' . esc_html__( 'No', 'dynamic-copyright-year' ) . '</b><br>
				' . esc_html__( 'Add © year text to your footer. Works with a footer socket or footer widget.', 'dynamic-copyright-year' ) . '
				<br><br>
				<b>' . esc_html__( 'Yes', 'dynamic-copyright-year' ) . '</b><br>
				' . esc_html__( 'All you need to do is activate automatic updating.', 'dynamic-copyright-year' ) . '<br>
				<br>
				' . esc_html__( 'This plugin detects a copyright year in the footer to automatically update it to the current year based on a visitor\'s location.', 'dynamic-copyright-year' ) . '<br>
				' . esc_html__( 'For example:', 'dynamic-copyright-year' ) . '<br>
				© YYYY<br>
				' . esc_html__( 'Copyright', 'dynamic-copyright-year' ) . ' YYYY<br>
				© ' . esc_html__( 'Copyright', 'dynamic-copyright-year' ) . ' YYYY<br>
				© YYYY - YYYY',
                        ),
                    ),
                ), array(
                    'title'  => esc_html__( 'Testing', 'dynamic-copyright-year' ),
                    'icon'   => 'fa fa-plus',
                    'fields' => array(
                        // Content Field
                        array(
                            'type'    => 'content',
                            'content' => esc_html__( 'Test if updates are working by entering a past year in your footer, then activate automatic updates and visit the site to view the footer. Refresh your browser to view changes. It does not work on years prior to 2000.', 'dynamic-copyright-year' ),
                        ),
                    ),
                ), array(
                    'title'  => esc_html__( 'Troubleshooting', 'dynamic-copyright-year' ),
                    'icon'   => 'fa fa-plus',
                    'fields' => array(
                        // Content Field
                        array(
                            'type'    => 'content',
                            'content' => esc_html__( 'This plugin does not work with this format:', 'dynamic-copyright-year' ) . '<br>
						© ' . esc_html__( 'Company', 'dynamic-copyright-year' ) . ' YYYY<br>
						' . esc_html__( 'Copyright', 'dynamic-copyright-year' ) . ' ' . esc_html__( 'Company', 'dynamic-copyright-year' ) . ' YYYY<br>
						© YYYY ' . esc_html__( 'to', 'dynamic-copyright-year' ) . ' YYYY<br>
						<br>
						' . esc_html__( 'Rearrange text to:', 'dynamic-copyright-year' ) . '<br>
						© YYYY ' . esc_html__( 'Company', 'dynamic-copyright-year' ) . '<br>
						' . esc_html__( 'Copyright', 'dynamic-copyright-year' ) . ' YYYY ' . esc_html__( 'Company', 'dynamic-copyright-year' ) . '<br>
						© YYYY - YYYY<br>
						<br>
						' . esc_html__( 'Page cache is automatically cleared sitewide each time settings are saved. If you do not see changes take effect, refresh your browser or clear page cache manually.', 'dynamic-copyright-year' ) . '<br>
						<br>
						<a href="https://support.5starplugins.com/category/244-troubleshooting?utm_source=dynamic-copyright&utm_medium=plugin&utm_campaign=dcy-dashboard&utm_source_platform=wordpress" target="_blank">' . esc_html__( 'More Tips', 'dynamic-copyright-year' ) . '</a>',
                        ),
                    ),
                )),
            ),
        ),
    ) );
    // Create a Sub-tab
    CSF::createSection( $prefix, array(
        'parent' => 'primary_tab',
        'title'  => esc_html__( 'Policy Links', 'dynamic-copyright-year' ),
        'icon'   => 'fa fa-star',
        'fields' => array(
            // Tab Heading
            array(
                'type'    => 'heading',
                'content' => esc_html__( 'Upgrade For Premium Settings', 'dynamic-copyright-year' ),
            ),
            array(
                'type'    => 'notice',
                'style'   => 'success',
                'content' => esc_html__( 'Upgrade to Premium for added features including:', 'dynamic-copyright-year' ) . '<br><br>
			- ' . esc_html__( 'Add up to 3 additional links after the copyright content', 'dynamic-copyright-year' ) . '<br>
			- ' . esc_html__( 'Position those links next to or below the copyright line', 'dynamic-copyright-year' ) . '<br><br>
			<button class="button button-primary" onclick="window.location.href=\'/wp-admin/admin.php?page=fscymu-options-pricing\'" type="button">' . esc_html__( 'View Upgrade Details', 'dynamic-copyright-year' ) . '</button>
		',
            ),
            // Select Dropdown
            array(
                'id'          => 'fscymu-prem-position-1',
                'type'        => 'select',
                'title'       => esc_html__( 'Position', 'dynamic-copyright-year' ),
                'placeholder' => esc_html__( 'Upgrade to choose positioning of links', 'dynamic-copyright-year' ),
                'options'     => array(
                    'sameline' => esc_html__( 'Same line with copyright', 'dynamic-copyright-year' ),
                    'below'    => esc_html__( 'Below copyright', 'dynamic-copyright-year' ),
                ),
                'attributes'  => array(
                    'disabled' => 'true',
                ),
                'default'     => 'sameline',
            ),
            // Fieldset
            array(
                'id'       => 'fscymu-policylinks-1',
                'type'     => 'fieldset',
                'title'    => esc_html__( 'Policy Links', 'dynamic-copyright-year' ),
                'subtitle' => esc_html__( 'Add footer links to a policy or disclosure. Leave blank to not show.', 'dynamic-copyright-year' ),
                'fields'   => array(array(
                    'id'      => 'fscymu-upgrade-link1',
                    'type'    => 'content',
                    'content' => '<div style="padding: 8px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px;"><strong>' . esc_html__( 'Link 1', 'dynamic-copyright-year' ) . ':</strong> ' . __( 'Upgrade to add links.', 'dynamic-copyright-year' ) . '</div>',
                ), array(
                    'id'      => 'fscymu-upgrade-link2',
                    'type'    => 'content',
                    'content' => '<div style="padding: 8px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px;"><strong>' . esc_html__( 'Link 2', 'dynamic-copyright-year' ) . ':</strong> ' . __( 'Upgrade to add links.', 'dynamic-copyright-year' ) . '</div>',
                ), array(
                    'id'      => 'fscymu-upgrade-link3',
                    'type'    => 'content',
                    'content' => '<div style="padding: 8px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px;"><strong>' . esc_html__( 'Link 3', 'dynamic-copyright-year' ) . ':</strong> ' . __( 'Upgrade to add links.', 'dynamic-copyright-year' ) . '</div>',
                )),
            ),
            // Enabled On/Off Activation
            array(
                'id'      => 'fscymu-policylink-ptag-upgrade',
                'type'    => 'content',
                'content' => '<div style="padding: 10px; background: #f5f5f5; border: 1px solid #ddd; border-radius: 3px;"><strong>' . esc_html__( 'Format Quick Fix', 'dynamic-copyright-year' ) . ':</strong> ' . esc_html__( 'Adding HTML &lt;p&gt; tags may help the policy links look the same as the copyright text.', 'dynamic-copyright-year' ) . ' ' . esc_html__( 'If your theme uses &lt;p&gt; tags around footer text, enable this so the style matches.', 'dynamic-copyright-year' ) . ' ' . __( 'Upgrade to enable adding HTML &lt;p&gt; tags around links', 'dynamic-copyright-year' ) . '</div>',
            ),
        ),
    ) );
    $whyUpgrade = (include plugin_dir_path( __FILE__ ) . 'whyupgrade.php');
    // fsdcy_fs()->get_trial_url()
    // Create a top-tab
    CSF::createSection( $prefix, array(
        'id'     => 'secondry_tab',
        'title'  => esc_html__( 'Why Upgrade?', 'dynamic-copyright-year' ),
        'fields' => array(
            // Heading
            array(
                'type'    => 'heading',
                'content' => esc_html__( 'Get More in the Premium Plugin', 'dynamic-copyright-year' ),
            ),
            // A Content Field Example
            array(
                'type'    => 'content',
                'content' => $whyUpgrade,
            ),
        ),
    ) );
}
// Add cache clearing
if ( !function_exists( 'fscymu_clear_cache_on_save' ) ) {
    function fscymu_clear_cache_on_save() {
        wp_cache_flush();
        $psp_cleared = FALSE;
        // WP Cloudlfare Super Page Cache purge CF cache
        if ( has_action( 'swcfpc_purge_cache' ) ) {
            do_action( "swcfpc_purge_cache" );
            //echo "<div class='notice notice-success is-dismissible'><p>WP Cloudflare Super Page cache cleared (may take 30+ seconds).</p></div>";
            $psp_cleared = TRUE;
        }
        // WP Rocket
        if ( function_exists( 'rocket_clean_domain' ) ) {
            rocket_clean_domain();
            //echo "<div class='notice notice-success is-dismissible'><p>WP Rocket cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // W3 Total Cache : w3tc
        if ( function_exists( 'w3tc_pgcache_flush' ) ) {
            w3tc_pgcache_flush();
            //echo "<div class='notice notice-success is-dismissible'><p>W3TC cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // WP Super Cache : wp-super-cache
        if ( function_exists( 'wp_cache_clear_cache' ) ) {
            wp_cache_clear_cache();
            //echo "<div class='notice notice-success is-dismissible'><p>WP Super Cache cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // WP Fastest Cache
        if ( function_exists( 'wpfc_clear_all_cache' ) ) {
            wpfc_clear_all_cache( true );
            //echo "<div class='notice notice-success is-dismissible'><p>WP Fastest Cache cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // WPEngine
        if ( class_exists( 'WpeCommon' ) && method_exists( 'WpeCommon', 'purge_memcached' ) ) {
            WpeCommon::purge_memcached();
            WpeCommon::purge_varnish_cache();
            //echo "<div class='notice notice-success is-dismissible'><p>WPEngine cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // SG Optimizer by Siteground
        if ( function_exists( 'sg_cachepress_purge_cache' ) ) {
            sg_cachepress_purge_cache();
            //echo "<div class='notice notice-success is-dismissible'><p>SiteGround Optimizer cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // LiteSpeed
        if ( class_exists( 'LiteSpeed_Cache_API' ) && method_exists( 'LiteSpeed_Cache_API', 'purge_all' ) ) {
            LiteSpeed_Cache_API::purge_all();
            //echo "<div class='notice notice-success is-dismissible'><p>LiteSpeed cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // Cache Enabler
        if ( class_exists( 'Cache_Enabler' ) && method_exists( 'Cache_Enabler', 'clear_total_cache' ) ) {
            Cache_Enabler::clear_total_cache();
            //echo "<div class='notice notice-success is-dismissible'><p>Cache Enabler cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // Pagely
        if ( class_exists( 'PagelyCachePurge' ) && method_exists( 'PagelyCachePurge', 'purgeAll' ) ) {
            PagelyCachePurge::purgeAll();
            //echo "<div class='notice notice-success is-dismissible'><p>Pagely cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // Autoptimize
        if ( class_exists( 'autoptimizeCache' ) && method_exists( 'autoptimizeCache', 'clearall' ) ) {
            autoptimizeCache::clearall();
            //echo "<div class='notice notice-success is-dismissible'><p>Autoptimize cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // Comet cache
        if ( class_exists( 'comet_cache' ) && method_exists( 'comet_cache', 'clear' ) ) {
            comet_cache::clear();
            //echo "<div class='notice notice-success is-dismissible'><p>Comet Cache cache cleared.</p></div>";
            $psp_cleared = TRUE;
        }
        // Hummingbird Cache
        if ( class_exists( '\\Hummingbird\\WP_Hummingbird' ) && method_exists( '\\Hummingbird\\WP_Hummingbird', 'flush_cache' ) ) {
            \Hummingbird\WP_Hummingbird::flush_cache();
            //echo "<div class='notice notice-success is-dismissible'><p>Hummingbird cache cleared.</p></div>";
            $dcy_cleared = TRUE;
        }
        if ( !$dcy_cleared ) {
            //echo "<div class='notice notice-success is-dismissible'><p>" . esc_html__('NOTE: Please be sure to clear any page caches for new settings to display.', 'dynamic-copyright-year') . "</p></div>";
        }
    }

}
// Free Beacon
if ( fsdcy_fs()->is_not_paying() ) {
    add_action( 'admin_enqueue_scripts', 'fsdcy_beacon_header_free' );
    // Add Freescout Free Beacon code
    if ( !function_exists( 'fsdcy_beacon_header_free' ) ) {
        function fsdcy_beacon_header_free(  $page  ) {
            if ( 'toplevel_page_fscymu-options' != $page ) {
                return;
            }
            $beacon_html = '<script>var FreeScoutW={s:{"color":"#295a85","position":"br","locale":"en","show_categories":"1","id":3539379351,"category_id":10,"fstr_free":"1"}};(function(d,e,s){if(d.getElementById("freescout-w"))return;a=d.createElement(e);m=d.getElementsByTagName(e)[0];a.async=1;a.id="freescout-w";a.src=s;m.parentNode.insertBefore(a, m)})(document,"script","https://support.presswizards.com/modules/knowledgebase/js/widget.js?v=3666");</script>';
            echo $beacon_html;
        }

    }
}