| Server IP : 38.190.208.107 / Your IP : 216.73.217.13 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : root ( 0) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.10/wp-content/plugins/clarity-seo/includes/ |
Upload File : |
<?php
// Add metabox to Gutenberg editor for every page/post
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
function clarity_seo_add_metabox() {
$post_types = ['page', 'post'];
if (post_type_exists('product')) {
$post_types[] = 'product'; // Add 'product' only if WooCommerce is active
}
add_meta_box(
'clarity_seo_metabox',
'Clarity SEO Settings',
'clarity_seo_metabox_callback',
$post_types,
'side',
'high'
);
}
add_action('add_meta_boxes', 'clarity_seo_add_metabox');
function clarity_seo_metabox_callback($post) {
wp_nonce_field('clarity_seo_save_meta', 'clarity_seo_nonce');
$meta_title = get_post_meta($post->ID, '_clarity_seo_meta_title', true);
$meta_description = get_post_meta($post->ID, '_clarity_seo_meta_description', true);
$meta_keywords = get_post_meta($post->ID, '_clarity_seo_meta_keywords', true);
$og_image = get_post_meta($post->ID, '_clarity_seo_og_image', true);
$schema_service = get_post_meta($post->ID, '_clarity_seo_service', true);
$focus_keywords = get_post_meta($post->ID, '_clarity_seo_focus_keywords', true);
$schema_type = get_post_meta($post->ID, '_clarity_seo_type', true) ?: 'WebPage';
$disable_schema = get_post_meta($post->ID, '_clarity_seo_disable', true);
// Readability score (Flesch-Kincaid)
$content = wp_strip_all_tags($post->post_content);
$words = str_word_count($content);
$sentences = preg_match_all('/[.!?]+/', $content) ?: 1;
preg_match_all('/[aeiouy]+/i', $content, $syllable_matches);
$syllables = count($syllable_matches[0]);
$readability = 0;
if ($words > 0 && $syllables > 0) {
$asl = $words / $sentences;
$asw = $syllables / $words;
$readability = max(0, 206.835 - (1.015 * $asl) - (84.6 * $asw));
}
$readability_score = round($readability);
$readability_tip = $readability_score < 60 ? 'Improve: Shorten sentences, use common words.' : 'Good: Easy to read.';
// Focus keywords density
$keyword_density = !empty($focus_keywords) && $words > 0 ? round((substr_count(strtolower($content), strtolower($focus_keywords)) / $words) * 100, 1) : 0;
$density_tip = $keyword_density > 2.5 ? 'High—consider reducing.' : ($keyword_density < 0.5 ? 'Low—add more naturally.' : 'Optimal.');
// SEO Score (simple: based on title, desc, keywords, density, readability)
$seo_score = 0;
if (!empty($meta_title)) $seo_score += 20;
if (!empty($meta_description)) $seo_score += 20;
if (!empty($meta_keywords)) $seo_score += 20;
if ($keyword_density >= 0.5 && $keyword_density <= 2.5) $seo_score += 20;
if ($readability_score >= 60) $seo_score += 20;
// Internal linking suggestions (based on focus keywords)
$linking_suggestions = [];
if (!empty($focus_keywords)) {
$related_posts = get_posts([
's' => $focus_keywords,
'numberposts' => 5, // Buffer for potential skip
'post_status' => 'publish'
]);
$count = 0;
foreach ($related_posts as $related) {
if ($related->ID === $post->ID) {
continue; // Skip current post
}
if ($count >= 3) break; // Limit to 3
$linking_suggestions[] = '<a href="' . esc_url(get_permalink($related->ID)) . '" target="_blank">' . esc_html($related->post_title) . '</a>';
$count++;
}
}
?>
<p>
<label for="clarity_seo_meta_title">
<?php esc_html_e('Meta Title:', 'clarity-seo'); ?>
</label><br>
<input type="text" id="clarity_seo_meta_title" name="clarity_seo_meta_title" value="<?php echo esc_attr($meta_title); ?>" style="width: 100%;">
</p>
<p>
<label for="clarity_seo_meta_description">
<?php esc_html_e('Meta Description:', 'clarity-seo'); ?>
</label><br>
<textarea id="clarity_seo_meta_description" name="clarity_seo_meta_description" style="width: 100%;"><?php echo esc_textarea($meta_description); ?></textarea>
</p>
<p>
<label for="clarity_seo_meta_keywords">
<?php esc_html_e('Meta Keywords:', 'clarity-seo'); ?>
</label><br>
<input type="text" id="clarity_seo_meta_keywords" name="clarity_seo_meta_keywords" value="<?php echo esc_attr($meta_keywords); ?>" style="width: 100%;">
</p>
<p>
<label for="clarity_seo_focus_keywords">
<?php esc_html_e('Focus Keywords:', 'clarity-seo'); ?>
</label><br>
<input type="text" id="clarity_seo_focus_keywords" name="clarity_seo_focus_keywords" value="<?php echo esc_attr($focus_keywords); ?>" style="width: 100%;" placeholder="comma, separated, keywords">
</p>
<p>
<label for="clarity_seo_og_image">
<?php esc_html_e('OG Image URL:', 'clarity-seo'); ?>
</label><br>
<input type="text" id="clarity_seo_og_image" name="clarity_seo_og_image" value="<?php echo esc_url($og_image); ?>" style="width: 100%;">
</p>
<p>
<label for="clarity_seo_service">
<?php esc_html_e('Service Schema Description:', 'clarity-seo'); ?>
</label><br>
<textarea id="clarity_seo_service" name="clarity_seo_service" style="width: 100%;"><?php echo esc_textarea($schema_service); ?></textarea>
</p>
<p>
<label for="clarity_seo_type">
<?php esc_html_e('Schema Type (comma for multiple):', 'clarity-seo'); ?>
</label><br>
<input type="text" id="clarity_seo_type" name="clarity_seo_type" value="<?php echo esc_attr($schema_type); ?>" style="width: 100%;" placeholder="WebPage,Article,Product,...">
</p>
<p>
<label for="clarity_seo_disable">
<?php esc_html_e('Disable Schema on this Post:', 'clarity-seo'); ?>
<input type="checkbox" id="clarity_seo_disable" name="clarity_seo_disable" value="1" <?php checked($disable_schema, 1); ?>>
</label>
</p>
<hr>
<p><strong><?php esc_html_e('Readability Score:', 'clarity-seo'); ?></strong> <?php echo esc_html($readability_score); ?>/100</p>
<p><small><?php echo esc_html($readability_tip); ?></small></p>
<p><strong><?php esc_html_e('Keyword Density:', 'clarity-seo'); ?></strong> <?php echo esc_html($keyword_density); ?>%</p>
<p><small><?php echo esc_html($density_tip); ?></small></p>
<p><strong><?php esc_html_e('SEO Score:', 'clarity-seo'); ?></strong> <?php echo esc_html($seo_score); ?>/100</p>
<hr>
<p><strong><?php esc_html_e('Internal Linking Suggestions:', 'clarity-seo'); ?></strong></p>
<?php if (!empty($linking_suggestions)): ?>
<ul>
<?php foreach ($linking_suggestions as $suggestion): ?>
<li><?php echo wp_kses_post($suggestion); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p><small><?php esc_html_e('No suggestions - Add focus keywords to see related posts for linking.', 'clarity-seo'); ?></small></p>
<?php endif; ?>
<!-- New: Trends div -->
<div id="clarity-seo-trends"></div>
<?php
}
// Save metabox data
function clarity_seo_save_meta($post_id) {
if ( ! isset( $_POST['clarity_seo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['clarity_seo_nonce'] ) ), 'clarity_seo_save_meta' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$fields = ['clarity_seo_meta_title', 'clarity_seo_meta_description', 'clarity_seo_meta_keywords', 'clarity_seo_og_image', 'clarity_seo_service', 'clarity_seo_focus_keywords', 'clarity_seo_type', 'clarity_seo_disable'];
foreach ( $fields as $field ) {
if ( isset( $_POST[ $field ] ) ) {
$value = in_array( $field, ['clarity_seo_meta_description', 'clarity_seo_service'] )
? sanitize_textarea_field( wp_unslash( $_POST[ $field ] ) )
: sanitize_text_field( wp_unslash( $_POST[ $field ] ) );
update_post_meta( $post_id, '_' . $field, $value );
} else if ($field === 'clarity_seo_disable') {
delete_post_meta($post_id, '_clarity_seo_disable');
}
}
}
add_action('save_post', 'clarity_seo_save_meta');
?>