<?php
/**
 * ACCP Data Management
 *
 * Data management functionality.
 *
 * @package    ARS_CONSTELLATION_CLIENT_PORTAL
 */

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

/**
 * ACCP Pro Settings
 */
class ARS_Constellation_Client_Portal_Data_Management {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * Plugin utility functions.
	 *
	 * @access   private
	 * @var      object   $utilities    Plugin utility functions class.
	 */
	private $utilities;

	/**
	 * Plugin admin functions.
	 *
	 * @access   private
	 * @var      object   $accp_admin    Plugin admin functions class.
	 */
	private $accp_admin;

	/**
	 * Core settings.
	 *
	 * @access   private
	 * @var      object   $core_settings    Plugin core settings class.
	 */
	private $core_settings;

	/**
	 * Client pages.
	 *
	 * @access   private
	 * @var      object   $client_pages    Client pages class.
	 */
	private $client_pages;

	/**
	 * Client files.
	 *
	 * @access   private
	 * @var      object   $client_files    Client files class.
	 */
	private $client_files;

	/**
	 * Client invoices.
	 *
	 * @access   private
	 * @var      object   $client_files    Client invoices class.
	 */
	private $client_invoices;

	/**
	 * Users.
	 *
	 * @access   private
	 * @var      object   $accp_users    Client users class.
	 */
	private $accp_users;

	/**
	 * The class construct.
	 */
	public function __construct() {

		$this->plugin_name     = ACCP_PLUGIN_NAME;
		$this->utilities       = new ACCP_Utility_Functions();
		$this->accp_admin      = new ARS_Constellation_Client_Portal_Admin( ACCP_PLUGIN_NAME, ACCP_PLUGIN_VERSION );
		$this->core_settings   = new ARS_Constellation_Client_Portal_Settings();
		$this->client_pages    = new ARS_Constellation_Client_Portal_Client_Pages( ACCP_PLUGIN_NAME, ACCP_PLUGIN_VERSION );
		$this->client_files    = new ARS_Constellation_Client_Portal_Client_File( ACCP_PLUGIN_NAME, ACCP_PLUGIN_VERSION );
		$this->client_invoices = new ARS_Constellation_Client_Portal_Client_Invoice( ACCP_PLUGIN_NAME, ACCP_PLUGIN_VERSION );
		$this->accp_users      = new ARS_Constellation_Client_Portal_Users( ACCP_PLUGIN_NAME, ACCP_PLUGIN_VERSION );
	}

	/**
	 * Delete post attachment on client file delete.
	 *
	 * @param int $post_id - The ID of the post.
	 */
	public function delete_post_attachment_on_file_post_delete( $post_id ) {

		if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) || ! $post_id ) {
			return;
		}

		$post_type = get_post_type( $post_id );

		if ( 'accp_clientfile' !== $post_type ) {
			return;
		}

		/**
		 * Set a $should_delete var and allow it
		 * to be filtered.
		 *
		 * Used by the pro data management settings.
		 */
		$should_delete_default = true;
		$should_delete         = apply_filters( 'accp_should_delete_attachment_on_file_post_delete', $should_delete_default, $post_id );

		if ( true !== $should_delete ) {
			return;
		}

		/**
		 * Delete the file attachment.
		 */
		$this->delete_client_post_attachment( $post_id );
	}


	/**
	 * Delete post attachment on client invoice delete.
	 *
	 * @param int $post_id - The ID of the post.
	 */
	public function delete_post_attachment_on_invoice_post_delete( $post_id ) {

		if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) || ! $post_id ) {
			return;
		}

		$post_type = get_post_type( $post_id );

		if ( 'accp_clientinvoice' !== $post_type ) {
			return;
		}

		/**
		 * Set a $should_delete var and allow it
		 * to be filtered.
		 *
		 * Used by the pro data management settings.
		 */
		$should_delete_default = true;
		$should_delete         = apply_filters( 'accp_should_delete_attachment_on_invoice_post_delete', $should_delete_default, $post_id );

		if ( true !== $should_delete ) {
			return;
		}

		/**
		 * Delete the file attachment.
		 */
		$this->delete_client_post_attachment( $post_id );
	}


	/**
	 * Delete Constellation post attachment.
	 *
	 * @param int $post_id - The ID of the target post.
	 */
	public function delete_client_post_attachment( $post_id ) {

		if ( ! is_admin() || ! is_user_logged_in() || ! current_user_can( 'manage_options' ) || ! $post_id ) {
			return;
		}

		$post_type = get_post_type( $post_id );

		if ( ! $post_type ) {
			return;
		}

		$allowed_post_types = array(
			'accp_clientfile',
			'accp_clientinvoice',
			'accp_global_file',
		);

		if ( ! in_array( $post_type, $allowed_post_types, true ) ) {
			return;
		}

		$accp_file = get_post_meta( $post_id, 'accp_file', true );

		if ( ! $accp_file || empty( $accp_file ) || ! is_array( $accp_file ) ) {
			return;
		}

		if ( ! array_key_exists( 'url', $accp_file ) || empty( $accp_file['url'] ) ) {
			return;
		}

		$file_parse_array = wp_parse_url( $accp_file['url'] );

		if ( ! is_array( $file_parse_array ) || empty( $file_parse_array ) ) {
			return;
		}

		if ( ! array_key_exists( 'path', $file_parse_array ) || empty( $file_parse_array['path'] ) ) {
			return;
		}

		if ( ! get_home_path() || empty( get_home_path() ) ) {
			return;
		}

		$accp_file_full_path = untrailingslashit( get_home_path() ) . $file_parse_array['path'];

		/**
		 * Delete the file.
		 */
		wp_delete_file( $accp_file_full_path );
	}
} // END ARS_Constellation_Client_Portal_Data_Management.
