<?php

/**
 * Plugin Name: Easy Social Feed
 * Plugin URI:        https://wordpress.org/plugins/easy-facebook-likebox
 * Description:       Formerly "Easy Facebook Like Box and Custom Facebook Feed" plugin allows you to easily display custom facebook feed, custom Instagram photos and videos feed, page plugin (like box) on your website using either widget or shortcode to increase facbook fan page likes. You can use the shortcode generator. Additionally, it also now allows you to display the customized facebook feed on your website using the same color scheme of your website. Its completely customizable with lots of optional settings. Its also responsive facebook like box at the same time.
 * Version:           6.7.5
 * Author:            Easy Social Feed
 * Author URI:        https://easysocialfeed.com/
 * Text Domain:       easy-facebook-likebox
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Run plugin cleanup on uninstall (options, transients, upload folders).
 * Hooked to Freemius after_uninstall so uninstall feedback is sent first.
 * Skips cleanup when "Preserve settings on uninstall" is enabled in General settings.
 *
 * @since 6.8.0
 */
function efl_fs_uninstall_cleanup() {
    global $wpdb;
    $fta_settings = get_option( 'fta_settings', array() );
    if ( !empty( $fta_settings['preserve_settings_on_uninstall'] ) ) {
        return;
    }
    $options = array(
        'fta_settings',
        'fta_supported',
        'efbl_version',
        'efbl_installDate',
        'efbl_version_type',
        'efbl_account_id',
        'efbl_skin_id',
        'mif_skin_id',
        'mif_account_id'
    );
    foreach ( $options as $option ) {
        delete_option( $option );
    }
    if ( is_multisite() ) {
        delete_site_option( 'fta_supported' );
    }
    $option_table = $wpdb->options;
    $wpdb->query( $wpdb->prepare( "DELETE FROM {$option_table} WHERE option_name LIKE %s OR option_name LIKE %s", $wpdb->esc_like( 'efbl_skin_' ) . '%', $wpdb->esc_like( 'mif_skin_' ) . '%' ) );
    $like_esf = $wpdb->esc_like( '_transient_esf_' ) . '%';
    $like_esf_timeout = $wpdb->esc_like( '_transient_timeout_esf_' ) . '%';
    $like_efbl = $wpdb->esc_like( '_transient_efbl_' ) . '%';
    $like_efbl_timeout = $wpdb->esc_like( '_transient_timeout_efbl_' ) . '%';
    $wpdb->query( $wpdb->prepare(
        "DELETE FROM {$option_table} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
        $like_esf,
        $like_esf_timeout,
        $like_efbl,
        $like_efbl_timeout
    ) );
    $upload_dir = wp_upload_dir();
    if ( !empty( $upload_dir['basedir'] ) ) {
        $base = trailingslashit( $upload_dir['basedir'] );
        $dirs = array('esf-instagram', 'esf-facebook', 'esf-youtube');
        require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
        require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
        $fs = new WP_Filesystem_Direct(false);
        foreach ( $dirs as $dir ) {
            $path = $base . $dir;
            if ( $fs->exists( $path ) && $fs->is_dir( $path ) ) {
                $fs->rmdir( $path, true );
            }
        }
    }
}

