Advertisement
JoachimBrnd

Voxel completeness calculator

Feb 20th, 2025 (edited)
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.20 KB | None | 0 0
  1. <?php
  2.  
  3. function update_post_completeness_score($post_id, $post, $update) {
  4.     // Bail if autosaving or if a revision.
  5.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  6.     if ( wp_is_post_revision( $post_id ) ) return;
  7.    
  8.     // Load voxel settings from wp_options.
  9.     $raw_settings = get_option('voxel:post_types');
  10.     if ( !$raw_settings ) {
  11.         return;
  12.     }
  13.     $voxel_settings = json_decode($raw_settings, true);
  14.     if ( !is_array($voxel_settings) ) {
  15.         return;
  16.     }
  17.    
  18.     $post_type = $post->post_type;
  19.     if ( !isset($voxel_settings[$post_type]) ) {
  20.         return;
  21.     }
  22.    
  23.     // Get field definitions for this post type.
  24.     $fields = isset($voxel_settings[$post_type]['fields']) ? $voxel_settings[$post_type]['fields'] : array();
  25.    
  26.     // Check if CPT has a field with key = "score"
  27.     $has_score_field = false;
  28.     foreach ( $fields as $field ) {
  29.         if ( isset($field['key']) && $field['key'] === 'score' ) {
  30.             $has_score_field = true;
  31.             break;
  32.         }
  33.     }
  34.     if ( !$has_score_field ) {
  35.         return;
  36.     }
  37.  
  38.     $total_score = 0;
  39.     foreach ( $fields as $field ) {
  40.         if ( empty($field['css_class']) ) {
  41.             continue;
  42.         }
  43.         // Match a css class pattern "score-[number]".
  44.         if ( preg_match('/score-(\d+)/', $field['css_class'], $matches) ) {
  45.             $score_value = intval($matches[1]);
  46.             $field_key = $field['key'];
  47.            
  48.             // Get the field value.
  49.             $value = null;
  50.             switch ( $field_key ) {
  51.                 case 'title':
  52.                     $value = $post->post_title;
  53.                     break;
  54.                 case 'content':
  55.                     $value = $post->post_content;
  56.                     break;
  57.                 case 'excerpt':
  58.                     $value = $post->post_excerpt;
  59.                     break;
  60.                 case '_thumbnail_id':
  61.                     $value = has_post_thumbnail($post_id) ? get_post_thumbnail_id($post_id) : '';
  62.                     break;
  63.                 default:
  64.                     $value = get_post_meta($post_id, $field_key, true);
  65.             }
  66.            
  67.             // If the field is filled add the score.
  68.             if ( !empty($value) ) {
  69.                 $total_score += $score_value;
  70.             }
  71.         }
  72.     }
  73.    
  74.     // Save the aggregated score in the "score" field.
  75.     update_post_meta($post_id, 'score', $total_score);
  76. }
  77. add_action('save_post', 'update_post_completeness_score', 10, 3);
  78.  
  79. // New: Activate hook to update completeness score for all posts.
  80. function cpt_completeness_score_update_all() {
  81.     // Load voxel settings from wp_options.
  82.     $raw_settings = get_option('voxel:post_types');
  83.     if (!$raw_settings) return;
  84.     $voxel_settings = json_decode($raw_settings, true);
  85.     if (!is_array($voxel_settings)) return;
  86.    
  87.     foreach ($voxel_settings as $post_type => $config) {
  88.         // Ensure the post type has a field with key 'score'
  89.         $fields = isset($config['fields']) ? $config['fields'] : array();
  90.         $has_score_field = false;
  91.         foreach ($fields as $field) {
  92.             if ( isset($field['key']) && $field['key'] === 'score' ) {
  93.                 $has_score_field = true;
  94.                 break;
  95.             }
  96.         }
  97.         if ( !$has_score_field ) {
  98.             continue;
  99.         }
  100.        
  101.         // Query all posts of the current type.
  102.         $posts = get_posts(array(
  103.             'post_type'      => $post_type,
  104.             'posts_per_page' => -1,
  105.             'post_status'    => 'any'
  106.         ));
  107.         if (empty($posts)) continue;
  108.        
  109.         // Update completeness score for each post.
  110.         foreach ($posts as $post) {
  111.             update_post_completeness_score($post->ID, $post, false);
  112.         }
  113.     }
  114. }
  115.  
  116. // New: Global function to manually trigger recalculation.
  117. function recalculate_all_post_scores() {
  118.     // Recalculate scores for all posts.
  119.     cpt_completeness_score_update_all();
  120.     // Update the flag option.
  121.     update_option('cpt_completeness_score_recalculated', 1);
  122.     return 'CPT Completeness Score: Recalculation completed.';
  123. }
  124.  
  125. // New: Activation hook to trigger the recalculation.
  126. if ( function_exists('register_activation_hook') ) {
  127.     register_activation_hook( __FILE__, function() {
  128.         recalculate_all_post_scores();
  129.     } );
  130. }
  131.  
  132. if ( is_admin() ) {
  133.     add_action('admin_init', function() {
  134.         global $cpt_score_message;
  135.         if ( !get_option('cpt_completeness_score_recalculated') ) {
  136.             recalculate_all_post_scores();
  137.             $cpt_score_message = 'CPT Completeness Score: Recalculation completed.';
  138.         } else {
  139.             $cpt_score_message = 'CPT Completeness Score: Already recalculated.';
  140.         }
  141.     });
  142.     add_action('admin_footer', function() {
  143.         global $cpt_score_message;
  144.         ?>
  145.         <script>
  146.             console.log('<?php echo addslashes($cpt_score_message); ?>');
  147.         </script>
  148.         <?php
  149.     });
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement