<?php
/**
 * Getting Started Page.
 *
 * @package Feminine_Lifestyle_Blog
 */


if( ! function_exists( 'feminine_lifestyle_blog_getting_started_admin_scripts' ) ) :
    /**
     * Load Getting Started styles in the admin
     */
    function feminine_lifestyle_blog_getting_started_admin_scripts( $hook ){

        // Load styles only on our page
        if( 'appearance_page_feminine-lifestyle-blog-theme-options' != $hook ) return;
        
        wp_enqueue_style( 'feminine-lifestyle-blog-flb-notice', get_template_directory_uri() . '/inc/admin/flb-notice/css/flb-notice.css', false, FEMININE_LIFESTYLE_BLOG_THEME_VERSION );

        wp_enqueue_style( 'feminine-lifestyle-blog-admin-fonts', 'https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap', false );
        
        wp_enqueue_script( 'feminine-lifestyle-blog-flb-notice', get_template_directory_uri() . '/inc/admin/flb-notice/js/flb-notice.js', array( 'jquery' ), FEMININE_LIFESTYLE_BLOG_THEME_VERSION, true );
        wp_enqueue_script( 'feminine-lifestyle-blog-recommended-plugin-install', get_template_directory_uri() . '/inc/admin/flb-notice/js/recommended-plugin-install.js', array( 'jquery','plugin-install','wp-util','updates' ), FEMININE_LIFESTYLE_BLOG_THEME_VERSION, true );    
        $localize = array(
            'ajaxUrl'              => admin_url( 'admin-ajax.php' ),
            'ActivatingText'       => __( 'Activating', 'feminine-lifestyle-blog' ),
            'DeactivatingText'     => __( 'Deactivating', 'feminine-lifestyle-blog' ),
            'PluginActivateText'   => __( 'Activate', 'feminine-lifestyle-blog' ),
            'PluginDeactivateText' => __( 'Deactivate', 'feminine-lifestyle-blog' ),
            'SettingsText'         => __( 'Settings', 'feminine-lifestyle-blog' ),
        );  
        wp_localize_script( 'feminine-lifestyle-blog-recommended-plugin-install', 'flb_notice_page', $localize );
    }
    endif;
add_action( 'admin_enqueue_scripts', 'feminine_lifestyle_blog_getting_started_admin_scripts' );

if( ! function_exists( 'feminine_lifestyle_blog_call_plugin_api' ) ) :
	/**
	 * Plugin API
	**/
	function feminine_lifestyle_blog_call_plugin_api( $plugin ) {
		include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
		$call_api = plugins_api( 
			'plugin_information', 
				array(
				'slug'   => $plugin,
				'fields' => array(
					'downloaded'        => false,
					'rating'            => false,
					'description'       => false,
					'short_description' => true,
					'donate_link'       => false,
					'tags'              => false,
					'sections'          => true,
					'homepage'          => true,
					'added'             => false,
					'last_updated'      => false,
					'compatibility'     => false,
					'tested'            => false,
					'requires'          => false,
					'downloadlink'      => false,
					'icons'             => true
				)
			) 
		);
		return $call_api;
	}
endif;
	
/**
 * Required Plugin Activate
 *
 * @since 1.2.4
 */
function required_plugin_activate() {

	if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! $_POST['init'] ) {
		wp_send_json_error(
			array(
				'success' => false,
				'message' => __( 'No plugin specified', 'feminine-lifestyle-blog' ),
			)
		);
	}

	$plugin_init = ( isset( $_POST['init'] ) ) ? esc_attr( $_POST['init'] ) : '';

	$activate = activate_plugin( $plugin_init, '', false, true );

	if ( is_wp_error( $activate ) ) {
		wp_send_json_error(
			array(
				'success' => false,
				'message' => $activate->get_error_message(),
			)
		);
	}

	wp_send_json_success(
		array(
			'success' => true,
			'message' => __( 'Plugin Successfully Activated', 'feminine-lifestyle-blog' ),
		)
	);

}
add_action('wp_ajax_gs-sites-plugin-activate', 'required_plugin_activate');
add_action('wp_ajax_nopriv_gs-sites-plugin-activate', 'required_plugin_activate');
	
/**
 * Required Plugin Activate
 *
 * @since 1.2.4
 */
function required_plugin_deactivate() {
	
		if ( ! current_user_can( 'install_plugins' ) || ! isset( $_POST['init'] ) || ! $_POST['init'] ) {
			wp_send_json_error(
				array(
					'success' => false,
					'message' => __( 'No plugin specified', 'feminine-lifestyle-blog' ),
				)
			);
		}
	
		$plugin_init = ( isset( $_POST['init'] ) ) ? esc_attr( $_POST['init'] ) : '';
	
		$deactivate = deactivate_plugins( $plugin_init, '', false );
	
		if ( is_wp_error( $deactivate ) ) {
			wp_send_json_error(
				array(
					'success' => false,
					'message' => $deactivate->get_error_message(),
				)
			);
		}
	
		wp_send_json_success(
			array(
				'success' => true,
				'message' => __( 'Plugin Successfully Deactivated', 'feminine-lifestyle-blog' ),
			)
		);
	
	}
add_action('wp_ajax_gs-sites-plugin-deactivate', 'required_plugin_deactivate');
add_action('wp_ajax_nopriv_gs-sites-plugin-deactivate', 'required_plugin_deactivate');
	
if( ! function_exists( 'feminine_lifestyle_blog_check_for_icon' ) ) :
	/**
	 * Check For Icon 
	**/
	function feminine_lifestyle_blog_check_for_icon( $arr ) {
		if( ! empty( $arr['svg'] ) ){
			$plugin_icon_url = $arr['svg'];
		}elseif( ! empty( $arr['2x'] ) ){
			$plugin_icon_url = $arr['2x'];
		}elseif( ! empty( $arr['1x'] ) ){
			$plugin_icon_url = $arr['1x'];
		}else{
			$plugin_icon_url = $arr['default'];
		}                               
		return $plugin_icon_url;
	}
endif;