<?php
/*
 Plugin Name: Apptivo Business Site
 Plugin URI: https://www.apptivo.com/integrations/wordpress-crm-plugin/
 Description: Apptivo Business Site plugin provides Testimonials, Jobs, Contact Forms, Cases and Newsletter sub plugins with <a href="https://www.apptivo.com" target="_blank">Apptivo CRM</a>.
 Version: 5.5
 Author: Apptivo
 Author URI: https://www.apptivo.com/
 License: GPLv2 or later
 License URI: https://www.gnu.org/licenses/gpl-2.0.html
 Text Domain: apptivo-business-site
 */

/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * @license GPL-2.0-or-later
 */

if (!session_id()) session_start();
if (!defined('AWP_PLUGIN_BASEPATH')) {
 	
 	if( is_admin() )
 	{
 		register_activation_hook( __FILE__, 'activate_apptivo_business' );
        register_activation_hook( __FILE__, 'awp_ip_jal_install' );
 	}
 	define('AWP_PLUGIN_BASEPATH',plugin_dir_path(__FILE__));
 	define('AWP_PLUGIN_BASEURL',plugins_url(basename( dirname(__FILE__))));
 	/**
     * Require plugin configuration
     */
    require_once dirname(__FILE__) . '/inc/define.php';
    require_once dirname(__FILE__) . '/inc/dummy-config.php';
    
    /**
     * Load plugins
     */
    awp_load_plugins();
    
    /**
     * Load Widgets
     */
    awp_load_widgets();

    /**
     * Run plugin
     */
	add_action( 'admin_enqueue_scripts', 'apptivo_business_enqueue' );

	function apptivo_business_enqueue($hook) {
		switch ($hook) {
				case 'apptivo_page_awp_news':
				case 'apptivo_page_awp_events':
				case 'apptivo_page_awp_testimonials':
				case 'apptivo_page_awp_jobs':
				case 'apptivo_page_awp_contactforms':
				case 'apptivo_page_awp_newsletter':
				case 'apptivo_page_awp_cases':	
					add_filter("mce_buttons", "remove_mce_buttons");
					//add_filter( 'wp_default_editor', create_function('', 'return "tinymce";') );
					add_filter( 'wp_default_editor', function() {return "tinymce";} );
					if ( is_admin() ) {
						add_action('admin_print_scripts', 'apptivo_business_scripts');
						add_action('admin_print_styles', 'apptivo_business_styles');					
					}
					break;  
			}
	}

	function isValueSet($val) {
		if(isset($val)) {
			return $val;
		} else {
			return "";
		}
	}

	/**
	 * Shortcode button in post editor
	 **/
	add_action( 'init', 'apptivo_businesssite_add_shortcode_button' );

	add_filter('widget_text', 'do_shortcode_apptivo');
	function do_shortcode_apptivo($content) {
		global $shortcode_tags;
		if (empty($shortcode_tags) || !is_array($shortcode_tags)) {
			return $content;
		}

		$pattern = get_shortcode_regex();    
		return preg_replace_callback( "/$pattern/s", 'do_shortcode_tag', $content );
	}

	function apptivo_businesssite_add_shortcode_button() {
		if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) return;
		if ( get_user_option('rich_editing') == 'true') :
			add_filter('mce_external_plugins', 'apptivo_businesssite_add_shortcode_tinymce_plugin');
			add_filter('mce_buttons', 'apptivo_businesssite_register_shortcode_button');
		endif;
	}

	function apptivo_businesssite_register_shortcode_button($buttons) {
		array_push($buttons, "|", "apptivo-businesssite_shortcodes_button");
		return $buttons;
	}

	function apptivo_businesssite_add_shortcode_tinymce_plugin($plugin_array) {
		$plugin_array['ApptivoBusinesssiteShortcodes'] = AWP_PLUGIN_BASEURL . '/assets/js/editor_plugin.js';
		return $plugin_array;
	}


	function remove_mce_buttons($buttons) {
	echo '<script type="text/javascript" language="javascript" >jQuery(document).ready(function(){	 
	jQuery("#editor-toolbar").remove(); 
	jQuery("#quicktags #ed_toolbar").remove(); 
	});</script>';
		unset($buttons[17]);
		array_push($buttons,'code');
		return $buttons;

	}
	require_once AWP_PLUGINS_DIR . '/messages.php'; 
    require_once AWP_PLUGINS_DIR . '/AWPMainController.php';
    
    $awp_maincontroller = new AWP_MainController(); 
    $awp_maincontroller->instance();
    /* $awp_maincontroller = & AWP_MainController::instance();*/
    $awp_maincontroller->run(); 
}

