Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function update_post_completeness_score($post_id, $post, $update) {
- // Bail if autosaving or if a revision.
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
- if ( wp_is_post_revision( $post_id ) ) return;
- // Load voxel settings from wp_options.
- $raw_settings = get_option('voxel:post_types');
- if ( !$raw_settings ) {
- return;
- }
- $voxel_settings = json_decode($raw_settings, true);
- if ( !is_array($voxel_settings) ) {
- return;
- }
- $post_type = $post->post_type;
- if ( !isset($voxel_settings[$post_type]) ) {
- return;
- }
- // Get field definitions for this post type.
- $fields = isset($voxel_settings[$post_type]['fields']) ? $voxel_settings[$post_type]['fields'] : array();
- // Check if CPT has a field with key = "score"
- $has_score_field = false;
- foreach ( $fields as $field ) {
- if ( isset($field['key']) && $field['key'] === 'score' ) {
- $has_score_field = true;
- break;
- }
- }
- if ( !$has_score_field ) {
- return;
- }
- $total_score = 0;
- foreach ( $fields as $field ) {
- if ( empty($field['css_class']) ) {
- continue;
- }
- // Match a css class pattern "score-[number]".
- if ( preg_match('/score-(\d+)/', $field['css_class'], $matches) ) {
- $score_value = intval($matches[1]);
- $field_key = $field['key'];
- // Get the field value.
- $value = null;
- switch ( $field_key ) {
- case 'title':
- $value = $post->post_title;
- break;
- case 'content':
- $value = $post->post_content;
- break;
- case 'excerpt':
- $value = $post->post_excerpt;
- break;
- case '_thumbnail_id':
- $value = has_post_thumbnail($post_id) ? get_post_thumbnail_id($post_id) : '';
- break;
- default:
- $value = get_post_meta($post_id, $field_key, true);
- }
- // If the field is filled add the score.
- if ( !empty($value) ) {
- $total_score += $score_value;
- }
- }
- }
- // Save the aggregated score in the "score" field.
- update_post_meta($post_id, 'score', $total_score);
- }
- add_action('save_post', 'update_post_completeness_score', 10, 3);
- // New: Activate hook to update completeness score for all posts.
- function cpt_completeness_score_update_all() {
- // Load voxel settings from wp_options.
- $raw_settings = get_option('voxel:post_types');
- if (!$raw_settings) return;
- $voxel_settings = json_decode($raw_settings, true);
- if (!is_array($voxel_settings)) return;
- foreach ($voxel_settings as $post_type => $config) {
- // Ensure the post type has a field with key 'score'
- $fields = isset($config['fields']) ? $config['fields'] : array();
- $has_score_field = false;
- foreach ($fields as $field) {
- if ( isset($field['key']) && $field['key'] === 'score' ) {
- $has_score_field = true;
- break;
- }
- }
- if ( !$has_score_field ) {
- continue;
- }
- // Query all posts of the current type.
- $posts = get_posts(array(
- 'post_type' => $post_type,
- 'posts_per_page' => -1,
- 'post_status' => 'any'
- ));
- if (empty($posts)) continue;
- // Update completeness score for each post.
- foreach ($posts as $post) {
- update_post_completeness_score($post->ID, $post, false);
- }
- }
- }
- // New: Global function to manually trigger recalculation.
- function recalculate_all_post_scores() {
- // Recalculate scores for all posts.
- cpt_completeness_score_update_all();
- // Update the flag option.
- update_option('cpt_completeness_score_recalculated', 1);
- return 'CPT Completeness Score: Recalculation completed.';
- }
- // New: Activation hook to trigger the recalculation.
- if ( function_exists('register_activation_hook') ) {
- register_activation_hook( __FILE__, function() {
- recalculate_all_post_scores();
- } );
- }
- if ( is_admin() ) {
- add_action('admin_init', function() {
- global $cpt_score_message;
- if ( !get_option('cpt_completeness_score_recalculated') ) {
- recalculate_all_post_scores();
- $cpt_score_message = 'CPT Completeness Score: Recalculation completed.';
- } else {
- $cpt_score_message = 'CPT Completeness Score: Already recalculated.';
- }
- });
- add_action('admin_footer', function() {
- global $cpt_score_message;
- ?>
- <script>
- console.log('<?php echo addslashes($cpt_score_message); ?>');
- </script>
- <?php
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement