<?php
/**
 * Metaboxes
 *
 * @package Metaboxes
 */

defined( 'ABSPATH' ) || exit( 'Please don&rsquo;t call the plugin directly. Thanks :)' );

/**
 * Display metabox in Custom Post Type
 */
function seopress_display_date_snippet() {
	if ( seopress_get_service( 'TitleOption' )->getSingleCptDate() === '1' ) {
		return '<div class="snippet-date">' . get_the_modified_date( 'M j, Y' ) . ' - </div>';
	}
}

/**
 * Primary category select
 *
 * @param bool $echo Echo the HTML.
 * @param bool $with_description Display the description.
 *
 * @return string HTML.
 */
function seopress_primary_category_select( $echo = true, $with_description = true ) {
	global $post;
	global $typenow;
	$seopress_robots_primary_cat = get_post_meta( $post->ID, '_seopress_robots_primary_cat', true );

	$cats    = 'product' == $typenow && seopress_get_service( 'WooCommerceActivate' )->isActive() ? get_the_terms( $post, 'product_cat' ) : get_categories();
	$cats    = apply_filters( 'seopress_primary_category_list', $cats );
	$options = '';

	if ( ! empty( $cats ) ) {
		$options .= sprintf(
			'<option value="none" %s>%s</option>',
			selected( 'none', $seopress_robots_primary_cat, false ),
			__( 'None (will disable this feature)', 'wp-seopress' )
		);
		foreach ( $cats as $category ) {
			$options .= sprintf(
				'<option value="%s" %s>%s</option>',
				(int) $category->term_id,
				selected( $category->term_id, $seopress_robots_primary_cat, false ),
				esc_html( $category->name )
			);
		}
	}

	$html        = '';
	$description = sprintf(
		/* translators: category permalink structure */
		__( 'Set the category that gets used in the %s permalink and in our breadcrumbs if you have multiple categories.', 'wp-seopress' ),
		'<code>%category%</code>'
	);
	if ( ! empty( $options ) ) {
		$html = sprintf(
			'<p>
				<label for="seopress_robots_primary_cat" style="display:block; margin-bottom:8px;">%s</label>
				%s
				<br><select id="seopress_robots_primary_cat" name="seopress_robots_primary_cat">%s</select>
			</p>',
			__( 'Select a primary category', 'wp-seopress' ),
			$with_description ? sprintf( '<span class="description">%s</span>', $description ) : '',
			$options
		);
	}

	if ( $echo ) {
		echo $html;
	}
	return $html;
}

/**
 * Register `_seopress_robots_primary_cat` as a REST-exposed post meta.
 *
 * Required so the Gutenberg primary category sidebar bundle can read and
 * write the value through `select('core/editor').getEditedPostAttribute('meta')`
 * and `dispatch('core/editor').editPost({ meta: ... })`. The same meta key
 * is consumed by `seopress_titles_primary_cat_hook()` for `%category%`
 * permalinks, by the React universal metabox's Advanced tab and by the
 * Rank Math / Yoast / SEO Framework migrators, so we keep that single
 * key as the source of truth.
 *
 * @since 9.8.x
 *
 * @return void
 */
function seopress_register_primary_category_post_meta() {
	$post_types = array( 'post', 'product' );

	foreach ( $post_types as $post_type ) {
		if ( ! post_type_exists( $post_type ) ) {
			continue;
		}
		register_post_meta(
			$post_type,
			'_seopress_robots_primary_cat',
			array(
				'show_in_rest'  => true,
				'single'        => true,
				'type'          => 'string',
				'default'       => '',
				'auth_callback' => function ( $allowed, $meta_key, $object_id ) {
					return current_user_can( 'edit_post', $object_id );
				},
			)
		);
	}
}
add_action( 'init', 'seopress_register_primary_category_post_meta', 11 );

/**
 * Inject the primary category dropdown into the WordPress Categories panel.
 *
 * Restores the inline UI removed alongside the legacy classic SEO metabox
 * in 9.8: in the Block Editor the Gutenberg bundle hooks into the
 * Categories sidebar panel via REST-exposed post meta; in the Classic
 * Editor a small script appends the rendered `<select>` markup (plus a
 * dedicated nonce) into the standard Categories metabox so the value
 * ships with the post form. The data layer (post meta
 * `_seopress_robots_primary_cat`) is unchanged and the React universal
 * SEO metabox keeps its own primary category field as the consolidated
 * power-user surface.
 *
 * @since 9.8.x
 *
 * @return void
 */
function seopress_enqueue_primary_category_injector() {
	global $pagenow, $typenow, $post, $wp_version;

	if ( ! in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) ) {
		return;
	}

	if ( ! in_array( $typenow, array( 'post', 'product' ), true ) ) {
		return;
	}

	if ( ! function_exists( 'get_current_screen' ) ) {
		return;
	}

	$screen          = get_current_screen();
	$is_block_editor = $screen && method_exists( $screen, 'is_block_editor' ) && true === $screen->is_block_editor();

	if ( $is_block_editor ) {
		if ( version_compare( $wp_version, '5.8', '<' ) ) {
			return;
		}
		wp_enqueue_script(
			'seopress-primary-category',
			SEOPRESS_URL_PUBLIC . '/editor/primary-category-select/index.js',
			array( 'wp-hooks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-data' ),
			SEOPRESS_VERSION,
			true
		);
		return;
	}

	$primary_category_id = $post ? get_post_meta( $post->ID, '_seopress_robots_primary_cat', true ) : '';

	wp_enqueue_script(
		'seopress-primary-category-classic',
		SEOPRESS_ASSETS_DIR . '/js/seopress-primary-category-classic.js',
		array(),
		SEOPRESS_VERSION,
		true
	);

	$nonce_field = wp_nonce_field( 'seopress_save_primary_category', 'seopress_primary_category_nonce', true, false );

	wp_localize_script(
		'seopress-primary-category-classic',
		'seopressPrimaryCategorySelectData',
		array(
			'selectHTML'      => seopress_primary_category_select( false, false ),
			'nonceField'      => $nonce_field,
			'primaryCategory' => $primary_category_id,
		)
	);
}
add_action( 'admin_enqueue_scripts', 'seopress_enqueue_primary_category_injector' );

/**
 * Save the primary category submitted from the Categories metabox in the
 * Classic Editor. The dedicated nonce + capability check makes this
 * handler self-contained, independent of the legacy SEO metabox save
 * pipeline (which only runs when the universal metabox is suppressed).
 *
 * @since 9.8.x
 *
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 * @return void
 */
function seopress_save_primary_category_from_categories_metabox( $post_id, $post ) {
	if ( ! isset( $_POST['seopress_primary_category_nonce'] ) ) {
		return;
	}
	if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['seopress_primary_category_nonce'] ) ), 'seopress_save_primary_category' ) ) {
		return;
	}
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}
	if ( ! in_array( $post->post_type, array( 'post', 'product' ), true ) ) {
		return;
	}
	if ( ! isset( $_POST['seopress_robots_primary_cat'] ) ) {
		return;
	}

	$value = sanitize_text_field( wp_unslash( $_POST['seopress_robots_primary_cat'] ) );
	if ( '' === $value || 'none' === $value ) {
		delete_post_meta( $post_id, '_seopress_robots_primary_cat' );
	} else {
		update_post_meta( $post_id, '_seopress_robots_primary_cat', $value );
	}
}
add_action( 'save_post', 'seopress_save_primary_category_from_categories_metabox', 10, 2 );

/**
 * Metaboxes init
 *
 * @return array Data attributes.
 */
function seopress_metaboxes_init() {
	global $typenow;
	global $pagenow;

	$data_attr             = array();
	$data_attr['data_tax'] = '';
	$data_attr['termId']   = '';

	if ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) {
		$data_attr['current_id'] = get_the_id();
		$data_attr['origin']     = 'post';
		$data_attr['title']      = get_the_title( $data_attr['current_id'] );
	} elseif ( 'term.php' === $pagenow || 'edit-tags.php' === $pagenow ) {
		global $tag;
		$data_attr['current_id'] = $tag->term_id;
		$data_attr['termId']     = $tag->term_id;
		$data_attr['origin']     = 'term';
		$data_attr['data_tax']   = $tag->taxonomy;
		$data_attr['title']      = $tag->name;
	}

	$data_attr['isHomeId'] = get_option( 'page_on_front' );
	if ( '0' === $data_attr['isHomeId'] ) {
		$data_attr['isHomeId'] = '';
	}

	return $data_attr;
}

