<?php
/**
 * Pinterest Follow Us Shortcode functionality
 *
 * @link       https://pinnerpress.com
 * @since      0.1.0
 *
 * @package    PinnerPress
 * @subpackage PinnerPress/includes
 */

if (!defined('WPINC')) {
    die;
}

/**
 * Pinterest Follow Button Shortcode
 * 
 * Usage: [pinnerpress_follow url="https://www.pinterest.com/yourusername" text="Follow us on Pinterest" size="medium" color="#E60023" text_color="#ffffff" show_icon="true" align="left" new_tab="true"]
 * Legacy: [pinnerpress_follow username="yourusername" ...] (still supported)
 */
function pinnerpress_follow_shortcode($atts) {
    // Get defaults from settings
    $default_pinterest_url = get_option('pinnerpress_pinterest_url', '');
    $default_button_text = get_option('pinnerpress_follow_button_default_text', 'Follow on Pinterest');
    $default_button_color = get_option('pinnerpress_follow_button_default_color', '#E60023');
    $default_text_color = get_option('pinnerpress_follow_button_default_text_color', '#ffffff');
    
    // Set default attributes
    $attributes = shortcode_atts(array(
        'url' => '',
        'username' => '', // Legacy support
        'text' => '',
        'size' => 'medium',
        'color' => '',
        'text_color' => '',
        'show_icon' => 'true',
        'align' => 'left',
        'new_tab' => 'true'
    ), $atts, 'pinnerpress_follow');

    // Handle legacy username parameter
    $pinterest_url = '';
    if (!empty($attributes['url'])) {
        $pinterest_url = esc_url($attributes['url']);
    } elseif (!empty($attributes['username'])) {
        // Legacy support: convert username to URL
        $pinterest_url = 'https://www.pinterest.com/' . sanitize_text_field($attributes['username']) . '/';
    } else {
        // Use default from settings
        $pinterest_url = $default_pinterest_url;
    }

    // Sanitize attributes with defaults
    $button_text = !empty($attributes['text']) ? sanitize_text_field($attributes['text']) : $default_button_text;
    $button_color = !empty($attributes['color']) ? sanitize_hex_color($attributes['color']) : $default_button_color;
    $text_color = !empty($attributes['text_color']) ? sanitize_hex_color($attributes['text_color']) : $default_text_color;
    $button_size = in_array($attributes['size'], array('small', 'medium', 'large')) ? $attributes['size'] : 'medium';
    $show_icon = filter_var($attributes['show_icon'], FILTER_VALIDATE_BOOLEAN);
    $alignment = in_array($attributes['align'], array('left', 'center', 'right')) ? $attributes['align'] : 'left';
    $open_in_new_tab = filter_var($attributes['new_tab'], FILTER_VALIDATE_BOOLEAN);

    // If no Pinterest URL is provided, return empty
    if (empty($pinterest_url)) {
        return '';
    }

    // Use the same rendering function as the block
    $block_attributes = array(
        'pinterestUrl' => $pinterest_url,
        'buttonText' => $button_text,
        'buttonColor' => $button_color,
        'textColor' => $text_color,
        'buttonSize' => $button_size,
        'showIcon' => $show_icon,
        'alignment' => $alignment,
        'openInNewTab' => $open_in_new_tab
    );

    return pinnerpress_render_follow_block($block_attributes);
}

/**
 * Register the shortcode
 */
add_shortcode('pinnerpress_follow', 'pinnerpress_follow_shortcode');

/**
 * Add shortcode button to TinyMCE editor (Classic Editor)
 */
function pinnerpress_add_follow_shortcode_button() {
    // Check if user has permission to edit posts
    if (!current_user_can('edit_posts') && !current_user_can('edit_pages')) {
        return;
    }

    // Check if Rich Editing is enabled
    if (get_user_option('rich_editing') !== 'true') {
        return;
    }

    // Add TinyMCE plugin
    add_filter('mce_external_plugins', 'pinnerpress_add_follow_tinymce_plugin');
    add_filter('mce_buttons', 'pinnerpress_register_follow_button');
}

/**
 * Register TinyMCE plugin
 */
function pinnerpress_add_follow_tinymce_plugin($plugin_array) {
    $plugin_array['pinnerpress_follow'] = plugin_dir_url(dirname(__FILE__)) . 'admin/js/pinnerpress-follow-tinymce.js';
    return $plugin_array;
}

/**
 * Register TinyMCE button
 */
function pinnerpress_register_follow_button($buttons) {
    array_push($buttons, 'pinnerpress_follow');
    return $buttons;
}

// Hook for Classic Editor
add_action('admin_head', 'pinnerpress_add_follow_shortcode_button');

/**
 * Enqueue shortcode styles on frontend
 */
function pinnerpress_enqueue_follow_shortcode_styles() {
    // Only enqueue if shortcode is used on the page
    global $post;
    if (is_a($post, 'WP_Post') && has_shortcode($post->post_content, 'pinnerpress_follow')) {
        wp_enqueue_style(
            'pinnerpress-follow-block',
            plugin_dir_url(dirname(__FILE__)) . 'public/css/pinnerpress-follow-block.css',
            array(),
            PINNERPRESS_VERSION
        );
    }
}

add_action('wp_enqueue_scripts', 'pinnerpress_enqueue_follow_shortcode_styles'); 