<?php
/**
 * Plugin Name: MKM Digital Social Publisher Bridge to Buffer
 * Plugin URI: https://www.mkm-digital.com/social-publisher-bridge-2-buffer/
 * Description: Directly connect WordPress to Buffer to automatically publish and schedule posts, events, and products across your 3 free Buffer.com social channels with custom templates and live previews.
 * Version: 2.0.2
 * Author: MKM DIGITAL
 * Author URI: https://mkm-digital.com
 * Support: supporto@mkm-digital.com
 * Text Domain: mkmdigital-social-publisher-bridge
 * Domain Path: /languages
 * License: GPL2
 *
 * @package MKM_DSPB_Bridge
 * @version 2.0.2
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( ! function_exists( 'mkm_dspb_fs' ) ) {
    // Create a helper function for easy SDK access.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
    function mkm_dspb_fs() {
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
        global $mkm_dspb_fs;

        if ( ! isset( $mkm_dspb_fs ) ) {
            // Include Freemius SDK.
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

            // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
            $mkm_dspb_fs = fs_dynamic_init( array(
                'id'                  => '32870',
                'slug'                => 'mkmdigital-social-publisher-bridge',
                'type'                => 'plugin',
                'public_key'          => 'pk_4ced86d18e946eefb3642f799e8c4',
                'is_premium'          => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'menu'                => array(
                    'slug'           => 'mkmdigital-social-publisher-bridge',
                    'account'        => true,
                    'support'        => false,
                ),
            ) );
        }

        return $mkm_dspb_fs;
    }

    // Init Freemius.
    mkm_dspb_fs();
    // Signal that SDK was initiated.
    // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
    do_action( 'mkm_dspb_fs_loaded' );
}

// Check if Premium version is active, and step down if so
if ( ! function_exists( 'mkm_dspb_free_check_premium_active' ) ) {
    function mkm_dspb_free_check_premium_active() {
        if ( defined( 'MKM_DSPB_PREMIUM_VERSION' ) ) {
            return true;
        }
        if ( is_admin() ) {
            if ( ! function_exists( 'is_plugin_active' ) ) {
                require_once ABSPATH . 'wp-admin/includes/plugin.php';
            }
            $premium_plugin = 'mkmdigital-social-publisher-bridge-premium/mkmdigital-social-publisher-bridge.php';
            if ( is_plugin_active( $premium_plugin ) ) {
                if ( function_exists( 'deactivate_plugins' ) ) {
                    deactivate_plugins( plugin_basename( __FILE__ ) );
                }
                return true;
            }
        }
        return false;
    }
}
if ( mkm_dspb_free_check_premium_active() ) {
    return;
}

// Define Constants
if ( ! defined( 'MKM_DSPB_VERSION' ) ) {
    define( 'MKM_DSPB_VERSION', '2.0.2' );
}
if ( ! defined( 'MKM_DSPB_PATH' ) ) {
    define( 'MKM_DSPB_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'MKM_DSPB_URL' ) ) {
    define( 'MKM_DSPB_URL', plugin_dir_url( __FILE__ ) );
}

// Load classes
require_once MKM_DSPB_PATH . 'includes/class-mkm-dspb-api.php';
require_once MKM_DSPB_PATH . 'includes/class-mkm-dspb-admin.php';
require_once MKM_DSPB_PATH . 'includes/class-mkm-dspb-publisher.php';

// Initialization
add_action( 'plugins_loaded', 'mkm_dspb_init' );

if ( ! function_exists( 'mkm_dspb_init' ) ) {
    function mkm_dspb_init() {
        // Load translations
        load_plugin_textdomain( 'mkmdigital-social-publisher-bridge', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

        // Instantiate classes
        $api       = new MKM_DSPB_API();
        $admin     = new MKM_DSPB_Admin( $api );
        $publisher = new MKM_DSPB_Publisher( $api );
    }
}

// Add settings link to plugins list page
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'mkm_dspb_action_links' );

if ( ! function_exists( 'mkm_dspb_action_links' ) ) {
    function mkm_dspb_action_links( $links ) {
        $settings_link = '<a href="' . admin_url( 'admin.php?page=mkmdigital-social-publisher-bridge' ) . '">' . __( 'Settings', 'mkmdigital-social-publisher-bridge' ) . '</a>';
        array_unshift( $links, $settings_link );
        return $links;
    }
}

// Customize plugin row meta on plugins list page to open official WordPress.org details modal
add_filter( 'plugin_row_meta', 'mkm_dspb_plugin_row_meta', 10, 2 );
if ( ! function_exists( 'mkm_dspb_plugin_row_meta' ) ) {
    function mkm_dspb_plugin_row_meta( $links, $file ) {
        if ( false !== strpos( $file, 'mkmdigital-social-publisher-bridge' ) ) {
            $modal_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=mkmdigital-social-publisher-bridge&TB_iframe=true&width=772&height=600' );
            $clean_links = array();
            foreach ( $links as $link ) {
                if ( false !== strpos( $link, 'mkm-digital.com' ) || false !== strpos( $link, 'thickbox' ) || false !== strpos( $link, 'plugin-information' ) ) {
                    continue;
                }
                $clean_links[] = $link;
            }
            $clean_links[] = sprintf(
                '<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>',
                esc_url( $modal_url ),
                esc_attr( __( 'More information about MKM Digital Social Publisher Bridge to Buffer', 'mkmdigital-social-publisher-bridge' ) ),
                esc_attr( 'MKM Digital Social Publisher Bridge to Buffer' ),
                __( 'View details', 'mkmdigital-social-publisher-bridge' )
            );
            return $clean_links;
        }
        return $links;
    }
}

// Ensure custom plugin icon is displayed in WordPress plugin update list & dashboard
add_filter( 'site_transient_update_plugins', 'mkm_dspb_add_plugin_update_icon' );
if ( ! function_exists( 'mkm_dspb_add_plugin_update_icon' ) ) {
    function mkm_dspb_add_plugin_update_icon( $transient ) {
        if ( is_object( $transient ) && isset( $transient->response ) && is_array( $transient->response ) ) {
            $icon_128 = MKM_DSPB_URL . 'assets/icon-128x128.png';
            $icon_256 = MKM_DSPB_URL . 'assets/icon-256x256.png';
            foreach ( $transient->response as $plugin_file => $data ) {
                if ( false !== strpos( $plugin_file, 'mkmdigital-social-publisher-bridge' ) ) {
                    if ( is_object( $transient->response[ $plugin_file ] ) ) {
                        $transient->response[ $plugin_file ]->icons = array(
                            '1x'      => $icon_128,
                            '2x'      => $icon_256,
                            'default' => $icon_256,
                        );
                    } elseif ( is_array( $transient->response[ $plugin_file ] ) ) {
                        $transient->response[ $plugin_file ]['icons'] = array(
                            '1x'      => $icon_128,
                            '2x'      => $icon_256,
                            'default' => $icon_256,
                        );
                    }
                }
            }
        }
        return $transient;
    }
}

// Register Activation and Deactivation Hooks
register_activation_hook( __FILE__, 'mkm_dspb_activate' );
register_deactivation_hook( __FILE__, 'mkm_dspb_deactivate' );

if ( ! function_exists( 'mkm_dspb_activate' ) ) {
    function mkm_dspb_activate() {
        // Default settings
        add_option( 'mkm_dspb_token', '' );
        add_option( 'mkm_dspb_profiles', array() );
        add_option( 'mkm_dspb_default_channels', array() );
        add_option( 'mkm_dspb_post_types', array( 'post' ) );
        add_option( 'mkm_dspb_publish_mode', 'addToQueue' ); // 'addToQueue' or 'shareNow'
        add_option( 'mkm_dspb_template', "{title}\n\n{excerpt}\n\n{link}" );
    }
}

if ( ! function_exists( 'mkm_dspb_deactivate' ) ) {
    function mkm_dspb_deactivate() {
        // No intrusive data deletion on deactivation
    }
}

// Hook into Freemius uninstall
add_action( 'fs_after_uninstall_mkmdigital-social-publisher-bridge', 'mkm_dspb_uninstall' );

if ( ! function_exists( 'mkm_dspb_uninstall' ) ) {
    function mkm_dspb_uninstall() {
        // Delete options from database
        $options = array(
            'mkm_dspb_token',
            'mkm_dspb_profiles',
            'mkm_dspb_default_channels',
            'mkm_dspb_post_types',
            'mkm_dspb_publish_mode',
            'mkm_dspb_template',
        );

        foreach ( $options as $option ) {
            delete_option( $option );
        }

        // Delete post meta for all posts
        global $wpdb;
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_mkm_dspb_%'" );
    }
}
