<?php

/*
  Plugin Name: ELEX Product Feed Social
  Plugin URI: https://elextensions.com/plugin/woocommerce-catalog-feed-facebook-instagram-free/
  Description: Generate and Sync Facebook & Instagram Shopping Feeds from WooCommerce Product Catalog.
  Version: 3.1.4
  WC requires at least: 2.6.0
  WC tested up to: 10.0.0
  Author: ELEXtensions
  Author URI: https://elextensions.com
  Developer: ELEXtensions
  Developer URI: https://elextensions.com
  Text Domain: elex-product-feed
 */

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


// for Required functions
if ( ! function_exists( 'elex_fb_basic_is_woocommerce_active' ) ) {
	require_once  'elex-includes/elex-fb-basic-functions.php';
}
// to check woocommerce is active
if ( ! ( elex_fb_basic_is_woocommerce_active() ) ) {
	add_action( 'admin_notices', 'woocommerce_activation_notice_in_basic' );
	return;
}

function woocommerce_activation_notice_in_basic() {  ?>
	<div id="message" class="error">
		<p>
			<?php echo( esc_attr_e( 'WooCommerce plugin must be active for ELEX Product Feed Social plugin to work.', 'elex-product-feed' ) ); ?>
		</p>
	</div>
	<?php
}
//adding wpfluent library
require 'includes/wp-fluent/autoload.php';

$activated_plugins = (array) get_option( 'active_plugins', array() );