if ( function_exists( 'efl_fs' ) ) {
    efl_fs()->set_basename( false, __FILE__ );
    efl_fs()->add_action( 'after_uninstall', 'efl_fs_uninstall_cleanup' );
} else {
    if ( !function_exists( 'efl_fs' ) ) {
        // Create a helper function for easy SDK access.
        function efl_fs() {
            global $efl_fs;
            if ( !isset( $efl_fs ) ) {
                // Include Freemius SDK.
                require_once __DIR__ . '/vendor/freemius/start.php';
                $efl_fs = fs_dynamic_init( array(
                    'id'               => '4142',
                    'slug'             => 'easy-facebook-likebox',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_d982f4dff842224ca5e54c84f6822',
                    'is_premium'       => false,
                    'has_addons'       => true,
                    'has_paid_plans'   => true,
                    'trial'            => array(
                        'days'               => 7,
                        'is_require_payment' => true,
                    ),
                    'has_affiliation'  => 'all',
                    'menu'             => array(
                        'support'    => false,
                        'slug'       => 'feed-them-all',
                        'first-path' => 'admin.php?page=esf_welcome',
                    ),
                    'is_live'          => true,
                    'is_org_compliant' => true,
                ) );
            }
            return $efl_fs;
        }

        // Init Freemius.
        efl_fs();
        efl_fs()->add_action( 'after_uninstall', 'efl_fs_uninstall_cleanup' );
        // Signal that SDK was initiated.
        do_action( 'efl_fs_loaded' );
    }
    //======================================================================
    // Code for the Main structure
    //======================================================================
    $options = get_option( 'fta_settings', array() );
    $fb_status = ( isset( $options['plugins']['facebook']['status'] ) ? $options['plugins']['facebook']['status'] : 'activated' );
    if ( isset( $options['plugins']['facebook'] ) ) {
        $fb = $options['plugins']['facebook'];
    } else {
        $fb = array();
    }
    if ( !array_key_exists( 'status', $fb ) ) {
        $fb_status = 'activated';
    }
    if ( $fb_status == 'activated' ) {
        require_once plugin_dir_path( __FILE__ ) . 'facebook/frontend/includes/core-functions.php';
        require_once plugin_dir_path( __FILE__ ) . 'facebook/frontend/easy-facebook-likebox.php';
        require_once plugin_dir_path( __FILE__ ) . 'facebook/includes/easy-custom-facebook-feed-widget.php';
        require_once plugin_dir_path( __FILE__ ) . 'facebook/includes/easy-facebook-page-plugin-widget.php';
        function register_fblx_widget() {
            register_widget( 'Easy_Custom_Facebook_Feed_Widget' );
            register_widget( 'Easy_Facebook_Page_Plugin_Widget' );
        }

        add_action( 'widgets_init', 'register_fblx_widget' );
    }
    $insta_status = ( isset( $options['plugins']['instagram']['status'] ) ? $options['plugins']['instagram']['status'] : 'activated' );
    if ( isset( $options['plugins']['instagram'] ) ) {
        $insta = $options['plugins']['instagram'];
    } else {
        $insta = array();
    }
    if ( !array_key_exists( 'status', $insta ) ) {
        $insta_status = 'activated';
    }
    if ( $insta_status == 'activated' ) {
        require_once plugin_dir_path( __FILE__ ) . 'instagram/includes/esf-instagram-feed-widget.php';
        function register_insta_widget() {
            register_widget( 'ESF_Instagram_Feed_Widget' );
        }

        add_action( 'widgets_init', 'register_insta_widget' );
    }
    // YouTube Module (default to activated when not explicitly set).
    $youtube_status = ( isset( $options['plugins']['youtube']['status'] ) ? $options['plugins']['youtube']['status'] : 'activated' );
    if ( isset( $options['plugins']['youtube'] ) ) {
        $youtube = $options['plugins']['youtube'];
    } else {
        $youtube = array();
    }
    // When status is missing or empty, treat YouTube as activated by default.
    if ( !array_key_exists( 'status', $youtube ) || '' === $youtube_status ) {
        $youtube_status = 'activated';
    }
    if ( 'activated' === $youtube_status ) {
        require_once plugin_dir_path( __FILE__ ) . 'youtube/autoload.php';
    }
    if ( !class_exists( 'Feed_Them_All' ) ) {
        class Feed_Them_All {
            public $version = '6.7.5';

            public $fta_slug = 'easy-facebook-likebox';

            public $plug_slug = 'easy-facebook-likebox';

            function __construct() {
                add_action( 'init', array($this, 'constants') );
                add_action( 'init', array($this, 'includes') );
                add_action( 'init', array($this, 'create_uploads_folder') );
                add_action( 'plugins_loaded', array($this, 'load_textdomain'), 1 );
                register_activation_hook( __FILE__, array($this, 'fta_activated') );
            }

            /*
             * constants defines all the plugin constants.
             * Returns nothing.
             * defined() func used to define constant.
             */
            public function constants() {
                if ( !defined( 'FTA_VERSION' ) ) {
                    define( 'FTA_VERSION', $this->version );
                }
                if ( !defined( 'FTA_PLUGIN_DIR' ) ) {
                    define( 'FTA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
                }
                if ( !defined( 'FTA_PLUGIN_URL' ) ) {
                    define( 'FTA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
                }
                if ( !defined( 'FTA_PLUGIN_FILE' ) ) {
                    define( 'FTA_PLUGIN_FILE', __FILE__ );
                }
            }

            /**
             * Load Easy Facebook likebox textdomain.
             *
             * @since 6.2.1
             *
             * @return void
             * @access public
             */
            public function load_textdomain() {
                load_plugin_textdomain( 'easy-facebook-likebox', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
            }

            /*
             * fta_activated will Add all the necessary data into the database on plugin install.
             * Returns nothing.
             */
            public function fta_activated() {
                $install_date = $this->fta_get_settings( 'installDate' );
                $fta_settings = array(
                    'version'     => $this->version,
                    'installDate' => date( 'Y-m-d h:i:s' ),
                    'plugins'     => $this::fta_plugins(),
                );
                if ( empty( $install_date ) ) {
                    update_option( 'fta_settings', $fta_settings );
                }
            }

            /*
             * includes will include all the necessary files.
             * Returns nothing.
             * include() func used to include any file.
             */
            public function includes() {
                include FTA_PLUGIN_DIR . '/includes/class-module-search.php';
                include FTA_PLUGIN_DIR . '/includes/core-functions.php';
                include FTA_PLUGIN_DIR . '/includes/class-esf-translation-strings.php';
                include FTA_PLUGIN_DIR . '/includes/class-esf-gdpr-integrations.php';
                include FTA_PLUGIN_DIR . 'admin/class-esf-admin.php';
                $fta_plugins = $this->fta_plugins();
                $instagram_status = $fta_plugins['instagram']['status'];
                if ( $instagram_status == 'activated' ) {
                    include FTA_PLUGIN_DIR . 'instagram/autoload.php';
                }
                $fb_status = $fta_plugins['facebook']['status'];
                if ( $fb_status == 'activated' ) {
                    include FTA_PLUGIN_DIR . 'facebook/autoload.php';
                }
            }

            /**
             * Create uploads folder if not exists.
             *
             * @since 6.4.4
             */
            public function create_uploads_folder() {
                $upload_dir = wp_upload_dir();
                $base = $upload_dir['basedir'];
                $folders = array('esf-instagram', 'esf-facebook', 'esf-youtube');
                foreach ( $folders as $folder ) {
                    $path = $base . '/' . $folder;
                    if ( !file_exists( $path ) ) {
                        wp_mkdir_p( $path );
                    }
                }
            }

            /*
             * fta_plugins Holds all the FTA plugins data
             */
            public function fta_plugins() {
                $feed_them_all = new Feed_Them_All();
                $settings = $feed_them_all->fta_get_settings();
                $fb_status = ( isset( $settings['plugins']['facebook']['status'] ) ? $settings['plugins']['facebook']['status'] : 'activated' );
                $insta_status = ( isset( $settings['plugins']['instagram']['status'] ) ? $settings['plugins']['instagram']['status'] : 'activated' );
                // Default YouTube to activated when no explicit status is stored.
                $youtube_status = ( isset( $settings['plugins']['youtube']['status'] ) ? $settings['plugins']['youtube']['status'] : 'activated' );
                if ( empty( $fb_status ) ) {
                    $fb_status = 'activated';
                }
                if ( empty( $insta_status ) ) {
                    $insta_status = 'activated';
                }
                if ( empty( $youtube_status ) ) {
                    $youtube_status = 'activated';
                }
                /*
                 * Making an array of all plugins
                 */
                $fta_plugins = array(
                    'facebook'  => array(
                        'name'          => __( 'Custom Facebook Feed - Page Plugin (Likebox)', 'easy-facebook-likebox' ),
                        'slug'          => 'easy-facebook-likebox',
                        'activate_slug' => 'facebook',
                        'description'   => __( 'Display customizable and mobile-friendly Facebook post, images, videos, events, albums feed and Facebook page plugin using shortcode and widget', 'easy-facebook-likebox' ),
                        'img_name'      => 'fb_cover.png',
                        'status'        => $fb_status,
                    ),
                    'instagram' => array(
                        'name'          => __( 'Custom Instagram Feed', 'easy-facebook-likebox' ),
                        'slug'          => 'mif',
                        'activate_slug' => 'instagram',
                        'description'   => __( 'Display stunning photos from the Instagram account in the feed, any Hashtag Feed and gallery of photos in the PopUp using shortcode, widget, inside popup and widget', 'easy-facebook-likebox' ),
                        'img_name'      => 'insta_cover.png',
                        'status'        => $insta_status,
                    ),
                    'youtube'   => array(
                        'name'          => __( 'YouTube Feed', 'easy-facebook-likebox' ),
                        'slug'          => 'esf-youtube',
                        'activate_slug' => 'youtube',
                        'description'   => __( 'Display YouTube videos from your channel using secure OAuth connection with customizable layouts and caching', 'easy-facebook-likebox' ),
                        'img_name'      => 'youtube_cover.png',
                        'status'        => $youtube_status,
                    ),
                );
                return $fta_plugins;
            }

            /*
             * It will get the saved settings.
             */
            public function fta_get_settings( $key = null ) {
                $fta_settings = get_option( 'fta_settings', false );
                if ( $key && isset( $fta_settings[$key] ) ) {
                    $fta_settings = $fta_settings[$key];
                }
                return $fta_settings;
            }

            /*
             * fta_settings_link Will add the My Instagram settings page link in the plugin area.
             */
            public function fta_settings_link( $links ) {
                $fta_link = array('<a href="' . admin_url( 'admin.php?page=feed-them-all' ) . '">' . __( 'Settings', 'easy-facebook-likebox' ) . '</a>');
                return array_merge( $fta_link, $links );
            }

            /**
             * Return module status
             *
             * @since 6.2.8
             *
             * @param $slug
             *
             * @return mixed|string
             */
            public function module_status( $slug ) {
                $settings = $this->fta_get_settings();
                $status = $settings['plugins'][$slug]['status'];
                if ( empty( $status ) ) {
                    $status = 'activated';
                }
                return $status;
            }

        }

        new Feed_Them_All();
    }
}