<?php
/*
Plugin Name: Smart Masjid Prayer Timings
Description: Display daily prayer timings for your mosque using the Smart Masjid platform.
Version: 1.1.0
Author: Smart Masjid
Author URI: https://smartmasjid.app/
Text Domain: smart-masjid-prayer-timings
Domain Path: /languages/
Requires at least: 5.6
Tested up to: 7.0
Requires PHP: 7.4
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/

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

// Define API base URL (override in wp-config.php if needed)
if ( ! defined( 'SMPT_API_BASE_URL' ) ) {
    define( 'SMPT_API_BASE_URL', 'https://api.smartmasjid.app' );
}

// Include necessary files
include_once dirname( __FILE__ ) . '/admin/settings.php';
include_once dirname( __FILE__ ) . '/functions.php';
include_once dirname( __FILE__ ) . '/shortcode.php';

/**
 * Supply plugin info for the "View details" modal on the Plugins screen.
 */
function smpt_plugin_info( $result, $action, $args ) {
    if ( $action !== 'plugin_information' ) {
        return $result;
    }

    // Match against this plugin's slug.
    if ( ! isset( $args->slug ) || $args->slug !== 'smart-masjid-prayer-timings' ) {
        return $result;
    }

    $plugin_data = get_plugin_data( __FILE__ );

    $info              = new stdClass();
    $info->name        = $plugin_data['Name'];
    $info->slug        = 'smart-masjid-prayer-timings';
    $info->version     = $plugin_data['Version'];
    $info->author      = $plugin_data['Author'];
    $info->homepage    = $plugin_data['PluginURI'];
    $info->requires    = $plugin_data['RequiresWP'];
    $info->tested      = $plugin_data['TestedUpTo'];
    $info->requires_php = $plugin_data['RequiresPHP'];

    // Pull sections from readme.txt if available.
    $readme_file = dirname( __FILE__ ) . '/readme.txt';
    if ( file_exists( $readme_file ) ) {
        $readme = file_get_contents( $readme_file );

        // Convert readme.txt markup to HTML.
        $to_html = function ( $text ) {
            $text = trim( $text );
            // Bold: **text** → <strong>text</strong>
            $text = preg_replace( '/\*\*(.+?)\*\*/', '<strong>$1</strong>', $text );
            // Italic: *text* → <em>text</em>
            $text = preg_replace( '/\*(.+?)\*/', '<em>$1</em>', $text );
            // Links: [text](url) → <a href="url">text</a>
            $text = preg_replace( '/\[([^\]]+)\]\(([^)]+)\)/', '<a href="$2">$1</a>', $text );
            // Sub-headings: = Heading = → <h4>Heading</h4>
            $text = preg_replace( '/^= (.+?) =$/m', '<h4>$1</h4>', $text );
            // Unordered list items: * item → <li>item</li>
            $text = preg_replace( '/^\* (.+)$/m', '<li>$1</li>', $text );
            $text = preg_replace( '/(<li>.*<\/li>\s*)+/s', '<ul>$0</ul>', $text );
            // Ordered list items: 1. item → <li>item</li>
            $text = preg_replace( '/^\d+\.\s+(.+)$/m', '<li>$1</li>', $text );
            $text = preg_replace( '/(<li>.*<\/li>\s*)+/s', '<ol>$0</ol>', $text );
            return wpautop( $text );
        };

        // Description
        if ( preg_match( '/== Description ==\s*(.+?)(?===\s)/s', $readme, $m ) ) {
            $info->sections['description'] = $to_html( $m[1] );
        }
        // Installation
        if ( preg_match( '/== Installation ==\s*(.+?)(?===\s)/s', $readme, $m ) ) {
            $info->sections['installation'] = $to_html( $m[1] );
        }
        // FAQ
        if ( preg_match( '/== Frequently Asked Questions ==\s*(.+?)(?===\s)/s', $readme, $m ) ) {
            $info->sections['faq'] = $to_html( $m[1] );
        }
        // Changelog
        if ( preg_match( '/== Changelog ==\s*(.+?)$/s', $readme, $m ) ) {
            $info->sections['changelog'] = $to_html( $m[1] );
        }
    } else {
        $info->sections['description'] = $plugin_data['Description'];
    }

    return $info;
}
add_filter( 'plugins_api', 'smpt_plugin_info', 20, 3 );

/**
 * Add a "View details" link to the plugin row on the Plugins screen.
 */
function smpt_plugin_row_meta( $plugin_meta, $plugin_file ) {
    if ( plugin_basename( __FILE__ ) !== $plugin_file ) {
        return $plugin_meta;
    }

    $plugin_meta[] = sprintf(
        '<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s">%s</a>',
        esc_url( self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=smart-masjid-prayer-timings&TB_iframe=true&width=600&height=550' ) ),
        esc_attr__( 'More information about Smart Masjid Prayer Timings', 'smart-masjid-prayer-timings' ),
        esc_html__( 'View details', 'smart-masjid-prayer-timings' )
    );

    return $plugin_meta;
}
add_filter( 'plugin_row_meta', 'smpt_plugin_row_meta', 10, 2 );

function smpt_load_textdomain() {
    $locale = get_locale();
    $path = dirname( __FILE__ ) . '/languages/';
    $file = $path . 'smart-masjid-prayer-timings-' . $locale . '.mo';
    load_textdomain( 'smart-masjid-prayer-timings', $file );
}
add_action( 'plugins_loaded', 'smpt_load_textdomain' );