function activate_apptivo_business()
{   
     //Update plugin version.
	update_option( "apptivo_business_plugin_version", '1.2.4' );
    update_option( "apptivo_business_plugin_installed", 1 );
    awp_bsp_createcaptcha();
    awp_ensure_log_dir_protected();
}

global $awp_ip_jal_db_version;


function awp_ip_jal_install() {
	global $wpdb;
	$awp_ip_jal_db_version = '1.0';

	$table_name = $wpdb->prefix . 'absp_ipdeny';
	
	$charset_collate = '';

	if ( ! empty( $wpdb->charset ) ) {
	  $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
	}

	if ( ! empty( $wpdb->collate ) ) {
	  $charset_collate .= " COLLATE {$wpdb->collate}";
	}

	$sql = "CREATE TABLE $table_name (
		ID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
		ip_address VARCHAR(200),
		ip_type VARCHAR(200),
		cur_timestamp TIMESTAMP
            ) $charset_collate;";

	require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
	dbDelta( $sql );

	add_option( 'jal_db_version', $awp_ip_jal_db_version );
}

/**
 * Returns the plugin log directory path (outside the web-served plugin folder).
 *
 * @return string
 */
function awp_get_log_dir() {
	return trailingslashit(WP_CONTENT_DIR) . 'apptivo-business-site-logs';
}

/**
 * Creates the log directory and adds deny rules to block direct HTTP access.
 *
 * @param string|null $log_dir Optional log directory path.
 * @return string|false Log directory path on success, false on failure.
 */
function awp_ensure_log_dir_protected($log_dir = null) {
	if ($log_dir === null) {
		$log_dir = awp_get_log_dir();
	}

	if (!is_dir($log_dir) && !wp_mkdir_p($log_dir)) {
		return false;
	}

	$index_file = $log_dir . '/index.php';
	if (!file_exists($index_file)) {
		file_put_contents($index_file, "<?php\n// Silence is golden.\n");
	}

	$htaccess_file = $log_dir . '/.htaccess';
	if (!file_exists($htaccess_file)) {
		file_put_contents($htaccess_file, "Order deny,allow\nDeny from all\n");
	}

	return $log_dir;
}

/**
 * Redacts sensitive values from log context arrays.
 *
 * @param mixed $context Log context value.
 * @return mixed Redacted context.
 */
function awp_redact_log_context($context) {
	if (!is_array($context)) {
		return $context;
	}

	$sensitive_keys = array(
		'email', 'emailid', 'email_id',
		'phone', 'phonenumber', 'phone_number',
		'request_token', 'recaptcha_token',
		'firstname', 'lastname', 'fullname',
		'address', 'address1', 'address2', 'city', 'state', 'zipcode', 'zip',
		'comments', 'response', 'note', 'notetext',
	);

	$redacted = array();
	foreach ($context as $key => $value) {
		$key_lower = strtolower((string) $key);
		if (in_array($key_lower, $sensitive_keys, true)) {
			$redacted[$key] = '[REDACTED]';
		} elseif (is_array($value)) {
			$redacted[$key] = awp_redact_log_context($value);
		} else {
			$redacted[$key] = $value;
		}
	}

	return $redacted;
}

/**
 * Whether plugin debug logging is enabled.
 *
 * Logging is disabled in production by default and only runs when WP_DEBUG is true.
 *
 * @return bool
 */
