<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin
 * @author     ShapedPlugin <support@shapedplugin.com>
 */

use SmartPostShow\Blocks\Helper;

/**
 * The admin-specific functionality of the plugin.
 */
class Smart_Post_Show_Pro_Admin {
	/**
	 * Script and style suffix
	 *
	 * @since 2.0.0
	 * @access protected
	 * @var string
	 */
	protected $suffix;
	/**
	 * Plugin name
	 *
	 * @since 2.0.0
	 * @access protected
	 * @var string
	 */
	protected $plugin_name;
	/**
	 * Plugin version
	 *
	 * @since 2.0.0
	 * @access protected
	 * @var string
	 */
	protected $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    2.0.0
	 * @param      string $plugin_name       The name of this plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->suffix      = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min';
		$this->plugin_name = $plugin_name;
		$this->version     = $version;

		// Autoload system.
		spl_autoload_register( array( $this, 'autoload' ) );
		add_action(
			'after_setup_theme',
			function () {

				// Get post types.
				$post_types = get_post_types( array( 'show_in_nav_menus' => true ) );

				SPSP_Metaboxes::preview_metabox( 'sp_pcp_display' );
				SPSP_Metaboxes::layout_metabox( 'sp_pcp_layouts' );
				SPSP_Metaboxes::option_metabox( 'sp_pcp_view_options' );
				SPSP_Metaboxes::shortcode_metabox( 'sp_pcp_display_shortcode' );
				SPSP_Settings::settings( 'sp_post_carousel_settings' );
				SPSP_ReplaceLayout::Replace_Layout( 'sp_post_carousel_rpl', $post_types );
				SPSP_Tools::tools( 'sp_post_carousel_tools' );
			}
		);

		$taxonomy_color_field = true;

		$block_options = get_option( 'sp-pcp-blocks-setting-options' );

		$module_options = array();

		if ( method_exists( Helper::class, 'get_modules_show_hide' ) ) {
			$module_options = Helper::get_modules_show_hide();
		}

		if ( is_array( $block_options ) ) {
			foreach ( $block_options as $option ) {
				if ( is_object( $option ) && 'taxonomy' === $option->block_name ) {
					$taxonomy_color_field = (bool) $option->show;
					break;
				}
			}
		}

		if ( is_array( $module_options ) && $taxonomy_color_field ) {
			$taxonomy_color_field = (bool) ( $module_options['taxonomy'] ?? $taxonomy_color_field );
		}

		if ( $taxonomy_color_field ) {
			// this is to bring thumbnail and color.
			add_action( 'rest_api_init', array( $this, 'register_category_thumbnail_rest' ) );

			// Show fields on Add Category form.
			add_action( 'category_add_form_fields', array( $this, 'add_category_thumbnail_and_color_picker' ) );

			// Show fields on Edit Category form.
			add_action( 'category_edit_form_fields', array( $this, 'edit_category_form' ) );

			// Save data on Add.
			add_action( 'created_category', array( $this, 'saved_category_thumbnail_color_on_add' ) );

			// Save data on Edit.
			add_action( 'edited_category', array( $this, 'saved_category_thumbnail_color_on_edit' ) );
		}

		// Enqueue media uploader & JS.
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_upload_js' ) );
		add_action( 'smart_post_show_weekly_scheduled_events', array( $this, 'init_smart_post_sdr' ) );

