<?php
// Exit if Accessed Directly
if ( ! defined( 'ABSPATH' ) ) exit;

// Verify if The Id is Correct
if ( ! isset( $_GET['id_wp'] ) || ! is_numeric( $_GET['id_wp'] ) || intval( $_GET['id_wp'] ) <= 0 ) {
    wp_die( esc_html__( 'Id Invalid or Incorrect.', 'xpose-content' ) );
}

$id_wp = intval( $_GET['id_wp'] );

// Verify if Post Exists
$post = get_post( $id_wp );
if ( ! $post ) {
    wp_die( esc_html__( 'Content Not Found', 'xpose-content' ) );
}

// Classes Require
require_once dirname( plugin_dir_path( __FILE__ ) ) . '/includes/xc-class-queries.php';
?>

<div class="xc-dash">
<h2 class="titles"><a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-dashboard" title="Xpose Content" class="links">Xpose Content</a></h2>

<h2 class="titles"><?php esc_html_e( 'Post OnPage', 'xpose-content' ); ?></h2>

<div class="xc-table-wrapper">
    <table class="xc-results xc-results-lists-op">
        <thead>
            <tr>
                <th colspan="2" class="left"><?php esc_html_e( 'Post Content', 'xpose-content' ); ?></th>
            </tr>
        </thead>
        <tbody>
        <?php
		$onpage_data = XC_Queries::get_onpage($id_wp);
		
		if ( $onpage_data ) {
			
			echo '<tr>
            	<td class="left">'.esc_html__( 'Title', 'xpose-content' ).' ('.esc_html__( 'Size', 'xpose-content' ).')</td>
            	<td>' . esc_html( $onpage_data->post_title ) . ' (' . esc_html( $onpage_data->title_length ) . ')</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Id WP', 'xpose-content' ).'</td>
            	<td>' . esc_html( $onpage_data->post_id ) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Type', 'xpose-content' ).'</td>
            	<td>' . esc_html( $onpage_data->post_type ) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Words', 'xpose-content' ).'</td>
            	<td>' . esc_html( $onpage_data->post_words ) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Modified', 'xpose-content' ).'</td>
            	<td>' . esc_html( date("d/m/Y", strtotime($onpage_data->post_modified ))) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Published', 'xpose-content' ).'</td>
            	<td>' . esc_html( date("d/m/Y", strtotime($onpage_data->post_date ))) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'URL', 'xpose-content' ).' ('.esc_html__( 'Size', 'xpose-content' ).')</td>
            	<td>' . esc_html( $onpage_data->post_name ) . ' (' . esc_html( $onpage_data->url_length ) . ')</td>
            </tr>';
		}        
		?>
        </tbody>
    </table>
</div>

<div class="xc-table-wrapper">
	<table class="xc-results xc-results-lists-op">
        <thead>
            <tr>
                <th colspan="2" class="left"><?php esc_html_e( 'Meta Description', 'xpose-content' ); ?></th>
            </tr>
        </thead>
        <tbody>
		<?php
        $meta_descriptions_data = XC_Queries::get_meta_descriptions($id_wp);
        
        if ( ! empty( $meta_descriptions_data ) ) {
            foreach ( $meta_descriptions_data as $plugin => $description ) {
                $clean = wp_strip_all_tags( $description, true );
        
                if ( class_exists( 'Normalizer' ) ) {
                    $clean = Normalizer::normalize( $clean, Normalizer::FORM_C );
                }
        
                $clean = preg_replace( '/\p{C}+/u', '', $clean );
                $clean = trim( $clean );
        
                $meta_description_size = function_exists( 'mb_strlen' ) ? mb_strlen( $clean, 'UTF-8' ) : strlen( $clean );
                $meta_description_text = esc_html( $clean );
        
                echo '<tr>
                        <td class="left">' . esc_html( $plugin . ' ' . __( 'Content', 'xpose-content' ) ) . '</td>
                        <td>' . esc_html( $meta_description_text ) . '</td>
                      </tr>
                      <tr>
                        <td class="left">' . esc_html( $plugin . ' ' . __( 'Size', 'xpose-content' ) ) . '</td>
                        <td>' . esc_html( $meta_description_size ) . '</td>
                      </tr>';
            }
        } else {
            echo '<tr>
                    <td class="left">' . esc_html__( 'Content', 'xpose-content' ) . '</td>
                    <td><span class="spans">' . esc_html__( 'No Results!', 'xpose-content' ) . '</span></td>
                  </tr>
                  <tr>
                    <td class="left">' . esc_html__( 'Size', 'xpose-content' ) . '</td>
                    <td>0</td>
                  </tr>';
        }
        ?>
        </tbody>
    </table>
</div>

<div class="xc-table-wrapper">
    <table class="xc-results xc-results-lists-op">
        <thead>
            <tr>
                <th colspan="2" class="left"><?php esc_html_e( 'Keyowrds Density', 'xpose-content' ); ?></th>
            </tr>
        </thead>
        <tbody>
		<?php
        $keywords_density_data = XC_Queries::get_keywords_density($id_wp);
        
		if ( !empty($keywords_density_data) ) {
			foreach ( $keywords_density_data as $word => $percent ) {
				echo '<tr>
            	<td class="left">' . esc_html( $word ) . '</td>
            	<td>' . esc_html( $percent ) . '%</td>
            </tr>';
			}
		} else {
			echo '<tr><td colspan="2"><span class="spans">' . esc_html__( 'No Results!', 'xpose-content' ) . '</span></td></tr>';
		}
        ?>
        </tbody>
    </table>
</div>

<div class="xc-table-wrapper">
    <table class="xc-results xc-results-lists-op">
        <thead>
            <tr>
                <th colspan="2" class="left"><?php esc_html_e( 'Tags', 'xpose-content' ); ?></th>
            </tr>
        </thead>
        <tbody>
		<?php
        $tags_data = XC_Queries::get_tags($id_wp);
		
		echo '<tr>
            	<td class="left">H2 - H3</td>
            	<td>' . esc_html( $tags_data['h2_h3'] ) . '</td>
            </tr>
        	<tr>
            	<td class="left">H4 - H6</td>
            	<td>' . esc_html( $tags_data['h4_h6'] ) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Images', 'xpose-content' ).'</td>
            	<td>' . esc_html( $tags_data['images'] ) . '</td>
            </tr>';
        ?>
        </tbody>
    </table>
</div>

<div class="xc-table-wrapper">
    <table class="xc-results xc-results-lists-op">
        <thead>
            <tr>
                <th colspan="2" class="left"><?php esc_html_e( 'Links', 'xpose-content' ); ?></th>
            </tr>
        </thead>
        <tbody>
		<?php
        $links_data = XC_Queries::get_links($id_wp);
		
		echo '<tr>
            	<td class="left">'.esc_html__( 'Linker', 'xpose-content' ).'</td>
            	<td>' . esc_html( $links_data['linker'] ) . '</td>
            </tr>
        	<tr>
            	<td class="left">'.esc_html__( 'Linked', 'xpose-content' ).'</td>
            	<td>' . esc_html( $links_data['linked'] ) . '</td>
            </tr>';
        ?>
        </tbody>
    </table>
</div>

<div class="xc-row">
	<div class="xc-col-12">
        <a href="<?php echo esc_url(site_url());?>/wp-admin/admin.php?page=xc-posts" title="Posts Dashboard" class="xc-button-export"><?php esc_html_e( 'Posts Dashboard', 'xpose-content' );?></a>
    </div>
</div>

<?php
// Credits
require_once dirname( plugin_dir_path( __FILE__ ) ) . '/includes/footer-credits.php';
?>
</div>