function awp_is_debug_logging_enabled() {
	return defined('WP_DEBUG') && WP_DEBUG;
}

/**
 * Common logger used by the plugin to track request and submission flow.
 *
 * @param string $message Log message.
 * @param array  $context Optional structured context.
 * @param string $log_file Log file name.
 */
function awp_common_log($message, $context = array(), $log_file = 'general_business_plugin.log') {
	if (!awp_is_debug_logging_enabled()) {
		return;
	}

	$log_dir = awp_ensure_log_dir_protected();
	if ($log_dir === false) {
		return;
	}

	$log_path = trailingslashit($log_dir) . basename($log_file);
	$entry = array(
		'timestamp' => current_time('mysql'),
		'message'   => (string) $message,
		'context'   => awp_redact_log_context(is_array($context) ? $context : array($context)),
	);

	$line = wp_json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL;
	error_log($line, 3, $log_path);
}

function awp_bsp_createcaptcha() {
	// Load the WP_Filesystem if it's not already available
	if ( ! function_exists( 'WP_Filesystem' ) ) {
		require_once ABSPATH . 'wp-admin/includes/file.php';
	}

	global $wp_filesystem;

	// Initialize the filesystem
	WP_Filesystem();

	$upload = wp_upload_dir();
	$upload_dir = trailingslashit( $upload['basedir'] ) . 'awp_captcha';

	// Check if the directory exists
	if ( ! $wp_filesystem->is_dir( $upload_dir ) ) {
		// Create the directory with proper permissions
		$wp_filesystem->mkdir( $upload_dir, 0700 );
	}
}

add_action('admin_init', 'install_apptivo_business_redirect');
function install_apptivo_business_redirect() {
	global $pagenow;
	//if ( is_admin() && !empty(sanitize_text_field($_GET['activate'])) && (sanitize_text_field($_GET['activate']) == true) && $pagenow == 'plugins.php' & get_option('apptivo_business_plugin_installed') == 1) :
	if ( is_admin() && isset($_GET['activate']) && !empty($_GET['activate']) && (sanitize_text_field(wp_unslash($_GET['activate'])) == true) && $pagenow == 'plugins.php' && get_option('apptivo_business_plugin_installed') == 1){
		update_option( "apptivo_business_plugin_installed", 0 );
		// Redirect to general settings page.
		wp_redirect(admin_url('admin.php?page=awp_general'));
		exit;
	}
}
/*function apptivo_business_admin_scripts()
{
	$screen = get_current_screen();
	wp_register_script( 'apptivo_business_plugin', AWP_PLUGIN_BASEURL . '/assets/js/apptivo-business-plugin.js', array('jquery'), '1.0' );
	if (in_array( $screen->id, array( 'toplevel_page_awp_general','apptivo_page_awp_cases','apptivo_page_awp_jobs','apptivo_page_awp_events','apptivo_page_awp_news','apptivo_page_awp_ip_deny','apptivo_page_awp_testimonials','apptivo_page_awp_newsletter', 'apptivo_page_awp_contactforms'))) :
	  wp_enqueue_script( 'apptivo_business_plugin' );
	endif;
}*/
function apptivo_business_admin_scripts(){
	$screen = get_current_screen();
	wp_register_script('apptivo_business_plugin', AWP_PLUGIN_BASEURL . '/assets/js/apptivo-business-plugin.js', array('jquery'), '1.0', true);
	if (in_array($screen->id, array('toplevel_page_awp_general', 'apptivo_page_awp_cases', 'apptivo_page_awp_jobs', 'apptivo_page_awp_events', 'apptivo_page_awp_news', 'apptivo_page_awp_ip_deny', 'apptivo_page_awp_testimonials', 'apptivo_page_awp_newsletter', 'apptivo_page_awp_contactforms'))){
		wp_enqueue_script('apptivo_business_plugin');
	}

	// Add help tab for this screen if needed
	$screen->add_help_tab(array(
		'id'      => 'apptivo-business-help',
		'title'   => __('Apptivo Business Plugin Help', 'apptivo-business-site'),
		'content' => '<p>' . __('Help content goes here.', 'apptivo-business-site') . '</p>',
	));
}
add_action('admin_enqueue_scripts', 'apptivo_business_admin_scripts');

