<?php

/**
 *
 * @link              https://magic-post-thumbnail.com/
 * @since             1.0.0
 * @package           Magic_Post_Thumbnail
 *
 * @wordpress-plugin
 * Plugin Name:       Magic Post Thumbnail
 * Plugin URI:        http://wordpress.org/plugins/magic-post-thumbnail/
 * Description:       Add stunning images to your posts effortlessly, as featured images or within content. Magic Post Thumbnail sources them automatically from multiple image banks.
 * Version:           6.2.1
 * Author:            Magic Post Thumbnail
 * Author URI:        https://magic-post-thumbnail.com/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       magic-post-thumbnail
 * Domain Path:       /languages
 *
 *
 *
 *
 */
// If this file is called directly, abort.
if ( !defined( 'WPINC' ) ) {
    die;
}
if ( function_exists( 'mpt_freemius' ) ) {
    mpt_freemius()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'mpt_freemius' ) ) {
        function mpt_freemius() {
            global $mpt_freemius;
            if ( !isset( $mpt_freemius ) ) {
                // Include Freemius SDK.
                require_once dirname( __FILE__ ) . '/admin/partials/freemius/start.php';
                $mpt_freemius = fs_dynamic_init( array(
                    'id'               => '2891',
                    'slug'             => 'magic-post-thumbnail',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_0842b408e487a0001e564a31d3a37',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'has_affiliation'  => 'selected',
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'        => 'magic-post-thumbnail-admin-display',
                        'first-path'  => 'admin.php?page=magic-post-thumbnail-admin-display',
                        'contact'     => false,
                        'support'     => false,
                        'affiliation' => false,
                        'account'     => false,
                        'addons'      => false,
                    ),
                    'navigation'       => 'tabs',
                    'is_live'          => true,
                ) );
            }
            return $mpt_freemius;
        }

        // Init Freemius.
        mpt_freemius();
        // Signal that SDK was initiated.
        do_action( 'mpt_freemius_loaded' );
    }
    /**
     * Currently plugin version.
     * Start at version 1.0.0 and use SemVer - https://semver.org
     * Rename this for your plugin and update it as you release new versions.
     */
    define( 'MAGIC_POST_THUMBNAIL_VERSION', '6.2.1' );
    /**
     * The code that runs during plugin activation.
     * This action is documented in includes/class-magic-post-thumbnail-activator.php
     */
    function activate_magic_post_thumbnail() {
        require_once plugin_dir_path( __FILE__ ) . 'includes/class-magic-post-thumbnail-activator.php';
        Magic_Post_Thumbnail_Activator::activate();
    }

    /**
     * The code that runs during plugin deactivation.
     * This action is documented in includes/class-magic-post-thumbnail-deactivator.php
     */
    function deactivate_magic_post_thumbnail() {
        require_once plugin_dir_path( __FILE__ ) . 'includes/class-magic-post-thumbnail-deactivator.php';
        Magic_Post_Thumbnail_Deactivator::deactivate();
    }

    register_activation_hook( __FILE__, 'activate_magic_post_thumbnail' );
    register_deactivation_hook( __FILE__, 'deactivate_magic_post_thumbnail' );
    /**
     * The core plugin class that is used to define internationalization,
     * admin-specific hooks, and public-facing site hooks.
     */
    require plugin_dir_path( __FILE__ ) . 'includes/class-magic-post-thumbnail.php';
    /**
     * Add capabilities
     */
    function MPT_add_capability() {
        $options = get_option( 'MPT_plugin_rights_settings' );
        // Administrators always have the capability
        $admin_role = get_role( 'administrator' );
        if ( $admin_role ) {
            $admin_role->add_cap( 'mpt_manage', true );
        }
        // Manage other roles by adding or removing capabilities according to options
        $roles = array(
            'editor'      => 'rights_editor',
            'author'      => 'rights_author',
            'contributor' => 'rights_contributor',
            'subscriber'  => 'rights_subscriber',
        );
        foreach ( $roles as $role_name => $option_key ) {
            $role = get_role( $role_name );
            if ( $role ) {
                if ( isset( $options[$option_key] ) && $options[$option_key] === 'true' ) {
                    // Adds capacity if the option is enabled
                    $role->add_cap( 'mpt_manage', true );
                } else {
                    // Removes the capacity if the option is deactivated
                    $role->remove_cap( 'mpt_manage' );
                }
            }
        }
    }

    /**
     * Check hook wp_insert_post to fire functionalities
     *
     * @since    6.0.0
     */
    function MPT_check_hook() {
    }

    /**
     * Check capabilities before launching the plugin main features
     *
     * @since    5.0.0
     */
    function MPT_check_capability() {
        $additional_check = false;
        // add capability for the cron
        if ( wp_doing_cron() || true == $additional_check ) {
            global $current_user;
            $current_user->add_cap( 'mpt_manage' );
        }
        if ( current_user_can( 'mpt_manage' ) ) {
            $plugin = new Magic_Post_Thumbnail();
            $plugin->run();
        }
    }

    /**
     * Begins execution of the plugin.
     *
     * Since everything within the plugin is registered via hooks,
     * then kicking off the plugin from this point in the file does
     * not affect the page life cycle.
     *
     * @since    4.0.0
     */
    function run_magic_post_thumbnail() {
        // User role & capacity
        add_action( 'init', 'MPT_add_capability', 1 );
        add_action( 'init', 'MPT_check_capability', 2 );
        // Features with hooks
        add_action( 'init', 'MPT_check_hook', 5 );
    }

    run_magic_post_thumbnail();
    // Fired when the plugin is uninstalled.
    mpt_freemius()->add_action( 'after_uninstall', 'mpt_freemius_uninstall_cleanup' );
    function mpt_freemius_uninstall_cleanup() {
        // Remove logs
        define( "MPT_FREEMIUS_UNINSTALL", true );
        require_once dirname( __FILE__ ) . '/admin/partials/delete_log.php';
        // Delete all plugin options
        delete_option( 'MPT_plugin_posts_settings' );
        delete_option( 'MPT_plugin_main_settings' );
        delete_option( 'MPT_plugin_banks_settings' );
        delete_option( 'MPT_plugin_interval_settings' );
        delete_option( 'MPT_plugin_cron_settings' );
        delete_option( 'MPT_plugin_rights_settings' );
        delete_option( 'MPT_plugin_proxy_settings' );
        delete_option( 'MPT_plugin_compatibility_settings' );
        delete_option( 'MPT_plugin_logs_settings' );
    }

}
add_action( 'init', function () {
    if ( !current_user_can( 'manage_options' ) ) {
        return;
    }
    if ( isset( $_GET['mpt_test_google'] ) ) {
        $search = 'test image';
        $params = array(
            'tbm'  => 'isch',
            'q'    => urlencode( $search ),
            'hl'   => 'en',
            'safe' => 'medium',
            'rsz'  => '3',
        );
        $url = add_query_arg( $params, 'https://www.google.com/search' );
        $defaults = array(
            'redirection'        => 9,
            'user-agent'         => 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) Chrome/41.0.2272.96',
            'reject_unsafe_urls' => false,
            'sslverify'          => false,
        );
        $result = wp_remote_request( $url, $defaults );
        header( 'Content-Type: text/plain; charset=utf-8' );
        if ( is_wp_error( $result ) ) {
            var_dump( $result->get_error_message() );
            exit;
        }
        echo "HTTP CODE: " . $result['response']['code'] . "\n\n";
        echo substr( $result['body'], 0, 50000 );
        // début du HTML
        exit;
    }
} );