if ( in_array( 'elex-facebook-feed/elex-facebook-feed.php', $activated_plugins, true ) || in_array( 'elex-facebook-feed-basic/elex-facebook-feed-basic.php', $activated_plugins, true ) ) {
	deactivate_plugins( basename( __FILE__ ) );
	wp_die( esc_html__( 'Oops! You tried installing the basic version without deactivating the premium version. Kindly deactivate ELEX WooCommerce Catalog Feed for Facebook & Instagram and then try again', 'elex-product-feed' ), '', array( 'back_link' => 1 ) );

} else {

	if ( ! defined( 'ELEX_FACEBOOK_FEED_PLUGIN_PATH' ) ) {
		define( 'ELEX_FACEBOOK_FEED_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
	}
	if ( ! defined( 'ELEX_FACEBOOK_FEED_TEMPLATE_PATH' ) ) {
		define( 'ELEX_FACEBOOK_FEED_TEMPLATE_PATH', ELEX_FACEBOOK_FEED_PLUGIN_PATH . 'templates' );
	}
	if ( ! defined( 'ELEX_FACEBOOK_FEED_MAIN_URL_PATH' ) ) {
		define( 'ELEX_FACEBOOK_FEED_MAIN_URL_PATH', plugin_dir_url( __FILE__ ) );
	}
	if ( ! defined( 'ELEX_FB_PREMIUM' ) ) {
		define( 'ELEX_FB_PREMIUM', 'basic' );
	} 
	function elex_fb_premium() {
		return defined( 'ELEX_FB_PREMIUM' ) && 'premium' === ELEX_FB_PREMIUM ;
	}
	
	add_action( 'admin_menu', 'elex_fpf_add_menu' );
	
	function elex_fpf_add_menu() {
		add_menu_page( 'Facebook Shopping Feed', 'Facebook Shopping Feed', 'manage_options', 'elex-facebook-feed-manage', 'elex_fpf_sub_menu', esc_url( plugins_url( '/assets/icons/elex-logo.svg', __FILE__ ) ) );
		add_submenu_page( 'elex-facebook-feed-manage', 'Facebook Shopping Feed', 'Manage Feeds', 'manage_options', 'elex-facebook-feed-manage', 'elex_fpf_sub_menu' );
		add_submenu_page( 'elex-facebook-feed-manage', 'Facebook Shopping Feed', 'Create Feed', 'manage_options', 'elex-facebook-feed', 'elex_fpf_product_feed_actions' );
		add_submenu_page( 'elex-facebook-feed-manage', 'Facebook Shopping Feed', 'Settings', 'manage_options', 'elex-facebook-feed-settings', 'elex_fpf_settings_tab_content' );
		if ( ! elex_fb_premium() ) {
			add_submenu_page( 'elex-facebook-feed-manage', 'Facebook Shopping Feed', 'Go Premium!', 'manage_options', 'elex-facebook-feed-go-premium', 'elex_fpf_go_premium' );
		} else {
			add_submenu_page( 'elex-facebook-feed-manage', 'Facebook Shopping Feed', 'Licence', 'manage_options', 'elex-facebook-feed-licence', 'elex_fpf_add_licence' );
		}

	}
	
	function elex_fpf_settings_tab_content() {
		?>
		<h2 class='nav-tab-wrapper'>
			<a href='admin.php?page=elex-facebook-feed-manage' class='nav-tab  '><?php esc_html_e( 'Manage Feeds', 'elex-product-feed' ); ?></a>
			<a href='admin.php?page=elex-facebook-feed' class='nav-tab'><?php esc_html_e( 'Create Feed', 'elex-product-feed' ); ?></a>
			<a href='admin.php?page=elex-facebook-feed-settings' class='nav-tab nav-tab-active'><?php esc_html_e( 'Settings', 'elex-product-feed' ); ?></a>
		<?php 
		if ( ! elex_fb_premium() ) {
			?>
			<a href='admin.php?page=elex-facebook-feed-go-premium' style="color:red;" class='nav-tab'><?php esc_html_e( 'Go Premium!', 'elex-product-feed' ); ?></a>
		<?php } else { ?>
			<a href='admin.php?page=elex-facebook-feed-licence' class='nav-tab'><?php esc_html_e( 'Licence', 'elex-product-feed' ); ?></a>
		<?php } ?>    
		</h2>
		<?php
		include_once 'templates/class-elex-settings-tab-fields.php';
	}
	
	
	function elex_fpf_sub_menu() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) );
		}
		
		include_once 'includes/elex-manage-feeds-tab.php';
	}
	
	function elex_fpf_go_premium() {
		?>
		<h2 class="nav-tab-wrapper">
			<a href="admin.php?page=elex-facebook-feed-manage" class="nav-tab  "><?php esc_html_e( 'Manage Feeds', 'elex-product-feed' ); ?></a>
			<a href="admin.php?page=elex-facebook-feed" class="nav-tab"><?php esc_html_e( 'Create Feed', 'elex-product-feed' ); ?></a>
			<a href='admin.php?page=elex-facebook-feed-settings' class='nav-tab '><?php esc_html_e( 'Settings', 'elex-product-feed' ); ?></a>

		<?php 
		if ( ! elex_fb_premium() ) {
			?>
			<a href='admin.php?page=elex-facebook-feed-go-premium' style="color:red;" class='nav-tab nav-tab-active'><?php esc_html_e( 'Go Premium!', 'elex-product-feed' ); ?></a>
		<?php } else { ?>
			<a href='admin.php?page=elex-facebook-feed-licence' class='nav-tab'><?php esc_html_e( 'Licence', 'elex-product-feed' ); ?></a>
		<?php } ?>        
		</h2>
		<br>
		<?php
		global $woocommerce;
		$woocommerce_version = function_exists( 'WC' ) ? WC()->version : $woocommerce->version;
		wp_enqueue_style( 'elex-fpf-bootstrap', ELEX_FACEBOOK_FEED_MAIN_URL_PATH . 'resources/css/market-bootstrap.css', array(), $woocommerce_version );
		include_once  'includes/market.php' ;
	}
	
	
	function elex_fpf_product_feed_actions() {
		
		include_once 'includes/elex-settings-section.php';
	}
	function elex_fpf_add_licence() {
		?>
		<h2 class="nav-tab-wrapper">
			<a href="admin.php?page=elex-facebook-feed-manage" class="nav-tab  "><?php esc_html_e( 'Manage Feeds', 'elex-product-feed' ); ?></a>
			<a href="admin.php?page=elex-facebook-feed" class="nav-tab"><?php esc_html_e( 'Create Feed', 'elex-product-feed' ); ?></a>
			<a href='admin.php?page=elex-facebook-feed-settings' class='nav-tab '><?php esc_html_e( 'Settings', 'elex-product-feed' ); ?></a>
		<?php 
		if ( ! elex_fb_premium() ) {
			?>
			<a href='admin.php?page=elex-facebook-feed-go-premium' style="color:red;" class='nav-tab'><?php esc_html_e( 'Go Premium', 'elex-product-feed' ); ?></a>
		<?php } else { ?>
				<a href="admin.php?page=elex-facebook-feed-licence" class="nav-tab nav-tab-active"><?php esc_html_e( 'Licence', 'elex-product-feed' ); ?></a>
		<?php } ?>        
		</h2>
		<br>
		<?php
		$plugin_name = 'elex-facebook-feed';
		include_once 'includes/wf_api_manager/html/html-wf-activation-window.php';
	}
	
	function elex_fpf_register_admin_page_menu() {
		$mypage = add_submenu_page(
			'exlude product',
			'ELEX Exclude Products',
			'ELEX Exclude Products',
			'manage_options',
			'elex-fpf-excluded-products',
			'elex_fpf_admin_page_content'
		);
		add_action( 'load-' . $mypage, 'elex_fpf_load_admin_page_menu' );
	}
	add_action( 'admin_menu', 'elex_fpf_register_admin_page_menu' );
	
	function elex_fpf_load_admin_page_menu() {
		global $woocommerce;
		$woocommerce_version = function_exists( 'WC' ) ? WC()->version : $woocommerce->version;
		wp_register_style( 'elex-exclude-product-page-style', ELEX_FACEBOOK_FEED_MAIN_URL_PATH . '/assets/css/elex-exclude-product-page-style.css', array(), $woocommerce_version );
		wp_enqueue_style( 'elex-exclude-product-page-style' );
		wp_register_script( 'elex-exclude-product-page-script', ELEX_FACEBOOK_FEED_MAIN_URL_PATH . '/assets/js/elex-exclude-products-script.js', array(), $woocommerce_version, false );
		wp_enqueue_script( 'elex-exclude-product-page-script' );
	}
	
	function elex_fpf_admin_page_content() {
		global $wpdb;
		$feed_id    = isset( $_GET['feed_id'] ) ? sanitize_text_field( $_GET['feed_id'] ) : '';
		$date_time  = isset( $_GET['date'] ) ? sanitize_text_field( $_GET['date'] ) : '';
		$prod_type  = isset( $_GET['type'] ) ? sanitize_text_field( $_GET['type'] ) : '';
		$table_name = $wpdb->prefix . 'fpf_feeds';
		
		$meta_content        = elex_fpf_get_feed_data( $feed_id, 'report_data' );
		$report_data         = json_decode( $meta_content[0]['feed_meta_content'], true );
		$search              = isset( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
		$manage_feed_content = elex_fpf_get_feed_data( $feed_id, 'manage_feed_data' );
		$manage_feed_data    = json_decode( $manage_feed_content[0]['feed_meta_content'], true );
		?>
		<!DOCTYPE html>
		<html>
		<div style="padding-top: 2%;"></div>
		<body>
		<ul class="header_banner">
			<li class="elex-sub-header">
				<ul  class="sub_header_banner">
					<li class="elex_delete_image_element">
		 <?php
			if ( 'simple' === $prod_type ) {
				$text = esc_html__( 'List of Simple products excluded from', 'elex-product-feed' );
			} else {
				$text = esc_html__( 'List of Variations excluded from', 'elex-product-feed' );
			}
			echo esc_html( $text . ' ' . $manage_feed_data ['name'] );
			?>
					</li>
					<li>
					</li>
				</ul>
		<?php echo esc_html__( ' Generated on', 'elex-product-feed' ) . ' ' . esc_html( $manage_feed_data ['modified_date'] ); ?>
				<br>
				<br>
				<a href="admin.php?page=elex-facebook-feed-manage"><strong>Back to Manage Feeds</strong></a>
			</li>
	
		<li class="elex-exc-prod-img" style="float:right"><img src="<?php echo esc_url( plugin_dir_url( __FILE__ ) . 'assets/images/product-image.png' ); ?>"></li>
		</ul>
	
		</body>
		</html>
		<div style="padding: 1% 2% 0% 75%;"><input value="<?php echo esc_html( $search ); ?>" style=" width: 65%;" type="text" id="elex_exclude_search_name"  placeholder="Search by Product name.." title="Type in a name"> <button onclick="elex_exclude_search_name_fun()" class="botton button-large button-primary">search</button><br></div>
		<table style="width: 95%;" id="elex_excluded_prods" class="wrap postbox ">
			<tr>
				<th class='elex-fpf-exclude-id'>ID</th>
				<th class='elex-fpf-exclude-name'>Name</th>
				<th class='elex-fpf-exclude-reason'>Reason</th>
			</tr>
		<?php
		if ( ! isset( $_GET['feed_page'] ) ) {
			$current_page = 1;
		} else {
			$current_page = sanitize_text_field( $_GET['feed_page'] );
		}
			$max_pagination          = 4;
			$products_each_iteration = 0;
			$start                   = ( $current_page - 1 ) * 100;
			$is_break                = false;
			$counter                 = 0;
		if ( ! empty( $report_data[ $date_time ][ $prod_type ] ) ) {
			foreach ( $report_data[ $date_time ][ $prod_type ] as $google_attr => $prod_ids_array ) {
				foreach ( $prod_ids_array as  $id_prod_title ) {
					$prod_id    = explode( '-', $id_prod_title )[0];
					$prod_title = str_replace( $prod_id . '-', '', $id_prod_title );
					if ( ! $search || false !== strpos( strtolower( $prod_title ), strtolower( $search ) ) ) {
						if ( $counter >= $start ) {
							if ( 100 === $products_each_iteration ) {
									 $is_break = true;
									 break;
							}
							$product = wc_get_product( $prod_id );
							if ( $product ) {
									
	
								$permalink = $product->get_permalink();
								?>
										<tr>
											<td class='elex-fpf-exclude-id'><a target="_blank" href="<?php echo esc_html( $permalink ); ?>"><?php echo esc_html( $prod_id ); ?></a></td>
											<td class='elex-fpf-exclude-name'><?php echo esc_html( htmlspecialchars_decode( $prod_title ) ); ?></td>
											<td class='elex-fpf-exclude-reason'><?php echo ( '<b>' . esc_html( $google_attr ) . '</b> is missing or invalid' ); ?></td>
										</tr>
								<?php
								$products_each_iteration++;
							}
						}
						 $counter++;
					}
				}
				if ( $is_break ) {
					break;
				}
			}
		}
		?>
		</table>
		<?php
		$pagination = 0;
		$type       = 'total_' . $prod_type;
		if ( $products_each_iteration < 100 ) {
			$total_num = $counter;
		} else {
			$total_num = 0;
			if ( ! empty( $report_data[ $date_time ][ $prod_type ] ) ) {
				foreach ( $report_data[ $date_time ][ $prod_type ] as $key => $value ) {
					$total_num += count( $value );
				}
			}
		}
		if ( $total_num ) {
			$pagination = ceil( ( $total_num ) / 100 );
		}
		if ( $pagination > 1 ) {
			if ( $pagination === $max_pagination ) {
				$start = 1;
			} else {
				$start = $current_page;
				if ( $pagination === $current_page ) {
					$start = $current_page - 1;
				}
			}
			if ( 1 === $current_page ) {
				$left_disable = 'pag-disable';
				$left_href    = '#';
			} else {
				$left_disable = '';
				$left_href    = 'admin.php?page=elex-fpf-excluded-products&feed_id=' . $feed_id . '&date=' . $date_time . '&type=' . $prod_type . '&feed_page=' . ( $current_page - 1 ) . '&search=' . $search;
			}
			if ( $pagination === $current_page ) {
				$right_disable = 'pag-disable';
				$right_href    = '#';
			} else {
				$right_disable = '';
				$right_href    = 'admin.php?page=elex-fpf-excluded-products&feed_id=' . $feed_id . '&date=' . $date_time . '&type=' . $prod_type . '&feed_page=' . ( $current_page + 1 ) . '&search=' . $search;
			}
			?>
				<div style="padding-left: 73%;" class="pagination">
					<a href="<?php echo esc_html( $left_href ); ?> " class="<?php echo esc_html( $left_disable ); ?> ">&laquo;</a>
			<?php
	
			for ( $flag = $start; $flag <= $pagination; $flag++ ) {
				$active = '';
				if ( $flag === $current_page ) {
					  $active = 'pag-active';
				}
				$href = 'admin.php?page=elex-fpf-excluded-products&feed_id=' . $feed_id . '&date=' . $date_time . '&type=' . $prod_type . '&feed_page=' . $flag . '&search=' . $search;
				?>
					<a href="<?php echo esc_html( $href ); ?>" class="<?php echo esc_html( $active ); ?> " > <?php echo esc_html( $flag ); ?> </a>
				<?php
				if ( $flag >= $start + $max_pagination - 1 ) {
					  break;
				}
			}
			?>
					<a href="<?php echo esc_html( $right_href ); ?> " class="<?php echo esc_html( $right_disable ); ?> ">&raquo;</a>
				</div>
			<?php
		}
	}
	
	add_filter( 'cron_schedules', 'elex_fpf_custom_schedules' );
	
	function elex_fpf_custom_schedules( $schedules ) {
		$schedules['every_thirty_minutes'] = array(
			'interval' => 1800,
			'display'  => __( 'Every thirty minutes', 'elex-product-feed' ),
		);
		return $schedules;
	}
	
	if ( ! wp_next_scheduled( 'elex_run_every_thirty_minutes' ) ) {
		wp_schedule_event( time(), 'every_thirty_minutes', 'elex_run_every_thirty_minutes' );
	}
	add_action( 'elex_run_every_thirty_minutes', 'elex_fpf_cron_function' );
	

	function elex_fpf_cron_function() {
		include_once 'includes/elex-cron-schedule.php';
	}
	add_action( 'init', 'elex_fpf_include_files' );
	
	
	function elex_fpf_include_files() {
		include_once 'includes/elex-ajax-functions.php';
		include_once 'includes/elex-manage-feed-ajax.php';
		include_once 'includes/elex-add-custom-fields.php';
		include_once 'includes/elex-save-settings-tab-fields.php';
		include_once 'includes/elex-plugin-install-functions.php';
		include_once 'includes/elex-plugin-public-functions.php';

	}
	
	add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'elex_fpf_product_feed_action_links' );
	
	function elex_fpf_product_feed_action_links( $links ) {
		$plugin_links = array(
			'<a href="' . admin_url( 'admin.php?page=elex-facebook-feed' ) . '">' . __( 'Settings', 'elex-product-feed' ) . '</a>',
			'<a href="https://elextensions.com/knowledge-base/set-up-elex-woocommerce-catalog-feed-facebook-instagram/" target="_blank">' . __( 'Documentation', 'elex-product-feed' ) . '</a>',
			'<a href="https://elextensions.com/support/" target="_blank">' . __( 'Support', 'elex-product-feed' ) . '</a>',
		);
		return array_merge( $plugin_links, $links );
	}
	function elex_fpf_load_plugin_textdomain() {
		load_plugin_textdomain( 'elex-product-feed', false, basename( dirname( __FILE__ ) ) . '/language/' );
	}
	add_action( 'plugins_loaded', 'elex_fpf_load_plugin_textdomain' );
	
	add_action( 'current_screen', 'elex_fpf_register_product_page_styles_scripts' );


	function elex_fpf_register_product_page_styles_scripts() {
		$screen = get_current_screen();
		if ( 'product' === $screen->id ) {
			global $woocommerce;
			$woocommerce_version = function_exists( 'WC' ) ? WC()->version : $woocommerce->version;
			wp_enqueue_script( 'elex-woocom-product-page-script', ELEX_FACEBOOK_FEED_MAIN_URL_PATH . '/assets/js/elex-woocom-product-page-script.js', array(), $woocommerce_version, false );
			wp_enqueue_style( 'elex-woocom-product-page-style', ELEX_FACEBOOK_FEED_MAIN_URL_PATH . '/assets/css/elex-woocom-product-page-style.css', array(), $woocommerce_version, false );

		}
		
	}
	// review component
	if ( ! function_exists( 'get_plugin_data' ) ) {
		include_once  ABSPATH . 'wp-admin/includes/plugin.php';
	}
	include_once __DIR__ . '/review_and_troubleshoot_notify/review-and-troubleshoot-notify-class.php';
	$data                      = get_plugin_data( __FILE__ , false, false);
	$data['name']              = $data['Name'];
	$data['basename']          = plugin_basename( __FILE__ );
	$data['rating_url']        = 'https://elextensions.com/plugin/woocommerce-catalog-feed-facebook-instagram-free/#reviews';
	$data['documentation_url'] = 'https://elextensions.com/knowledge-base/set-up-elex-woocommerce-catalog-feed-facebook-instagram/';
	$data['support_url']       = 'https://wordpress.org/support/plugin/elex-product-feed/';
}

new Elex_Review_Components( $data );

