<?php
/**
 * Plugin Name: Majalahpro Core
 * Plugin URI: https://www.idtheme.com/majalahpro/
 * Description: Majalahpro Core - Add functionally to magazine theme for easy maintenance. This plugin using only for theme with blog type from idtheme.com
 * Author: Gian Mokhammad R
 *
 * @package Majalahpro Core
 * Version: 1.2.1
 * Author URI: http://www.gianmr.com
 */

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

/**
 * If using amp endpoint
 *
 * @since v.1.1.3
 */
function majalahpro_core_is_amp() {
	return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}

if ( ! class_exists( 'Majalahpro_Core_Init' ) ) {
	/**
	 * Main Plugin Class
	 */
	class Majalahpro_Core_Init {
		/**
		 * Contract
		 *
		 * @since 1.0.0
		 * @access public
		 */
		public function __construct() {
			/* Define */
			define( 'MAJALAHPRO_CORE_VER', '1.1.2' );
			define( 'MAJALAHPRO_CORE_DIRNAME', plugin_dir_path( __FILE__ ) );
			define( 'MAJALAHPRO_CORE_URL', plugin_dir_url( __FILE__ ) );

			/* this is the URL our updater / license checker pings. This should be the URL of the site */
			define( 'MAJALAHPRO_API_URL_CHECK', 'http://member.kentooz.com/softsale/api/check-license' );
			define( 'MAJALAHPRO_API_URL', 'http://member.kentooz.com/softsale/api/activate' );
			define( 'MAJALAHPRO_API_URL_DEACTIVATED', 'http://member.kentooz.com/softsale/api/deactivate' );

			/* the name of the settings page for the license input to be displayed */
			define( 'MAJALAHPRO_PLUGIN_LICENSE_PAGE', 'majalahpro-license' );

			/* Include widget */
			include MAJALAHPRO_CORE_DIRNAME . 'widgets/feedburner-widget.php';
			include MAJALAHPRO_CORE_DIRNAME . 'widgets/custom-form-widget.php';
			include MAJALAHPRO_CORE_DIRNAME . 'widgets/recent-posts-widget.php';
			include MAJALAHPRO_CORE_DIRNAME . 'widgets/tags-widget.php';

			/* Include library */
			include MAJALAHPRO_CORE_DIRNAME . 'lib/breadcrumbs.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/banner.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/external/class.settings-api.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/authorbox.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/relatedpost.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/headfooterscript.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/gallery.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/jetpack.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/faster.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/metabox.php';
			include MAJALAHPRO_CORE_DIRNAME . 'lib/taxonomy.php';

			/**
			 * Include Amember HTTP API
			 * Load only if dashboard
			 */
			if ( is_admin() ) {
				include_once MAJALAHPRO_CORE_DIRNAME . 'lib/amember_http_api.php';
			}

			/* Action */
			add_action( 'plugins_loaded', array( $this, 'majalahpro_core_load_textdomain' ) );

			add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );

			// Enable the use of shortcodes in text widgets.
			add_filter( 'widget_text', 'do_shortcode' );

			$majpro_social = get_option( 'majpro_social' );

			if ( isset( $majpro_social['enable_fb_comment'] ) && ! empty( $majpro_social['enable_fb_comment'] ) ) {
				/* option section default */
				$option = $majpro_social['enable_fb_comment'];
			} else {
				$option = 'off';
			}

			/* if option on then using fb comment */
			if ( 'on' === $option ) {
				add_filter( 'comments_template', array( $this, 'fb_comments_template' ), 20 );
			}

			$status = trim( get_option( 'majalahpro_core_license_status' . md5( home_url() ) ) );

			if ( ! empty( $status ) && 'ok' === $status ) {
				include_once MAJALAHPRO_CORE_DIRNAME . 'lib/z_setting.majalahpro-core.php';
				/* Other functionally */
				include_once MAJALAHPRO_CORE_DIRNAME . 'lib/update/plugin-update-checker.php';
				$MyUpdateChecker = PucFactory::buildUpdateChecker(
					'http://www.kentooz.com/files/majalahpro-core/majalahproindoautouptberita.json',
					__FILE__,
					'majalahpro-core'
				);
			} else {
				include_once MAJALAHPRO_CORE_DIRNAME . 'lib/z_license.majalahpro-core.php';
			}

		}

		/**
		 * Activated plugin
		 *
		 * @since 1.0.0
		 * @access public
		 */
		public static function majalahpro_core_activate() {
			/* nothing to do yet */
		}

		/**
		 * Deativated plugin
		 *
		 * @since 1.0.0
		 * @access public
		 */
		public static function majalahpro_core_deactivate() {
			/* nothing to do yet */
		}

		/**
		 * Fb comment
		 *
		 * @since 1.0.2
		 * @access public
		 */
		public function fb_comments_template() {
			return MAJALAHPRO_CORE_DIRNAME . 'lib/fb-comment.php';
		}

		/**
		 * Enqueue assets
		 *
		 * @since 1.0.0
		 * @access public
		 */
		public function register_scripts() {

			$majpro_ads = get_option( 'majpro_ads' );

			$majpro_ajax = get_option( 'majpro_ajax' );

			if ( isset( $majpro_ajax['enable_ajax_navigation'] ) && ! empty( $majpro_ajax['enable_ajax_navigation'] ) ) {
				/* option section default */
				$option_ajax = $majpro_ajax['enable_ajax_navigation'];
			} else {
				$option_ajax = 'on';
			}
			if ( 'on' === $option_ajax ) {

				if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'infinite-scroll' ) ) {
					/* If jetpack enable, disable this feature */
				} else {
					if ( isset( $majpro_ajax['ajax_navigation_type'] ) && 'infinite' === $majpro_ajax['ajax_navigation_type'] ) {
						$type = 'infinite';
					} elseif ( isset( $majpro_ajax['ajax_navigation_type'] ) && 'more' === $majpro_ajax['ajax_navigation_type'] ) {
						$type = 'more';
					} else {
						$type = 'infinite';
					}

					if ( isset( $majpro_ajax['ajax_finishtext'] ) && ! empty( $majpro_ajax['ajax_finishtext'] ) ) {
						/* option section default */
						$finishtext = esc_attr( $majpro_ajax['ajax_finishtext'] );
					} else {
						$finishtext = __( 'No More Posts Available.', 'majalahpro-core' );
					}

					if ( ! majalahpro_core_is_amp() ) {
						wp_enqueue_script( 'gmr-ajax-loadmore-lib', MAJALAHPRO_CORE_URL . 'js/jquery-ajax-loadmore.js', array( 'jquery' ), '1.0.0', true );
						wp_enqueue_script( 'gmr-ajax-loadmore', MAJALAHPRO_CORE_URL . 'js/ajax-loadmore.js', array( 'jquery', 'gmr-ajax-loadmore-lib' ), '1.0.0', true );
						wp_localize_script(
							'gmr-ajax-loadmore',
							'gmr_infiniteload',
							array(
								'navSelector'         => '.gmr-infinite-selector .page-numbers',
								'contentSelector'     => '#gmr-main-load',
								'nextSelector'        => '.gmr-infinite-selector .page-numbers .next',
								'itemSelector'        => '.item-infinite',
								'paginationType'      => $type,
								'loadingImage'        => MAJALAHPRO_CORE_URL . 'img/loader.gif',
								'loadingText'         => '',
								'loadingButtonLabel'  => __( 'Load More', 'majalahpro-core' ),
								'loadingButtonClass'  => '',
								'loadingFinishedText' => $finishtext,
							)
						);
					}
				}
			}

			wp_register_style( 'majalahpro-core', MAJALAHPRO_CORE_URL . 'css/majalahpro-core.css', '', '1.0.0' );
			wp_enqueue_style( 'majalahpro-core' );
		}

		/**
		 * Load languange
		 *
		 * @since 1.0.0
		 * @access public
		 */
		public function majalahpro_core_load_textdomain() {
			load_plugin_textdomain( 'majalahpro-core', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
			/**
			 * Class_exists on plugin working if using plugins_loaded filter
			 * Most view widget exist if wp postview plugin installed
			 */
			if ( class_exists( 'WP_Widget_PostViews' ) ) {
				include_once MAJALAHPRO_CORE_DIRNAME . 'widgets/mostview-posts-widget.php';
			}
		}
	}
}


if ( class_exists( 'Majalahpro_Core_Init' ) ) {
	/* Installation and uninstallation hooks */
	register_activation_hook( __FILE__, array( 'Majalahpro_Core_Init', 'majalahpro_core_activate' ) );
	register_deactivation_hook( __FILE__, array( 'Majalahpro_Core_Init', 'majalahpro_core_deactivate' ) );
	/* Initialise Class */
	$majalahpro_core_init_by_gianmr = new Majalahpro_Core_Init();

}