function apptivo_business_scripts() {
	wp_enqueue_script('media-upload');
	wp_enqueue_script('thickbox');	
}

function apptivo_business_styles() {
	echo '<style type="text/css">
	#wp-awp_contactform_confirmationmsg-editor-container .wp-editor-area{height:175px; width:100%;}
	#wp-awp_cases_confirmationmsg-editor-container .wp-editor-area{height:175px; width:100%;}
	#wp-awp_jobsform_confirmationmsg-editor-container .wp-editor-area{height:175px; width:100%;}
	#wp-awp_newsletterform_confirmation_msg-editor-container .wp-editor-area{height:175px; width:100%;}
	</style>';
	wp_enqueue_style('thickbox');
}


/**
 * Create auto Pages ( News, Events, Testimonials and Cobtactus ) Nedd to place theme's function.php [ do_action( 'absp_autopages'); ]
 *
 */
function absp_autopages()
{
	/*apptivo Plugin settings*/
    $general_plugins_settings=get_option("awp_plugins");

    if($general_plugins_settings == '' || empty($general_plugins_settings)) {
    	    $plugins = array('contactforms'=>true,'testimonials'=>true,'events'=> true,'news'=>true);
	    	update_option("awp_plugins", $plugins);
	}
	    
	/*Contact Forms*/
     $contact_form_options = get_option('awp_contactforms');
     if($contact_form_options == '' || empty($contact_form_options)) {
	 $config_contactforms = array('0'=>array('name'=>'contact_form',
                                 'properties'=>array('tmpltype'=>'awp_plugin_template','layout'=>'single-column-layout2.php','confirm_msg_page'=>'same','submit_button_type'=>'submit'),
                                 'fields'=>array('0'=>array('fieldid'=>'lastname','showtext'=>'Last Name','required'=>1,'type'=>'text','validation'=>'none','order'=>2),
                                                 '1'=>array('fieldid'=>'email','showtext'=>'Email','required'=>1,'type'=>'text','validation'=>'text','order'=>3))
                                 ));                                 
	 update_option("awp_contactforms", $config_contactforms);

    $contactus_pageid = get_option('awp_contactus_pageid');
	if($contactus_pageid == '' || empty($contactus_pageid)) 
	{
	$page_content = '[apptivocontactform name="contact_form"]';
	 $page_data = array(
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_author' => 1,
        'post_name' => 'contact',
        'post_title' => 'Contact',
        'post_content' => $page_content,
        'post_parent' => '',
        'comment_status' => 'closed'
    );
    $page_id = wp_insert_post($page_data);
    update_option("awp_contactus_pageid", $page_id);   
	}
	
	 }
	
	
	/*Testimonials*/
	 $testimonials_options = get_option('awp_testimonials_settings');
	  if($testimonials_options == '' || empty($testimonials_options)) {
	   $config_testimonials = array('template_type'=>'awp_plugin_template','template_layout'=>'default-testimonials.php','order'=>1,'itemsperpage'=>5);
	   update_option("awp_testimonials_settings", $config_testimonials);
	   
	  $testimonials_pageid = get_option('awp_testimonials_pageid');
	if($testimonials_pageid == '' || empty($testimonials_pageid)) 
	{ 
	 $page_content = '[apptivo_testimonials_fullview]';
	 $page_data = array(
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_author' => 1,
        'post_name' => 'testimonials',
        'post_title' => 'Testimonials',
        'post_content' => $page_content,
        'post_parent' => '',
        'comment_status' => 'closed'
    );
    $page_id = wp_insert_post($page_data);
    update_option("awp_testimonials_pageid", $page_id);      
	}
	
	
	  }
	
	/*News*/
   $news_options = get_option('awp_news_settings');
	  if($news_options == '' || empty($news_options)) {
	   $config_news =array('template_type'=>'awp_plugin_template','template_layout'=>'default-news.php','order'=>1,'itemsperpage'=>5);
	   update_option("awp_news_settings", $config_news);
	   
	  $news_pageid = get_option('awp_news_pageid');
	if($news_pageid == '' || empty($news_pageid)) 
	{ 
	 $page_content = '[apptivo_news_fullview]';
	 $page_data = array(
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_author' => 1,
        'post_name' => 'news',
        'post_title' => 'News',
        'post_content' => $page_content,
        'post_parent' => '',
        'comment_status' => 'closed'
    );
    $page_id = wp_insert_post($page_data);
    update_option("awp_news_pageid", $page_id);      
	}
	
	  }
	
 
	
	/*Events*/	
	$events_options = get_option('awp_events_settings');
	  if($events_options == '' || empty($events_options)) {
	   $config_events =array('template_type'=>'awp_plugin_template','template_layout'=>'default-events.php','order'=>1,'itemsperpage'=>5);
	   update_option("awp_events_settings", $config_events);
	   
	   $events_pageid = get_option('awp_events_pageid');
	if($events_pageid == '' || empty($events_pageid)) 
	{ 
	 $page_content = '[apptivo_events_fullview]';
	 $page_data = array(
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_author' => 1,
        'post_name' => 'events',
        'post_title' => 'Events',
        'post_content' => $page_content,
        'post_parent' => '',
        'comment_status' => 'closed'
    );
    $page_id = wp_insert_post($page_data);
    update_option("awp_events_pageid", $page_id);      
	}	
	
	  }
	
             
} 
add_action( 'absp_autopages', 'absp_autopages', 10, 2 );
//Powered By Apptivo.
add_action('wp_footer','powered_apptivo_status');
function powered_apptivo_status()
{
	$status = get_option('apptivo_poweredby_status');
	if( $status != '' && $status != 'dont_show') :	
	if($status == 'show_homepage') :
        if(is_front_page()) :	
  		  $apptivo_logo = poweredby_apptivo();
  		endif;
  	else:
  		  $apptivo_logo = poweredby_apptivo();
  	endif;	
  	echo '<div class="poweredbyapptivo" style="text-align:center;" >'.esc_attr($apptivo_logo).'</div>';
  	endif;
}
//Contact Form Submit.
add_action('wp_ajax_apptivo_business_contactus', 'apptivo_business_contactus_lead');
add_action('wp_ajax_nopriv_apptivo_business_contactus', 'apptivo_business_contactus_lead');
function apptivo_business_contactus_lead(){    
	if ( isset($_POST['awp_contact_frontend_form_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['awp_contact_frontend_form_nonce'])), 'awp_contact_frontend_form_action')) {
		$formname = isset($_POST['awp_contactformname']) ? sanitize_text_field(wp_unslash($_POST['awp_contactformname'])) : '';
	}
    $contact_form = new AWP_ContactForms();
    $contact_formlead = $contact_form->save_contact($formname,true);
    echo esc_attr($contact_formlead);exit;		
}


//Newsletter Form Submit.
add_action('wp_ajax_apptivo_business_newsletter', 'apptivo_business_newsletter_target');
add_action('wp_ajax_nopriv_apptivo_business_newsletter', 'apptivo_business_newsletter_target');
function apptivo_business_newsletter_target(){
    $formname = isset($_POST['awp_newsletterformname']) ? sanitize_text_field(wp_unslash($_POST['awp_newsletterformname'])) : '';
    $newsletter_form = new AWP_Newsletter();
    $newsletter_subscribe = $newsletter_form->save_newsletter($formname);
	echo esc_attr($newsletter_subscribe);exit;		
}

function abwpExternalScripts(){
	wp_enqueue_script("jquery");
	wp_enqueue_script( 'jquery_validation', AWP_PLUGIN_BASEURL . '/assets/js/validator-min.js', array("jquery"), '1.0.0', true );
}
?>
