<?php
/**
 * Plugin Name: TW Link Expiration Manager
 * Plugin URI: https://technologywisdom.com/
 * Description: Adds an "Expiration Time" for post, page, WooCommerce products and custom post types URLs.
 * Version: 1.0.2
 * Requires at least: 5.0
 * Requires PHP: 7.4
 * Author: technologywisdom
 * Author URI: https://technologywisdom.com/about/
 * License: GPL-2.0+
 * License URI: https://opensource.org/licenses/GPL-2.0
 * Text Domain: tw-link-expiration-manager
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

define( 'TW_LEM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TW_LEM_PLUGIN_PATH', TW_LEM_PLUGIN_DIR );
define( 'TW_LEM_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TW_LEM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'TW_LEM_PLUGIN_VERSION', '1.0.2' );

require_once TW_LEM_PLUGIN_DIR . 'includes/tw-lem-functions.php';
require_once TW_LEM_PLUGIN_DIR . 'includes/admin-menu.php';
require_once TW_LEM_PLUGIN_DIR . 'includes/support-page.php';
require_once TW_LEM_PLUGIN_DIR . 'includes/rating-popup.php';

register_activation_hook( __FILE__, 'tw_lem_activate' );

/**
 * Plugin activation.
 */
function tw_lem_activate() {
    tw_lem_set_install_timestamp();
}

add_action( 'admin_enqueue_scripts', 'tw_lem_enqueue_admin_assets' );

/**
 * Enqueue admin scripts and styles.
 */
function tw_lem_enqueue_admin_assets( $hook ) {
    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
    $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
    $is_plugin_page = $page && strpos( $page, 'tw-lem' ) === 0;

    if ( $hook === 'edit.php' || $is_plugin_page ) {
        wp_enqueue_style(
            'tw-lem-admin-css',
            TW_LEM_PLUGIN_URL . 'assets/css/admin.css',
            array(),
            TW_LEM_PLUGIN_VERSION,
            'all'
        );

        if ( $hook === 'edit.php' ) {
            wp_enqueue_script(
                'tw-lem-admin-js',
                TW_LEM_PLUGIN_URL . 'assets/js/admin.js',
                array( 'jquery' ),
                TW_LEM_PLUGIN_VERSION,
                true
            );
        }
    }
}

/**
 * Render the tutorial page content.
 */
function tw_lem_render_tutorial_page() {
    ?>
    <div class="wrap tw-lem-wrap">
        <h1><?php esc_html_e( 'Tutorial: TW Link Expiration Manager', 'tw-link-expiration-manager' ); ?></h1>

        <?php tw_lem_render_rate_us_banner(); ?>
        <p><?php esc_html_e( 'This tutorial explains how to use the TW Link Expiration Manager plugin:', 'tw-link-expiration-manager' ); ?></p>
        <ul style="margin-left: 20px; list-style: disc;">
            <li><?php esc_html_e( 'Set an expiration date on any post, page, woocommerce product or custom post type.', 'tw-link-expiration-manager' ); ?></li>
            <li><?php esc_html_e( 'Once the expiration time is reached, the post will automatically be set to Draft.', 'tw-link-expiration-manager' ); ?></li>
            <li><?php esc_html_e( 'You can manage expiration in the editor sidebar or via Quick Edit in the post list.', 'tw-link-expiration-manager' ); ?></li>
        </ul>

        <h2><?php esc_html_e( 'Video Tutorial', 'tw-link-expiration-manager' ); ?></h2>
        <div style="max-width: 700px;">
            <?php $video_url = TW_LEM_PLUGIN_URL . 'video-tutorial/tutorial.mp4'; ?>
            <video width="100%" height="400" controls>
                <source src="<?php echo esc_url( $video_url ); ?>" type="video/mp4">
                <?php esc_html_e( 'Your browser does not support the video tag.', 'tw-link-expiration-manager' ); ?>
            </video>
        </div>
    </div>
    <?php
}

add_filter( 'plugin_action_links_' . TW_LEM_PLUGIN_BASENAME, 'tw_lem_add_plugin_action_links' );

/**
 * Add tutorial and rate us links to plugin actions.
 */
function tw_lem_add_plugin_action_links( $links ) {
    $tutorial_url  = admin_url( 'admin.php?page=tw-lem' );
    $tutorial_link = '<a href="' . esc_url( $tutorial_url ) . '">' . esc_html__( 'Tutorial', 'tw-link-expiration-manager' ) . '</a>';
    $rate_us_link  = '<a href="' . esc_url( 'https://wordpress.org/support/plugin/tw-link-expiration-manager/reviews/' ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Rate Us', 'tw-link-expiration-manager' ) . '</a>';
    array_unshift( $links, $rate_us_link );
    array_unshift( $links, $tutorial_link );
    return $links;
}