		// Plugin action link Button.
		$active_plugins = get_option( 'active_plugins' );
		foreach ( $active_plugins as $active_plugin ) {
			$_temp = strpos( $active_plugin, 'smart-post-show-pro.php' );
			if ( false !== $_temp ) {
				add_filter( 'plugin_action_links_' . $active_plugin, array( $this, 'add_plugin_action_links' ) );
			}
		}
	}

	/**
	 * Register category meta fields for REST API: thumbnail and color.
	 */
	public function register_category_thumbnail_rest() {
		register_rest_field(
			'category',
			'category_thumbnail',
			array(
				'get_callback'    => function ( $term ) {
					return get_term_meta( $term['id'], 'category_thumbnail', true );
				},
				'update_callback' => null,
				'schema'          => array(
					'description' => 'Category thumbnail URL',
					'type'        => 'string',
					'format'      => 'uri',
					'context'     => array( 'view', 'edit' ),
				),
			)
		);

		// Register category_color meta for REST API.
		register_rest_field(
			'category',
			'category_color',
			array(
				'get_callback'    => function ( $term ) {
					return get_term_meta( $term['id'], 'category_color', true );
				},
				'update_callback' => null,
				'schema'          => array(
					'description' => 'Category color hex code',
					'type'        => 'string',
					'context'     => array( 'view', 'edit' ),
				),
			)
		);
	}

	/**
	 * Adds custom fields to the category add form in the WordPress admin.
	 *
	 * This includes:
	 * - A media uploader for selecting a category thumbnail image
	 * - A color picker input for assigning a custom category color
	 *
	 * These fields are stored as term meta (category_thumbnail and category_color).
	 */
	public function add_category_thumbnail_and_color_picker() {
		?>
		<div class="form-field">
			<label for="category_thumbnail">Thumbnail</label>
			<input type="text" name="category_thumbnail" id="category_thumbnail" style="display:none;" />
			<button type="button" class="button category-media-upload" data-target="#category_thumbnail">Upload Image</button>
			<div class="thumbnail-preview">
				<img id="category_thumbnail_preview" src="" style="max-width:100px; display:none; margin-top:10px;" />
			</div>
		</div>
		<div class="form-field" style="margin-bottom: 16px; font-family: Arial, sans-serif;">
			<label
				for="category_color"
				style="display: block; margin-bottom: 6px; font-size: 14px;">
				Color
			</label>
			<input
				type="text"
				class="sp-custom-color-picker"
				name="category_color"
				id="category_color"
				style="width: 100px;
					height: 30px;
					border: 1px solid;
					cursor: pointer;
					background:	#f0f0f1;
					transition: transform 0.2s ease;" />
		</div>

		<?php
	}

	/**
	 * Adds custom fields to the category edit form in the WordPress admin.
	 *
	 * This function:
	 * - Retrieves and displays the saved category thumbnail
	 * - Retrieves and displays the saved category color
	 *
	 * @param WP_Term $term The term object being edited.
	 */
	public function edit_category_form( $term ) {
		$thumbnail = get_term_meta( $term->term_id, 'category_thumbnail', true );
		$color     = get_term_meta( $term->term_id, 'category_color', true );
		?>
		<tr class="form-field">
			<th scope="row"><label for="category_thumbnail">Thumbnail</label></th>
			<td>
				<input type="text" name="category_thumbnail" id="category_thumbnail" value="<?php echo esc_attr( $thumbnail ); ?>" style="display:none;" />
				<button type="button" class="button category-media-upload" data-target="#category_thumbnail">Upload Image</button>
				<div class="thumbnail-preview">
					<img id="category_thumbnail_preview" src="<?php echo esc_url( $thumbnail ); ?>"
						style="max-width:100px; <?php echo empty( $thumbnail ) ? 'display:none;' : ''; ?> margin-top:10px;" />
				</div>
			</td>
		</tr>
		<tr class="form-field">
			<th scope="row"><label for="category_color">Color</label></th>
			<td>
				<input type="text" name="category_color" class="sp-custom-color-picker" id="category_color" style="width: 100px;
					height: 30px;
					border: 1px solid;
					cursor: pointer;
					background:	#f0f0f1;
					transition: transform 0.2s ease;" value="<?php echo esc_attr( $color ); ?>" />
			</td>
		</tr>
		<?php
	}

	/**
	 * Add custom category meta (thumbnail and color) when a new category is added.
	 *
	 * @param int $term_id The ID of the newly created category.
	 */
	public function saved_category_thumbnail_color_on_add( $term_id ) {
		if ( isset( $_REQUEST['action'] ) && 'add-tag' === $_REQUEST['action'] ) {
			check_admin_referer( 'add-tag', '_wpnonce_add-tag' );

			if ( isset( $_POST['category_thumbnail'] ) ) {
				update_term_meta( $term_id, 'category_thumbnail', esc_url_raw( wp_unslash( $_POST['category_thumbnail'] ) ) );
			}
			if ( isset( $_POST['category_color'] ) ) {
				update_term_meta( $term_id, 'category_color', sanitize_hex_color( wp_unslash( $_POST['category_color'] ) ) );
			}
		}
	}

	/**
	 * Save custom category meta (thumbnail and color) when a new category is edited.
	 *
	 * @param int $term_id The ID of the edited category.
	 */
	public function saved_category_thumbnail_color_on_edit( $term_id ) {
		if (
			! isset( $_POST['_wpnonce'] ) ||
			! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-tag_' . $term_id )
		) {
			return;
		}

		if ( isset( $_POST['category_thumbnail'] ) ) {
			update_term_meta( $term_id, 'category_thumbnail', esc_url_raw( wp_unslash( $_POST['category_thumbnail'] ) ) );
		}
		if ( isset( $_POST['category_color'] ) ) {
			update_term_meta( $term_id, 'category_color', sanitize_hex_color( wp_unslash( $_POST['category_color'] ) ) );
		}
	}

	/**
	 * Add category thumbnail and color js.
	 *
	 * @param int $hook is used.
	 */
	public function enqueue_upload_js( $hook ) {

		if ( in_array( $hook, array( 'edit-tags.php', 'term.php' ), true ) && isset( $_GET['taxonomy'] ) && $_GET['taxonomy'] === 'category' ) {
			wp_enqueue_media();

			// Enqueue color picker assets.
			wp_enqueue_script( 'wp-color-picker' );
			wp_enqueue_style( 'wp-color-picker' );

			// Enqueue custom script with dependencies.
			wp_enqueue_script(
				'category-media-upload',
				plugin_dir_url( __FILE__ ) . 'js/category-thumbnail-upload.min.js',
				array( 'jquery', 'wp-color-picker' ), // Add wp-color-picker as dependency.
				null,
				true
			);
		}
	}

	/**
	 * Add plugin action menu
	 *
	 * @param array $links The action link.
	 *
	 * @return array
	 */
	public function add_plugin_action_links( $links ) {
		$manage_license     = new Smart_Post_Show_Pro_License( SMART_POST_SHOW_PRO_FILE, SP_PCP_VERSION, 'ShapedPlugin', SP_SPSP_STORE_URL, SP_SPSP_ITEM_ID, SP_PCP_PLUGIN_NAME );
		$license_key_status = $manage_license->get_license_status();
		$license_status     = ( is_object( $license_key_status ) ? $license_key_status->license : '' );

		if ( 'valid' === $license_status ) {
			$new_links = array(
				sprintf( '<a href="%s">%s</a>', admin_url( 'post-new.php?post_type=page&spblock_inserter=true' ), __( 'Add New', 'smart-post-show-pro' ) ),
				sprintf( '<a href="%s">%s</a>', admin_url( 'edit.php?post_type=sp_post_carousel&page=pcp_blocks#settings' ), __( 'Settings', 'smart-post-show-pro' ) ),
			);
		} else {
			$new_links = array(
				sprintf( '<a style="color: red; font-weight: 600;" href="%s">%s</a>', admin_url( 'edit.php?post_type=sp_post_carousel&page=pcp_blocks#settings=license-key' ), __( 'Activate license', 'smart-post-show-pro' ) ),
			);
		}

		return array_merge( $new_links, $links );
	}
	/**
	 *  Add plugin row meta link
	 *
	 * @param [array] $plugin_meta Add plugin row meta link.
	 * @param [url]   $file plugin row meta link.
	 * @return array
	 */
	public function after_pcp_row_meta( $plugin_meta, $file ) {
		if ( SP_PCP_BASENAME === $file ) {
			$plugin_meta[] = '<a href="https://wpsmartpost.com/blocks/" target="_blank">' . __( 'Live Demo', 'smart-post-show-pro' ) . '</a>';
			$plugin_meta[] = '<a href="https://wpsmartpost.com/docs/" target="_blank">' . __( 'Documentation', 'smart-post-show-pro' ) . '</a>';
			$plugin_meta[] = '<a href="https://shapedplugin.com/create-new-ticket/" target="_blank">' . __( 'Support', 'smart-post-show-pro' ) . '</a>';
		}
		return $plugin_meta;
	}
	/**
	 * Autoload class files on demand
	 *
	 * @param string $class requested class name.
	 * @since 2.0.0
	 */
	private function autoload( $class ) {
		$name = explode( '_', $class );
		if ( isset( $name[1] ) ) {
			$class_name       = strtolower( $name[1] );
			$pcp_config_paths = array( 'views/', 'views/configs/settings/', 'views/configs/generator/' );
			foreach ( $pcp_config_paths as $sp_ppc_path ) {
				$filename = plugin_dir_path( __FILE__ ) . '/' . $sp_ppc_path . 'class-spsp-' . $class_name . '.php';
				if ( file_exists( $filename ) ) {
					require_once $filename;
				}
			}
		}
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    2.0.0
	 */
	public function enqueue_styles() {

		$current_screen        = get_current_screen();
		$the_current_post_type = $current_screen->post_type;
		$the_current_base      = $current_screen->base;
		if ( 'sp_post_carousel' === $the_current_post_type ) {
			wp_enqueue_style( 'pcp-font-awesome' );
			wp_enqueue_style( 'pcp_swiper' );
			wp_enqueue_style( 'pcp-bxslider' );
			wp_enqueue_style( 'pcp-likes' );
			wp_enqueue_style( 'pcp-popup' );
			wp_enqueue_style( 'pcp_fonttello_icon' );

			wp_enqueue_style( 'pcp-style' );
			// wp_enqueue_style( 'smart-post-show-pro-admin', SP_PCP_URL . 'admin/css/smart-post-show-pro-admin.min.css', array(), SP_PCP_VERSION, 'all' );
		}
		if ( 'sp_smart_post_font' === $the_current_post_type ) {
			wp_enqueue_style( 'smart-post-show-custom-font-meta', SP_PCP_URL . 'admin/css/smart-post-show-custom-font-meta.min.css', array(), SP_PCP_VERSION, 'all' );
		}
		if ( 'sp_post_carousel_page_pcp_blocks' === $the_current_base || 'sp_post_carousel' === $the_current_post_type ) {
			wp_enqueue_style( 'smart-post-show-pro-admin', SP_PCP_URL . 'admin/css/smart-post-show-pro-admin.min.css', array(), SP_PCP_VERSION, 'all' );
			wp_enqueue_style( 'pcp_social_icons_style' ); // For React Dashboard tabs.
		}
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    2.0.0
	 */
	public function enqueue_scripts() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Smart_Post_Show_Pro_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Smart_Post_Show_Pro_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$current_screen        = get_current_screen();
		$the_current_post_type = $current_screen->post_type;
		$the_current_post_id   = $current_screen->id;
		if ( 'post' === $the_current_post_type ) {
			// Video upload in meta field.
			wp_enqueue_script(
				'sp_image_and_video_upload',
				plugin_dir_url( __FILE__ ) . 'js/image_gallery_and_video_upload.min.js',
				array(),
				null,
				true
			);
		}
		if ( 'sp_post_carousel' === $the_current_post_type ) {
			wp_enqueue_script( SP_PCP_PLUGIN_NAME, SP_PCP_URL . 'admin/js/smart-post-show-pro-admin.min.js', array( 'jquery' ), SP_PCP_VERSION, true );
			wp_enqueue_script( 'pcp_swiper' );
			wp_enqueue_script( 'pcp_isotope' );
			wp_enqueue_script( 'pcp_bxslider' );
			wp_enqueue_script( 'pcp_popup' );
		}
		if ( 'sp_post_carousel' === $the_current_post_type || 'sp_post_carousel_page_pcp_blocks' === $the_current_post_id ) {
			add_filter( 'admin_footer_text', array( $this, 'sp_custom_admin_footer_text' ) );

			add_filter( 'update_footer', array( $this, 'sp_custom_admin_footer_version_text' ), 99999 );
		}
		if ( 'sp_smart_post_font' === $the_current_post_type ) {
			wp_enqueue_media();
			wp_enqueue_script( 'smart-post-show-custom-font-meta', SP_PCP_URL . 'admin/js/smart-post-show-custom-font-meta.min.js', array( 'jquery' ), SP_PCP_VERSION, true );
		}
	}

	/**
	 * Add carousel admin columns.
	 *
	 * @since 2.0.0
	 * @return statement
	 */
	public function filter_carousel_admin_column() {
		$admin_columns['cb']         = '<input type="checkbox" />';
		$admin_columns['title']      = __( 'Title', 'smart-post-show-pro' );
		$admin_columns['shortcode']  = __( 'Shortcode', 'smart-post-show-pro' );
		$admin_columns['pcp_layout'] = __( 'Layout', 'smart-post-show-pro' );
		$admin_columns['date']       = __( 'Date', 'smart-post-show-pro' );

		return $admin_columns;
	}

	/**
	 * Display admin columns for the carousels.
	 *
	 * @param mix    $column The columns.
	 * @param string $post_id The post ID.
	 * @return void
	 */
	public function display_carousel_admin_fields( $column, $post_id ) {
		$pcp_layouts     = get_post_meta( $post_id, 'sp_pcp_layouts', true );
		$carousels_types = isset( $pcp_layouts['pcp_layout_preset'] ) ? $pcp_layouts['pcp_layout_preset'] : '';
		$allowed_html    = array(
			'input' => array(
				'class'    => array(),
				'style'    => array(),
				'type'     => array(),
				'onclick'  => array(), // Note: onClick is usually stripped for security; consider a JS listener instead.
				'readonly' => array(),
				'value'    => array(),
			),
			'div'   => array(
				'class' => array(),
			),
			'i'     => array(
				'class' => array(),
			),
		);
		switch ( $column ) {
			case 'shortcode':
				$column_field = '<input  class="sp_pcp_input" style="width: 230px;padding: 4px 8px;cursor: pointer;" type="text" onClick="this.select();" readonly="readonly" value="[smart_post_show id=&quot;' . $post_id . '&quot;]"/> <div class="pcp-after-copy-text"><i class="fa fa-check-circle"></i> Shortcode Copied to Clipboard! </div>';
				echo wp_kses( $column_field, $allowed_html );
				break;
			case 'pcp_layout':
				$layout = ucwords( str_replace( '_layout', ' ', $carousels_types ) );
				echo esc_html( $layout );
				break;
		} // end switch.
	}

	/**
	 *  Sp_post_carousel post type Save and update alert in Admin Dashboard created by Smart Post Show Pro
	 *
	 * @param array $messages alert messages.
	 */
	public function sppcp_update( $messages ) {
		global $post, $post_ID;
		$messages['sp_post_carousel'][1] = __( 'Shortcode Updated', 'smart-post-show-pro' );
		$messages['sp_post_carousel'][6] = __( 'Shortcode Published', 'smart-post-show-pro' );
		return $messages;
	}

	/**
	 * Redirect after active.
	 *
	 * @param mixed $plugin plugin.
	 * @return void
	 */
	public function redirect_to( $plugin ) {
		$manage_license          = new Smart_Post_Show_Pro_License( SMART_POST_SHOW_PRO_FILE, SP_PCP_VERSION, 'ShapedPlugin', SP_SPSP_STORE_URL, SP_SPSP_ITEM_ID, SP_PCP_PLUGIN_NAME );
		$license_key_status      = $manage_license->get_license_status();
		$license_status          = ( is_object( $license_key_status ) ? $license_key_status->license : '' );
		$is_visited_setup_wizard = get_option( 'sp_smart_post_setup_wizard_visited', false );

		if ( SP_PCP_BASENAME === $plugin && ! $is_visited_setup_wizard ) {
			exit( wp_redirect( admin_url( 'edit.php?post_type=sp_post_carousel&page=pcp_blocks#setupwizard' ) ) ); // phpcs:ignore
		} elseif ( SP_PCP_BASENAME === $plugin && $is_visited_setup_wizard ) {
			exit( wp_redirect( admin_url( 'edit.php?post_type=sp_post_carousel&page=pcp_blocks' ) ) ); // phpcs:ignore
		}
	}

	/**
	 * Function creates smart post show shortcode duplicate as a draft.
	 */
	public function duplicate_smart_post_shortcode() {
		global $wpdb;
		if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'sp_duplicate_smart_post' === $_REQUEST['action'] ) ) ) {
			wp_die( 'No smart post show to duplicate has been supplied!' );
		}

		/**
		 * Nonce verification
		 */
		if ( ! isset( $_GET['sp_duplicate_smart_post_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['sp_duplicate_smart_post_nonce'] ) ), basename( __FILE__ ) ) ) {
			return;
		}

		/**
		 * Get the original shortcode id
		 */
		$post_id    = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
		$capability = apply_filters( 'pcp_user_role_permission', 'manage_options' );
		$show_ui    = current_user_can( $capability ) ? true : false;

		if ( ! $show_ui && get_post_type( $post_id ) !== 'sp_post_carousel' ) {
			wp_die( esc_html__( 'No shortcode to duplicate has been supplied!', 'smart-post-show-pro' ) );
		}
		/**
		 * And all the original shortcode data then
		 */
		$post = get_post( $post_id );

		$current_user    = wp_get_current_user();
		$new_post_author = $current_user->ID;

		/**
		 * If shortcode data exists, create the shortcode duplicate
		 */
		if ( isset( $post ) && null !== $post ) {

			/**
			 * New shortcode data array
			 */
			$args = array(
				'comment_status' => $post->comment_status,
				'ping_status'    => $post->ping_status,
				'post_author'    => $new_post_author,
				'post_content'   => $post->post_content,
				'post_excerpt'   => $post->post_excerpt,
				'post_name'      => $post->post_name,
				'post_parent'    => $post->post_parent,
				'post_password'  => $post->post_password,
				'post_status'    => 'draft',
				'post_title'     => $post->post_title,
				'post_type'      => $post->post_type,
				'to_ping'        => $post->to_ping,
				'menu_order'     => $post->menu_order,
			);

			/**
			 * Insert the shortcode by wp_insert_post() function
			 */
			$new_post_id = wp_insert_post( $args );

			/**
			 * Get all current post terms ad set them to the new post draft
			 */
			$taxonomies = get_object_taxonomies( $post->post_type ); // returns array of taxonomy names for post type, ex array("category", "post_tag");.
			foreach ( $taxonomies as $taxonomy ) {
				$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
				wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
			}

			/**
			 * Duplicate all post meta just in two SQL queries
			 */
			$post_meta_infos = get_post_custom( $post_id );
			// Duplicate all post meta.
			foreach ( $post_meta_infos as $key => $values ) {
				foreach ( $values as $value ) {
					$value = wp_slash( maybe_unserialize( $value ) ); // Unserialize data to avoid conflicts.
					add_post_meta( $new_post_id, $key, $value );
				}
			}
			/**
			 * Finally, redirect to the edit post screen for the new draft
			 */
			wp_safe_redirect( admin_url( 'edit.php?post_type=' . $post->post_type ) );

			exit;
		} else {
			wp_die( esc_html__( 'Smart Post Show creation failed, could not find original posts: ', 'smart-post-show-pro' ) . esc_attr( $post_id ) );
		}
	}

	/**
	 * Add the duplicate link to action list for post_row_actions.
	 *
	 * @param  mixed $actions action.
	 * @param  mixed $post post type.
	 * @return url
	 */
	public function sp_duplicate_smart_post_link( $actions, $post ) {
		$capability = apply_filters( 'pcp_user_role_permission', 'manage_options' );
		$show_ui    = current_user_can( $capability ) ? true : false;

		if ( $show_ui && 'sp_post_carousel' === $post->post_type ) {
			$actions['duplicate'] = '<a href="' . wp_nonce_url( 'admin.php?action=sp_duplicate_smart_post&post=' . $post->ID, basename( __FILE__ ), 'sp_duplicate_smart_post_nonce' ) . '" rel="permalink">' . __( 'Duplicate', 'smart-post-show-pro' ) . '</a>';
		}
		return $actions;
	}

	/**
	 * Create a placeholder image in the media library.
	 *
	 * @since 3.5.0
	 */
	public function create_placeholder_img() {

		if ( ! ini_get( 'allow_url_fopen' ) ) {
			return;
		}

		if ( ! apply_filters( 'sps_placeholder_image_upload', true ) ) {
			return;
		}

		$placeholder_image = get_option( 'sps_placeholder_image', 0 );

		// Validate current setting if set. If set, return.
		if ( ! empty( $placeholder_image ) ) {
			if ( is_numeric( $placeholder_image ) && wp_attachment_is_image( $placeholder_image ) ) {
				return;
			}
		}

		// Prepare upload directory and placeholder image URL.
		$upload_dir      = wp_upload_dir();
		$placeholder_img = SP_PCP_URL . 'public/assets/img/placeholder.png';
		$placeholder_img = apply_filters( 'pcp_no_thumb_placeholder', $placeholder_img );
		$filename        = trailingslashit( $upload_dir['basedir'] ) . 'sps-placeholder.png';

		// Download the image if it doesn't already exist.
		if ( ! file_exists( $filename ) ) {
			$response = wp_remote_get(
				$placeholder_img,
				array(
					'timeout'   => 20,
					'sslverify' => true,
				)
			);

			// Handle errors during download.
			if ( is_wp_error( $response ) ) {
				update_option( 'sps_placeholder_image', 0 );
				return;
			}

			// Check for valid HTTP response.
			$response_code = wp_remote_retrieve_response_code( $response );
			if ( 200 !== $response_code ) {
				update_option( 'sps_placeholder_image', 0 );
				return;
			}

			// Save the downloaded file to the uploads directory.
			$image_data = wp_remote_retrieve_body( $response );
			if ( false === file_put_contents( $filename, $image_data ) ) {
				update_option( 'sps_placeholder_image', 0 );
				return;
			}
		}

		// Validate that the file was successfully saved.
		if ( ! file_exists( $filename ) ) {
			update_option( 'sps_placeholder_image', 0 );
			return;
		}

		// Get file type and prepare attachment data.
		$filetype   = wp_check_filetype( basename( $filename ), null );
		$attachment = array(
			'guid'           => trailingslashit( $upload_dir['url'] ) . basename( $filename ),
			'post_mime_type' => $filetype['type'],
			'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
			'post_content'   => '',
			'post_status'    => 'inherit',
		);

		// Insert the attachment into the WordPress media library.
		$attach_id = wp_insert_attachment( $attachment, $filename );
		if ( is_wp_error( $attach_id ) ) {
			update_option( 'sps_placeholder_image', 0 );
			return;
		}

		// Update the placeholder image option with the attachment ID.
		update_option( 'sps_placeholder_image', $attach_id );

		// Include required WordPress file for metadata generation.
		require_once ABSPATH . 'wp-admin/includes/image.php';

		// Generate attachment metadata and update the database record.
		$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
		wp_update_attachment_metadata( $attach_id, $attach_data );
	}

	/**
	 * Footer text function
	 *
	 * @return string
	 */
	public function sp_custom_admin_footer_text() {
		return '<div class="sp-smart-post-admin-footer-text">Enjoying Smart Post? Kindly rate us <span>★★★★★</span> on <a target="_blank" href="' . esc_url( 'https://wordpress.org/support/plugin/post-carousel/reviews/' ) . '">WordPress.org</a>. on WordPress.org. Your feedback helps spread the word. Thanks! 😊</div>';
	}

	/**
	 * Footer Version text function
	 *
	 * @return string
	 */
	public function sp_custom_admin_footer_version_text() {
		return '<div class="sp-smart-post-admin-footer-version-text" style="text-align:right;margin-top:-18px;font-weight:600;font-size:14px;color:#6C6A6D;">Smart Post: ' . SP_PCP_VERSION . '</div>';
	}
}
