*/
namespace RankMathPro\Admin;
use RankMath\Helper;
use RankMath\Traits\Hooker;
use RankMath\Admin\Admin_Helper;
use RankMathPro\Admin\Admin_Helper as ProAdminHelper;
use MyThemeShop\Helpers\Param;
use MyThemeShop\Helpers\Arr;
use MyThemeShop\Helpers\Conditional;
defined( 'ABSPATH' ) || exit;
/**
 * Quick edit class.
 *
 * @codeCoverageIgnore
 */
class Quick_Edit {
	use Hooker;
	/**
	 * Register hooks.
	 */
	public function __construct() {
		$this->action( 'admin_enqueue_scripts', 'admin_scripts', 20 );
		$this->action( 'rank_math/post/column/seo_details', 'quick_edit_hidden_fields' );
		$this->action( 'quick_edit_custom_box', 'quick_edit' );
		$this->action( 'bulk_edit_custom_box', 'bulk_edit' );
		$this->action( 'save_post', 'save_post' );
		$this->action( 'load-edit.php', 'maybe_save_bulk_edit', 20 );
		$taxonomies = Helper::get_accessible_taxonomies();
		unset( $taxonomies['post_format'] );
		$taxonomies = wp_list_pluck( $taxonomies, 'label', 'name' );
		foreach ( $taxonomies as $taxonomy => $label ) {
			$this->filter( "manage_edit-{$taxonomy}_columns", 'add_tax_seo_column' );
			$this->filter( "manage_{$taxonomy}_custom_column", 'tax_seo_column_content', 10, 3 );
			$this->filter( "edited_{$taxonomy}", 'save_tax' );
		}
	}
	/**
	 * Add hidden column for SEO details for the quick edit.
	 *
	 * @param string[] $columns Original columns array.
	 *
	 * @return string[]         New columns array.
	 */
	public function add_tax_seo_column( $columns ) {
		$columns['rank_math_tax_seo_details'] = __( 'SEO Details', 'rank-math-pro' );
		return $columns;
	}
	/**
	 * Add the hidden fields in the SEO Details column of the terms listing screen.
	 *
	 * @param  string $string      Current content.
	 * @param  string $column_name Column name.
	 * @param  int    $term_id     Term ID.
	 *
	 * @return string              New content.
	 */
	public function tax_seo_column_content( $string, $column_name, $term_id ) {
		if ( 'rank_math_tax_seo_details' !== $column_name ) {
			return $string;
		}
		ob_start();
		$this->quick_edit_hidden_fields( $term_id, 'term' );
		return ob_get_clean();
	}
	/**
	 * Output hidden fields in the `seo_details` column on the posts and the
	 * terms screen, to use the data in the quick edit form.
	 *
	 * @param  int    $object_id   Post/term ID.
	 * @param  object $object_type Object type: post or term.
	 *
	 * @return void
	 */
	public function quick_edit_hidden_fields( $object_id, $object_type = 'post' ) {
		if ( ! in_array( $object_type, [ 'post', 'term' ], true ) ) {
			return;
		}
		if ( 'post' === $object_type && ! $this->can_bulk_edit() ) {
			return;
		}
		$robots = array_filter( (array) get_metadata( $object_type, $object_id, 'rank_math_robots', true ) );
		if ( empty( $robots ) ) {
			$robots = Helper::get_robots_defaults();
		}
		// Maybe product with hidden visibility!
		if ( Conditional::is_woocommerce_active() && 'product' === get_post_type( $object_id ) && Helper::get_settings( 'general.noindex_hidden_products' ) ) {
			$product = \wc_get_product( $object_id );
			if ( $product && $product->get_catalog_visibility() === 'hidden' ) {
				// Preserve other robots values.
				$robots = array_filter(
					$robots,
					function ( $robot ) {
						return 'index' !== $robot;
					}
				);
				$robots = array_merge( $robots, [ 'noindex' ] );
			}
		}
		$title = get_metadata( $object_type, $object_id, 'rank_math_title', true );
		if ( ! $title ) {
			if ( 'post' === $object_type ) {
				$post_type = get_post_type( $object_id );
				$title     = Helper::get_settings( "titles.pt_{$post_type}_title" );
			} elseif ( 'term' === $object_type ) {
				$term     = get_term( $object_id );
				$taxonomy = $term->taxonomy;
				$title    = Helper::get_settings( "titles.tax_{$taxonomy}_title" );
			}
		}
		$description = get_metadata( $object_type, $object_id, 'rank_math_description', true );
		if ( ! $description ) {
			if ( 'post' === $object_type ) {
				$post_type   = get_post_type( $object_id );
				$description = Helper::get_settings( "titles.pt_{$post_type}_description" );
			} elseif ( 'term' === $object_type ) {
				$term        = get_term( $object_id );
				$taxonomy    = $term->taxonomy;
				$description = Helper::get_settings( "titles.tax_{$taxonomy}_description" );
			}
		}
		$canonical       = get_metadata( $object_type, $object_id, 'rank_math_canonical_url', true );
		$focus_keywords  = Arr::from_string( get_metadata( $object_type, $object_id, 'rank_math_focus_keyword', true ) );
		$primary_keyword = ! empty( $focus_keywords ) ? $focus_keywords[0] : '';
		$canonical_placeholder = '';
		if ( 'post' === $object_type ) {
			$canonical_placeholder = get_permalink( $object_id );
		} elseif ( 'term' === $object_type ) {
			$canonical_placeholder = get_term_link( $object_id );
		}
		?>
		
		
		
		
		
		
		
			
		
		can_bulk_edit() ) {
			return;
		}
		global $post_type;
		$ptype = $post_type;
		if ( is_a( $ptype, 'WP_Post_Type' ) ) {
			$ptype = $ptype->name;
		}
		global $pagenow;
		if ( 'edit-tags.php' === $pagenow ) {
			global $taxonomy;
			$ptype = $taxonomy;
		}
		$columns = get_column_headers( 'edit-' . $ptype );
		if ( ! isset( $columns['rank_math_seo_details'] ) && ! isset( $columns['rank_math_tax_seo_details'] ) ) {
			return;
		}
		$robots = [
			'index'        => __( 'Index', 'rank-math-pro' ),
			'noindex'      => __( 'No Index', 'rank-math-pro' ),
			'nofollow'     => __( 'No Follow', 'rank-math-pro' ),
			'noarchive'    => __( 'No Archive', 'rank-math-pro' ),
			'noimageindex' => __( 'No Image Index', 'rank-math-pro' ),
			'nosnippet'    => __( 'No Snippet', 'rank-math-pro' ),
		];
		switch ( $column ) {
			case 'rank_math_seo_details':
				wp_nonce_field( 'rank-math-quick-edit', 'rank_math_quick_edit_nonce' );
				?>
				
					
				
				
				 
				
				
				quick_edit( $column, true );
	}
	/**
	 * Save bulk edit data if needed.
	 *
	 * @return void
	 */
	public function maybe_save_bulk_edit() {
		if ( ! Param::request( 'bulk_edit' ) ) {
			return;
		}
		$this->save_bulk_edit( $_REQUEST ); // phpcs:ignore
	}
	/**
	 * Save bulk edit data.
	 *
	 * @param array $post_data Post data input.
	 * @return void
	 */
	public function save_bulk_edit( $post_data ) {
		if ( empty( $post_data ) ) {
			$post_data = &$_POST; // phpcs:ignore
		}
		if ( isset( $post_data['post_type'] ) ) {
			$ptype = get_post_type_object( $post_data['post_type'] );
		} else {
			$ptype = get_post_type_object( 'post' );
		}
		if ( ! current_user_can( $ptype->cap->edit_posts ) ) {
			return;
		}
		if ( ! Helper::has_cap( 'onpage_general' ) ) {
			return;
		}
		if ( ! $this->can_bulk_edit( $ptype ) ) {
			return;
		}
		$save_fields = [
			'title',
			'description',
			'robots',
			'primary_term',
		];
		$post_ids = array_map( 'intval', (array) $post_data['post'] );
		foreach ( $post_ids as $post_id ) {
			foreach ( $save_fields as $field ) {
				$field_name  = 'rank_math_' . $field;
				$field_value = isset( $post_data[ $field_name ] ) ? $post_data[ $field_name ] : '';
				if ( is_string( $field_value ) ) {
					$field_value = trim( $field_value );
				}
				if ( empty( $field_value ) ) {
					// Skip if not set.
					continue;
				}
				if ( 'robots' === $field ) {
					$field_value = (array) $field_value;
				} elseif ( 'primary_term' === $field ) {
					$taxonomy   = ProAdminHelper::get_primary_taxonomy( $post_id );
					$field_name = 'rank_math_primary_' . $taxonomy['name'];
				}
				update_post_meta( $post_id, $field_name, $field_value );
			}
		}
	}
	/**
	 * Save post quick edit.
	 *
	 * @param  int $post_id Post ID.
	 * @return mixed
	 */
	public function save_post( $post_id ) {
		if ( wp_is_post_revision( $post_id ) || ! wp_verify_nonce( Param::post( 'rank_math_quick_edit_nonce' ), 'rank-math-quick-edit' ) ) {
			return;
		}
		if ( ! Helper::has_cap( 'onpage_general' ) ) {
			return;
		}
		$post_type = get_post_type( $post_id );
		if ( ! $this->can_bulk_edit( $post_type ) ) {
			return;
		}
		$taxonomy = ProAdminHelper::get_primary_taxonomy( $post_id );
		$save_fields = [
			'title',
			'description',
			'robots',
			'focus_keyword',
			'canonical_url',
			'primary_term',
		];
		foreach ( $save_fields as $field ) {
			$field_name = 'rank_math_' . $field;
			$flag       = [];
			if ( 'robots' === $field ) {
				$flag = FILTER_REQUIRE_ARRAY;
			}
			$field_value = Param::post( $field_name, false, FILTER_DEFAULT, $flag );
			if ( false === $field_value ) {
				continue;
			}
			$default_value = '';
			if ( $post_type ) {
				$default_value = Helper::get_settings( 'titles.pt_' . $post_type . '_' . $field );
			}
			if ( 'robots' === $field ) {
				$field_value = array_filter( $field_value );
				$field_value = array_unique( $field_value );
				$field_value = array_intersect( $field_value, [ 'index', 'noindex', 'nofollow', 'noarchive', 'noimageindex', 'nosnippet' ] );
			} elseif ( 'canonical_url' === $field ) {
				$field_value = esc_url_raw( $field_value );
			} elseif ( 'focus_keyword' === $field ) {
				$current_value = get_post_meta( $post_id, $field_name, true );
				$current       = Arr::from_string( $current_value );
				$keywords      = Arr::from_string( $field_value );
				$current[0]    = ! empty( $keywords ) ? $keywords[0] : '';
				if ( '' === $current[0] ) {
					array_shift( $current );
				}
				$field_value = join( ', ', $current );
			} elseif ( 'primary_term' === $field ) {
				if ( ! $field_value ) {
					delete_post_meta( $post_id, $field_name );
					continue;
				}
				if ( ! has_term( absint( $field_value ), $taxonomy['name'], $post_id ) ) {
					continue;
				}
				$field_name = 'rank_math_primary_' . $taxonomy['name'];
			}
			if ( empty( $field_value ) || $field_value === $default_value ) {
				delete_post_meta( $post_id, $field_name );
				continue;
			}
			update_post_meta( $post_id, $field_name, $field_value );
		}
	}
	/**
	 * Save taxonomy term quick edit.
	 *
	 * @param  int $term_id Term ID.
	 *
	 * @return void
	 */
	public function save_tax( $term_id ) {
		$term_id = Param::post( 'tax_ID' );
		if ( ! $term_id ) {
			return;
		}
		if ( ! wp_verify_nonce( Param::post( 'rank_math_quick_edit_nonce' ), 'rank-math-quick-edit' ) ) {
			return;
		}
		if ( ! Helper::has_cap( 'onpage_general' ) ) {
			return;
		}
		$save_fields = [
			'title',
			'description',
			'robots',
			'focus_keyword',
			'canonical_url',
		];
		foreach ( $save_fields as $field ) {
			$field_name = 'rank_math_' . $field;
			$flag       = [];
			if ( 'robots' === $field ) {
				$flag = FILTER_REQUIRE_ARRAY;
			}
			$field_value = Param::post( $field_name, false, FILTER_DEFAULT, $flag );
			if ( false === $field_value ) {
				continue;
			}
			if ( 'robots' === $field ) {
				$field_value = array_filter( $field_value );
				$field_value = array_unique( $field_value );
				$field_value = array_intersect( $field_value, [ 'index', 'noindex', 'nofollow', 'noarchive', 'noimageindex', 'nosnippet' ] );
			} elseif ( 'canonical_url' === $field ) {
				$field_value = esc_url_raw( $field_value );
			} elseif ( 'focus_keyword' === $field ) {
				$current    = get_term_meta( $term_id, $field_name, true );
				$current    = Arr::from_string( $current );
				$keywords   = Arr::from_string( $field_value );
				$current[0] = ! empty( $keywords ) ? $keywords[0] : '';
				if ( '' === $current[0] ) {
					array_shift( $current );
				}
				$field_value = join( ', ', $current );
			}
			update_term_meta( $term_id, $field_name, $field_value );
		}
	}
	/**
	 * Check if bulk editing is enabled for the current post type.
	 *
	 * @param  string $ptype Post type name.
	 *
	 * @return boolean
	 */
	public function can_bulk_edit( $ptype = null ) {
		global $post_type;
		if ( ! $ptype ) {
			$ptype = $post_type;
		}
		if ( is_a( $ptype, 'WP_Post_Type' ) ) {
			$ptype = $ptype->name;
		}
		$allow_editing = Helper::get_settings( 'titles.pt_' . $ptype . '_bulk_editing', true );
		if ( ! $allow_editing || 'readonly' === $allow_editing ) {
			return false;
		}
		return true;
	}
}