<?php

/**
 * Plugin Name: FreshenUp
 * Plugin URI: https://freshenup.io/
 * Description: Programmatic updates for your old WP posts
 * Version: 1.5.3
 * Author: Julian Yanover
 * Author URI: https://wpsummarize.com/
 * Text Domain: freshenup
 * License: GPLv2
 * Released under the GNU General Public License (GPL)
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */
/**
 * freshenup main plugin file.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Define constants for easy access to the plugin's version and path.
 */
define( 'FRESHENUP_VERSION', '1.5.3' );
define( 'FRESHENUP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'FRESHENUP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
require_once plugin_dir_path( __FILE__ ) . '/vendor/autoload.php';
add_filter( 'cron_schedules', 'freshenup_add_every_minute_schedule' );
function freshenup_add_every_minute_schedule(  $schedules  ) {
    $schedules['freshenup_every_minute'] = array(
        'interval' => 60,
        'display'  => esc_html__( 'Every Minute', 'freshenup' ),
    );
    return $schedules;
}

/**
 * Include Freemius SDK.
 */
if ( !function_exists( 'freshenup_fs' ) ) {
    // Create a helper function for easy SDK access.
    function freshenup_fs() {
        global $freshenup_fs;
        if ( !isset( $freshenup_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/wordpress-sdk/start.php';
            $freshenup_fs = fs_dynamic_init( array(
                'id'               => '16819',
                'slug'             => 'freshenup',
                'premium_slug'     => 'freshenup-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_f0bb67dd731b7157bda801085aad8',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'trial'            => array(
                    'days'               => 14,
                    'is_require_payment' => true,
                ),
                'menu'             => array(
                    'slug'    => 'freshenup',
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
        }
        return $freshenup_fs;
    }

    // Init Freemius.
    freshenup_fs();
    // Signal that SDK was initiated.
    do_action( 'freshenup_fs_loaded' );
}
/**
 * Include the main plugin functionalities.
 */
require_once FRESHENUP_PLUGIN_DIR . 'admin/admin_menu.php';
require_once FRESHENUP_PLUGIN_DIR . 'admin/related_links.php';
require_once FRESHENUP_PLUGIN_DIR . 'admin/settings.php';
require_once FRESHENUP_PLUGIN_DIR . 'admin/apikey_functions.php';
require_once FRESHENUP_PLUGIN_DIR . 'includes/api_handler.php';
register_activation_hook( __FILE__, 'freshenup_activate' );
function freshenup_activate() {
    // Get current options from the database
    $openai_api_key = get_option( 'freshenup_openai_api_key' );
    // If options do not exist, set them to the defaults
    if ( $openai_api_key === false ) {
        update_option( 'freshenup_openai_api_key', '' );
    }
    $schedules = wp_get_schedules();
    if ( isset( $schedules['freshenup_every_minute'] ) ) {
        if ( !wp_next_scheduled( 'action_scheduler_run_queue' ) ) {
            wp_schedule_event( time(), 'freshenup_every_minute', 'action_scheduler_run_queue' );
        }
    }
}

if ( !class_exists( 'ActionScheduler' ) ) {
    // Include Action Scheduler
    require_once dirname( __FILE__ ) . '/vendor/woocommerce/action-scheduler/action-scheduler.php';
}
function freshenup_enqueue_admin_styles(  $hook  ) {
    wp_enqueue_style(
        'freshenup_admin_css',
        plugin_dir_url( __FILE__ ) . 'admin/css/admin-style.css',
        array(),
        FRESHENUP_VERSION,
        'all'
    );
}

add_action( 'admin_enqueue_scripts', 'freshenup_enqueue_admin_styles' );
function freshenup_enqueue_prompt_scripts(  $hook  ) {
    // Only load on your plugin's admin page
    //echo $hook;
    if ( $hook != 'freshenup_page_freshenup_prompt' && $hook != 'freshenup-1_page_freshenup_prompt' && strpos( $hook, 'page_freshenup_prompt' ) === false ) {
        return;
    }
    wp_enqueue_style(
        'freshenup-prompt-style',
        plugin_dir_url( __FILE__ ) . 'admin/css/freshenup_prompts.css',
        array(),
        FRESHENUP_VERSION,
        'all'
    );
    $prompt_script_file = 'admin/js/freshenup_prompt.js';
    wp_enqueue_script(
        'freshenup-prompt-script',
        plugin_dir_url( __FILE__ ) . $prompt_script_file,
        array('jquery'),
        FRESHENUP_VERSION,
        true
    );
    // Base localized data (shared by both free and pro)
    $localize_data = array(
        'postTypes'                 => array_map( function ( $post_type, $name ) {
            return array(
                'name' => $name,
                'slug' => $post_type,
            );
        }, array_keys( get_post_types( array(
            'public' => true,
        ), 'names' ) ), array_values( get_post_types( array(
            'public' => true,
        ), 'names' ) ) ),
        'categories'                => array_map( function ( $cat ) {
            return array(
                'id'    => $cat->term_id,
                'name'  => $cat->name,
                'count' => $cat->count,
            );
        }, get_categories( array(
            'hide_empty' => false,
        ) ) ),
        'tags'                      => array_map( function ( $tag ) {
            return array(
                'id'    => $tag->term_id,
                'name'  => $tag->name,
                'count' => $tag->count,
            );
        }, get_tags( array(
            'hide_empty' => false,
        ) ) ),
        'authors'                   => array_map( 
            function ( $user ) {
                // Get count of published posts for this user
                $count = count_user_posts( $user->ID );
                return array(
                    'id'    => $user->ID,
                    'name'  => $user->display_name,
                    'count' => $count,
                );
            },
            // Get users who can publish posts and have published at least one post
            get_users( array(
                'capability'          => array('publish_posts'),
                'has_published_posts' => true,
                'orderby'             => 'post_count',
                'order'               => 'DESC',
            ) )
         ),
        'ajaxurl'                   => admin_url( 'admin-ajax.php' ),
        'removeButtonText'          => esc_html__( 'Delete', 'freshenup' ),
        'everydayText'              => esc_html__( 'every day', 'freshenup' ),
        'mondayText'                => esc_html__( 'Monday', 'freshenup' ),
        'tuesdayText'               => esc_html__( 'Tuesday', 'freshenup' ),
        'wednesdayText'             => esc_html__( 'Wednesday', 'freshenup' ),
        'thursdayText'              => esc_html__( 'Thursday', 'freshenup' ),
        'fridayText'                => esc_html__( 'Friday', 'freshenup' ),
        'saturdayText'              => esc_html__( 'Saturday', 'freshenup' ),
        'sundayText'                => esc_html__( 'Sunday', 'freshenup' ),
        'allposttypesText'          => esc_html__( 'all post types', 'freshenup' ),
        'allText'                   => esc_html__( 'all', 'freshenup' ),
        'allcategoriesText'         => esc_html__( 'all categories', 'freshenup' ),
        'alltagsText'               => esc_html__( 'all tags', 'freshenup' ),
        'nocategoriesText'          => esc_html__( 'no categories', 'freshenup' ),
        'notagsText'                => esc_html__( 'no tags', 'freshenup' ),
        'theseposttypesText'        => esc_html__( 'these post types', 'freshenup' ),
        'posttypesText'             => esc_html__( 'post types', 'freshenup' ),
        'thesecategoriesText'       => esc_html__( 'these categories', 'freshenup' ),
        'thesetagsText'             => esc_html__( 'these tags', 'freshenup' ),
        'tagsText'                  => esc_html__( 'tags', 'freshenup' ),
        'allauthorsText'            => esc_html__( 'all authors', 'freshenup' ),
        'includefromallauthorsText' => esc_html__( 'posts from all authors', 'freshenup' ),
        'theseauthorsText'          => esc_html__( 'posts from these authors', 'freshenup' ),
        'promptidentifierText'      => esc_html__( 'Prompt identifier', 'freshenup' ),
        'UpdateText'                => esc_html__( 'Update', 'freshenup' ),
        'postsText'                 => esc_html__( 'posts', 'freshenup' ),
        'postSingularText'          => esc_html__( 'post', 'freshenup' ),
        'onText'                    => esc_html__( 'on', 'freshenup' ),
        'addingaroundText'          => esc_html__( 'adding around', 'freshenup' ),
        'ofnewcontentText'          => esc_html__( 'of new content', 'freshenup' ),
        'includeText'               => esc_html__( 'Include', 'freshenup' ),
        'youcanupdateText'          => esc_html__( 'Update posts from', 'freshenup' ),
        'excludeText'               => esc_html__( 'Exclude', 'freshenup' ),
        'onlyDatebased'             => esc_html__( 'Only update posts if they meet these date-based conditions', 'freshenup' ),
        'minDaysText'               => esc_html__( 'Not updated (in general) in the last', 'freshenup' ),
        'minDaysFreshenUpText'      => esc_html__( 'Not sent to FreshenUp in the last', 'freshenup' ),
        'minDaysPublishedText'      => esc_html__( 'Published at least', 'freshenup' ),
        'daysAgoText'               => esc_html__( 'days ago', 'freshenup' ),
        'oldminDaysText'            => esc_html__( 'Only update posts that haven\'t been updated for at least', 'freshenup' ),
        'daysText'                  => esc_html__( 'days', 'freshenup' ),
        'canUpdateText'             => esc_html__( 'can', 'freshenup' ),
        'cannotUpdateText'          => esc_html__( 'cannot', 'freshenup' ),
        'multipleUpdatesText'       => esc_html__( 'Posts', 'freshenup' ),
        'beUpdatedText'             => esc_html__( 'be updated more than once with FreshenUp', 'freshenup' ),
        'ModerateText'              => esc_html__( 'Moderate', 'freshenup' ),
        'donotModerateText'         => esc_html__( 'Do not moderate', 'freshenup' ),
        'modbeforeliveText'         => esc_html__( 'before making the updates live for users', 'freshenup' ),
    );
    wp_localize_script( 'freshenup-prompt-script', 'freshenupData', $localize_data );
}

add_action( 'admin_enqueue_scripts', 'freshenup_enqueue_prompt_scripts' );
function freshenup_initialize_action_scheduler() {
    if ( class_exists( 'ActionScheduler_Versions' ) ) {
        ActionScheduler_Versions::initialize_latest_version();
    }
}

add_action( 'plugins_loaded', 'freshenup_initialize_action_scheduler' );
register_deactivation_hook( __FILE__, 'freshenup_plugin_deactivation' );
function freshenup_plugin_deactivation() {
    // Clear all scheduled actions for our plugin
    as_unschedule_all_actions( 'freshenup_generate_content_hook', array(), 'freshenup_scheduler' );
    as_unschedule_all_actions( 'freshenup_schedule_prompt_generation_hook', null, 'freshenup_scheduler' );
}

register_deactivation_hook( __FILE__, 'freshenup_reset_notices' );