/**
 * Display SEO metaboxe
 */
function seopress_display_seo_metaboxe() {
	add_action( 'add_meta_boxes', 'seopress_init_metabox' );

	/**
	 * Init metaboxe
	 */
	function seopress_init_metabox() {
		$seopress_get_post_types = seopress_get_service( 'WordPressData' )->getPostTypes();

		$seopress_get_post_types = apply_filters( 'seopress_metaboxe_seo', $seopress_get_post_types );

		if ( ! empty( $seopress_get_post_types ) && ! seopress_get_service( 'EnqueueModuleMetabox' )->canEnqueue() ) {
			foreach ( $seopress_get_post_types as $key => $value ) {
				add_meta_box( 'seopress_cpt', __( 'SEO', 'wp-seopress' ), 'seopress_cpt', $key, 'normal', 'default' );
			}
		}
		add_meta_box( 'seopress_cpt', __( 'SEO', 'wp-seopress' ), 'seopress_cpt', 'seopress_404', 'normal', 'default' );
	}

	/**
	 * Display SEO metaboxe
	 *
	 * @param object $post Post object.
	 */
	function seopress_cpt( $post ) {
		$prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
		wp_nonce_field( plugin_basename( __FILE__ ), 'seopress_cpt_nonce' );

		// init.
		$disabled = array();

		wp_enqueue_script( 'seopress-cpt-tabs', SEOPRESS_ASSETS_DIR . '/js/seopress-metabox' . $prefix . '.js', [ 'jquery-ui-tabs' ], SEOPRESS_VERSION, true );

		// Note: Classic metabox is now only used for seopress_404 post type.
		// Universal metabox handles all other post types.

		$seopress_titles_title = get_post_meta( $post->ID, '_seopress_titles_title', true );
		$seopress_titles_desc  = get_post_meta( $post->ID, '_seopress_titles_desc', true );

		$disabled['robots_index'] = '';
		if ( seopress_get_service( 'TitleOption' )->getSingleCptNoIndex() || seopress_get_service( 'TitleOption' )->getTitleNoIndex() || true === post_password_required( $post->ID ) ) {
			$seopress_robots_index    = 'yes';
			$disabled['robots_index'] = 'disabled';
		} else {
			$seopress_robots_index = get_post_meta( $post->ID, '_seopress_robots_index', true );
		}

		$disabled['robots_follow'] = '';
		if ( seopress_get_service( 'TitleOption' )->getSingleCptNoFollow() || seopress_get_service( 'TitleOption' )->getTitleNoFollow() ) {
			$seopress_robots_follow    = 'yes';
			$disabled['robots_follow'] = 'disabled';
		} else {
			$seopress_robots_follow = get_post_meta( $post->ID, '_seopress_robots_follow', true );
		}

		$disabled['snippet'] = '';
		if ( seopress_get_service( 'TitleOption' )->getTitleNoSnippet() ) {
			$seopress_robots_snippet = 'yes';
			$disabled['snippet']     = 'disabled';
		} else {
			$seopress_robots_snippet = get_post_meta( $post->ID, '_seopress_robots_snippet', true );
		}

		$disabled['imageindex'] = '';
		if ( seopress_get_service( 'TitleOption' )->getTitleNoImageIndex() ) {
			$seopress_robots_imageindex = 'yes';
			$disabled['imageindex']     = 'disabled';
		} else {
			$seopress_robots_imageindex = get_post_meta( $post->ID, '_seopress_robots_imageindex', true );
		}

		$seopress_robots_canonical                 = get_post_meta( $post->ID, '_seopress_robots_canonical', true );
		$seopress_robots_primary_cat               = get_post_meta( $post->ID, '_seopress_robots_primary_cat', true );
		$seopress_robots_freeze_modified_date      = get_post_meta( $post->ID, '_seopress_robots_freeze_modified_date', true );
		if ( '' === $seopress_robots_freeze_modified_date && '1' === seopress_get_service( 'AdvancedOption' )->getAppearanceFreezeModifiedDate() ) {
			$seopress_robots_freeze_modified_date = 'yes';
		}
		$seopress_robots_custom_modified_date      = get_post_meta( $post->ID, '_seopress_robots_custom_modified_date', true );
		$seopress_social_fb_title                  = get_post_meta( $post->ID, '_seopress_social_fb_title', true );
		$seopress_social_fb_desc                   = get_post_meta( $post->ID, '_seopress_social_fb_desc', true );
		$seopress_social_fb_img                    = get_post_meta( $post->ID, '_seopress_social_fb_img', true );
		$seopress_social_fb_img_attachment_id      = get_post_meta( $post->ID, '_seopress_social_fb_img_attachment_id', true );
		$seopress_social_fb_img_width              = get_post_meta( $post->ID, '_seopress_social_fb_img_width', true );
		$seopress_social_fb_img_height             = get_post_meta( $post->ID, '_seopress_social_fb_img_height', true );
		$seopress_social_twitter_title             = get_post_meta( $post->ID, '_seopress_social_twitter_title', true );
		$seopress_social_twitter_desc              = get_post_meta( $post->ID, '_seopress_social_twitter_desc', true );
		$seopress_social_twitter_img               = get_post_meta( $post->ID, '_seopress_social_twitter_img', true );
		$seopress_social_twitter_img_attachment_id = get_post_meta( $post->ID, '_seopress_social_twitter_img_attachment_id', true );
		$seopress_social_twitter_img_width         = get_post_meta( $post->ID, '_seopress_social_twitter_img_width', true );
		$seopress_social_twitter_img_height        = get_post_meta( $post->ID, '_seopress_social_twitter_img_height', true );
		$seopress_redirections_enabled             = get_post_meta( $post->ID, '_seopress_redirections_enabled', true );
		$seopress_redirections_enabled_regex       = get_post_meta( $post->ID, '_seopress_redirections_enabled_regex', true );
		$seopress_redirections_logged_status       = get_post_meta( $post->ID, '_seopress_redirections_logged_status', true );
		$seopress_redirections_type                = get_post_meta( $post->ID, '_seopress_redirections_type', true );
		$seopress_redirections_value               = get_post_meta( $post->ID, '_seopress_redirections_value', true );
		$seopress_redirections_param               = get_post_meta( $post->ID, '_seopress_redirections_param', true );

		require_once dirname( __DIR__ ) . '/admin-dyn-variables-helper.php'; // Dynamic variables.

		require_once __DIR__ . '/admin-metaboxes-form.php'; // Metaboxe HTML.

		do_action( 'seopress_seo_metabox_init' );
	}

	add_action( 'save_post', 'seopress_save_metabox', 10, 2 );
	/**
	 * Save metabox
	 *
	 * @param int $post_id Post ID.
	 * @param object $post Post object.
	 */
	function seopress_save_metabox( $post_id, $post ) {
		// Nonce.
		if ( ! isset( $_POST['seopress_cpt_nonce'] ) || ! wp_verify_nonce( $_POST['seopress_cpt_nonce'], plugin_basename( __FILE__ ) ) ) {
			return $post_id;
		}

		// Post type object.
		$post_type = get_post_type_object( $post->post_type );

		// Check permission.
		if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
			return $post_id;
		}

		if ( 'attachment' !== get_post_type( $post_id ) ) {
			$seo_tabs = isset( $_POST['seo_tabs'] ) ? json_decode( stripslashes( htmlspecialchars_decode( $_POST['seo_tabs'] ) ), true ) : array();

			if ( json_last_error() !== JSON_ERROR_NONE ) {
				return $post_id;
			}

			if ( in_array( 'title-tab', $seo_tabs, true ) ) {
				if ( ! empty( $_POST['seopress_titles_title'] ) ) {
					update_post_meta( $post_id, '_seopress_titles_title', sanitize_text_field( $_POST['seopress_titles_title'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_titles_title' );
				}
				if ( ! empty( $_POST['seopress_titles_desc'] ) ) {
					update_post_meta( $post_id, '_seopress_titles_desc', sanitize_textarea_field( $_POST['seopress_titles_desc'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_titles_desc' );
				}
			}
			if ( in_array( 'advanced-tab', $seo_tabs, true ) ) {
				if ( isset( $_POST['seopress_robots_index'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_index', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_index' );
				}
				if ( isset( $_POST['seopress_robots_follow'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_follow', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_follow' );
				}
				if ( isset( $_POST['seopress_robots_imageindex'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_imageindex', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_imageindex' );
				}
				if ( isset( $_POST['seopress_robots_snippet'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_snippet', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_snippet' );
				}
				if ( ! empty( $_POST['seopress_robots_canonical'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_canonical', sanitize_url( $_POST['seopress_robots_canonical'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_canonical' );
				}
				if ( ! empty( $_POST['seopress_robots_primary_cat'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_primary_cat', sanitize_text_field( $_POST['seopress_robots_primary_cat'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_primary_cat' );
				}
				if ( isset( $_POST['seopress_robots_freeze_modified_date'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_freeze_modified_date', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_freeze_modified_date' );
				}
				if ( ! empty( $_POST['seopress_robots_custom_modified_date'] ) ) {
					update_post_meta( $post_id, '_seopress_robots_custom_modified_date', sanitize_text_field( wp_unslash( $_POST['seopress_robots_custom_modified_date'] ) ) );
				} else {
					delete_post_meta( $post_id, '_seopress_robots_custom_modified_date' );
				}
			}
			if ( in_array( 'social-tab', $seo_tabs, true ) ) {
				// Facebook.
				if ( ! empty( $_POST['seopress_social_fb_title'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_title', sanitize_text_field( $_POST['seopress_social_fb_title'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_title' );
				}
				if ( ! empty( $_POST['seopress_social_fb_desc'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_desc', sanitize_textarea_field( $_POST['seopress_social_fb_desc'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_desc' );
				}
				if ( ! empty( $_POST['seopress_social_fb_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_img', sanitize_url( $_POST['seopress_social_fb_img'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_img' );
				}
				if ( ! empty( $_POST['seopress_social_fb_img_attachment_id'] ) && ! empty( $_POST['seopress_social_fb_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_img_attachment_id', sanitize_text_field( $_POST['seopress_social_fb_img_attachment_id'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_img_attachment_id' );
				}
				if ( ! empty( $_POST['seopress_social_fb_img_width'] ) && ! empty( $_POST['seopress_social_fb_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_img_width', sanitize_text_field( $_POST['seopress_social_fb_img_width'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_img_width' );
				}
				if ( ! empty( $_POST['seopress_social_fb_img_height'] ) && ! empty( $_POST['seopress_social_fb_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_fb_img_height', sanitize_text_field( $_POST['seopress_social_fb_img_height'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_fb_img_height' );
				}

				// Twitter.
				if ( ! empty( $_POST['seopress_social_twitter_title'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_title', sanitize_text_field( $_POST['seopress_social_twitter_title'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_title' );
				}
				if ( ! empty( $_POST['seopress_social_twitter_desc'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_desc', sanitize_textarea_field( $_POST['seopress_social_twitter_desc'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_desc' );
				}
				if ( ! empty( $_POST['seopress_social_twitter_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_img', sanitize_url( $_POST['seopress_social_twitter_img'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_img' );
				}
				if ( ! empty( $_POST['seopress_social_twitter_img_attachment_id'] ) && ! empty( $_POST['seopress_social_twitter_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_img_attachment_id', sanitize_text_field( $_POST['seopress_social_twitter_img_attachment_id'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_img_attachment_id' );
				}
				if ( ! empty( $_POST['seopress_social_twitter_img_width'] ) && ! empty( $_POST['seopress_social_twitter_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_img_width', sanitize_text_field( $_POST['seopress_social_twitter_img_width'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_img_width' );
				}
				if ( ! empty( $_POST['seopress_social_twitter_img_height'] ) && ! empty( $_POST['seopress_social_twitter_img'] ) ) {
					update_post_meta( $post_id, '_seopress_social_twitter_img_height', sanitize_text_field( $_POST['seopress_social_twitter_img_height'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_social_twitter_img_height' );
				}
			}
			if ( in_array( 'redirect-tab', $seo_tabs, true ) ) {
				if ( isset( $_POST['seopress_redirections_type'] ) ) {
					$redirection_type = intval( $_POST['seopress_redirections_type'] );

					if ( in_array( $redirection_type, array( 301, 302, 307, 410, 451 ), true ) ) {
						update_post_meta( $post_id, '_seopress_redirections_type', $redirection_type );
					} else {
						delete_post_meta( $post_id, '_seopress_redirections_type' );
					}
				}
				if ( ! empty( $_POST['seopress_redirections_value'] ) ) {
					update_post_meta( $post_id, '_seopress_redirections_value', sanitize_url( $_POST['seopress_redirections_value'] ) );
				} else {
					delete_post_meta( $post_id, '_seopress_redirections_value' );
				}
				if ( isset( $_POST['seopress_redirections_enabled'] ) ) {
					update_post_meta( $post_id, '_seopress_redirections_enabled', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_redirections_enabled', '' );
				}
				if ( isset( $_POST['seopress_redirections_enabled_regex'] ) ) {
					update_post_meta( $post_id, '_seopress_redirections_enabled_regex', 'yes' );
				} else {
					delete_post_meta( $post_id, '_seopress_redirections_enabled_regex' );
				}
				if ( isset( $_POST['seopress_redirections_logged_status'] ) ) {
					$logged_status = sanitize_text_field( $_POST['seopress_redirections_logged_status'] );

					$allowed_options = array( 'both', 'only_logged_in', 'only_not_logged_in' );

					if ( in_array( $logged_status, $allowed_options, true ) ) {
						update_post_meta( $post_id, '_seopress_redirections_logged_status', $logged_status );
					} else {
						delete_post_meta( $post_id, '_seopress_redirections_logged_status' );
					}
				}
				if ( isset( $_POST['seopress_redirections_param'] ) ) {
					$redirections_param = sanitize_text_field( $_POST['seopress_redirections_param'] );

					$allowed_options = array( 'exact_match', 'without_param', 'with_ignored_param' );

					if ( in_array( $redirections_param, $allowed_options, true ) ) {
						update_post_meta( $post_id, '_seopress_redirections_param', $redirections_param );
					} else {
						delete_post_meta( $post_id, '_seopress_redirections_param' );
					}
				}
			}
			do_action( 'seopress_seo_metabox_save', $post_id, $seo_tabs );
		}
	}
}

/**
 * Display CA metaboxe
 */
function seopress_display_ca_metaboxe() {
	add_action( 'add_meta_boxes', 'seopress_init_ca_metabox' );

	/**
	 * Init CA metabox
	 */
	function seopress_init_ca_metabox() {
		$seopress_get_post_types = seopress_get_service( 'WordPressData' )->getPostTypes();

		$seopress_get_post_types = apply_filters( 'seopress_metaboxe_content_analysis', $seopress_get_post_types );

		if ( ! empty( $seopress_get_post_types ) && ! seopress_get_service( 'EnqueueModuleMetabox' )->canEnqueue() ) {
			foreach ( $seopress_get_post_types as $key => $value ) {
				add_meta_box( 'seopress_content_analysis', __( 'Content analysis', 'wp-seopress' ), 'seopress_content_analysis', $key, 'normal', 'default' );
			}
		}
	}

	/**
	 * Display CA metaboxe
	 *
	 * @param object $post Post object.
	 */
	function seopress_content_analysis( $post ) {
		$prefix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
		wp_nonce_field( plugin_basename( __FILE__ ), 'seopress_content_analysis_nonce' );

		// Tagify.
		wp_enqueue_script( 'seopress-tagify', SEOPRESS_ASSETS_DIR . '/js/tagify' . $prefix . '.js', array( 'jquery' ), SEOPRESS_VERSION, true );
		wp_register_style( 'seopress-tagify', SEOPRESS_ASSETS_DIR . '/css/tagify' . $prefix . '.css', array(), SEOPRESS_VERSION );
		wp_enqueue_style( 'seopress-tagify' );

		wp_enqueue_script(
			'seopress-cpt-counters',
			SEOPRESS_ASSETS_DIR . '/js/seopress-counters' . $prefix . '.js',
			array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-accordion', 'jquery-ui-autocomplete' ),
			SEOPRESS_VERSION,
			array(
				'strategy'  => 'defer',
				'in_footer' => true,
			)
		);
		$seopress_real_preview = array(
			'seopress_nonce'               => wp_create_nonce( 'seopress_real_preview_nonce' ),
			'seopress_real_preview'        => admin_url( 'admin-ajax.php' ),
			'i18n'                         => array( 'progress' => __( 'Analysis in progress...', 'wp-seopress' ) ),
			'ajax_url'                     => admin_url( 'admin-ajax.php' ),
			'get_preview_meta_title'       => wp_create_nonce( 'get_preview_meta_title' ),
			'get_preview_meta_description' => wp_create_nonce( 'get_preview_meta_description' ),
		);
		wp_localize_script( 'seopress-cpt-counters', 'seopressAjaxRealPreview', $seopress_real_preview );

		$seopress_inspect_url = array(
			'seopress_nonce'       => wp_create_nonce( 'seopress_inspect_url_nonce' ),
			'seopress_inspect_url' => admin_url( 'admin-ajax.php' ),
		);
		wp_localize_script( 'seopress-cpt-counters', 'seopressAjaxInspectUrl', $seopress_inspect_url );

		$seopress_analysis_target_kw = get_post_meta( $post->ID, '_seopress_analysis_target_kw', true );
		$seopress_analysis_data      = get_post_meta( $post->ID, '_seopress_analysis_data', true );
		$seopress_titles_title       = get_post_meta( $post->ID, '_seopress_titles_title', true );
		$seopress_titles_desc        = get_post_meta( $post->ID, '_seopress_titles_desc', true );

		if ( seopress_get_service( 'TitleOption' )->getSingleCptNoIndex() || seopress_get_service( 'TitleOption' )->getTitleNoIndex() || true === post_password_required( $post->ID ) ) {
			$seopress_robots_index = 'yes';
		} else {
			$seopress_robots_index = get_post_meta( $post->ID, '_seopress_robots_index', true );
		}

		if ( seopress_get_service( 'TitleOption' )->getSingleCptNoFollow() || seopress_get_service( 'TitleOption' )->getTitleNoFollow() ) {
			$seopress_robots_follow = 'yes';
		} else {
			$seopress_robots_follow = get_post_meta( $post->ID, '_seopress_robots_follow', true );
		}

		if ( seopress_get_service( 'TitleOption' )->getTitleNoSnippet() ) {
			$seopress_robots_snippet = 'yes';
		} else {
			$seopress_robots_snippet = get_post_meta( $post->ID, '_seopress_robots_snippet', true );
		}

		if ( seopress_get_service( 'TitleOption' )->getTitleNoImageIndex() ) {
			$seopress_robots_imageindex = 'yes';
		} else {
			$seopress_robots_imageindex = get_post_meta( $post->ID, '_seopress_robots_imageindex', true );
		}

		require_once __DIR__ . '/admin-metaboxes-content-analysis-form.php'; // Metaboxe HTML.
	}

	add_action( 'save_post', 'seopress_save_ca_metabox', 10, 2 );
	/**
	 * Save CA metabox
	 *
	 * @param int $post_id Post ID.
	 * @param object $post Post object.
	 */
	function seopress_save_ca_metabox( $post_id, $post ) {
		// Nonce.
		if ( ! isset( $_POST['seopress_content_analysis_nonce'] ) || ! wp_verify_nonce( $_POST['seopress_content_analysis_nonce'], plugin_basename( __FILE__ ) ) ) {
			return $post_id;
		}

		// Post type object.
		$post_type = get_post_type_object( $post->post_type );

		// Check permission.
		if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
			return $post_id;
		}

		if ( 'attachment' !== get_post_type( $post_id ) ) {
			if ( isset( $_POST['seopress_analysis_target_kw'] ) ) {
				$target_kw = sanitize_text_field( $_POST['seopress_analysis_target_kw'] );
				update_post_meta( $post_id, '_seopress_analysis_target_kw', $target_kw );
			}
		}
	}
}

if ( is_user_logged_in() ) {
	if ( is_super_admin() ) {
		echo seopress_display_seo_metaboxe();
	} else {
		global $wp_roles;
		$user = wp_get_current_user();
		// Get current user role.
		if ( isset( $user->roles ) && is_array( $user->roles ) && ! empty( $user->roles ) ) {
			$seopress_user_role = current( $user->roles );

			// If current user role matchs values from Security settings then apply -- SEO Metaboxe.
			if ( ! empty( seopress_get_service( 'AdvancedOption' )->getSecurityMetaboxRole() ) ) {
				if ( array_key_exists( $seopress_user_role, seopress_get_service( 'AdvancedOption' )->getSecurityMetaboxRole() ) ) {
					// Do nothing.
				} else {
					echo seopress_display_seo_metaboxe();
				}
			} else {
				echo seopress_display_seo_metaboxe();
			}
		}
	}
}
