| @@ -1,233 +1,233 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -// If this file is called directly, abort. | |
| 4 | -if ( ! defined( 'WPINC' ) ) { | |
| 5 | - die; | |
| 6 | -} | |
| 7 | - | |
| 8 | - | |
| 9 | -/** | |
| 10 | - * HeartBeat: | |
| 11 | - * - Slug automatic translation | |
| 12 | - * - Clear bad translations and slugs | |
| 13 | - * | |
| 14 | - * @return void | |
| 15 | - */ | |
| 16 | -function wplng_ajax_heartbeat() { | |
| 17 | - | |
| 18 | - $last_beat = get_option( 'wplng_hb_last_update' ); | |
| 19 | - $now = time(); | |
| 20 | - $counter = 100; | |
| 21 | - | |
| 22 | - // Prevents frequent execution if the last heartbeat was within 10 minutes | |
| 23 | - if ( ! empty( $last_beat ) | |
| 24 | - && ( $last_beat + ( MINUTE_IN_SECONDS * 10 ) ) > $now | |
| 25 | - ) { | |
| 26 | - wp_send_json_success(); | |
| 27 | - return; | |
| 28 | - } | |
| 29 | - | |
| 30 | - // Update last heartbeat timestamp | |
| 31 | - update_option( 'wplng_hb_last_update', $now ); | |
| 32 | - | |
| 33 | - $counter = wplng_ajax_heartbeat_clear_bad_translations( $counter ); | |
| 34 | - $counter = wplng_ajax_heartbeat_clear_bad_slugs( $counter ); | |
| 35 | - | |
| 36 | - wp_send_json_success(); | |
| 37 | -} | |
| 38 | - | |
| 39 | - | |
| 40 | -/** | |
| 41 | - * Clears invalid or incorrect translations based on metadata validation and language checks. | |
| 42 | - * | |
| 43 | - * This function iterates through all translation posts of type 'wplng_translation', | |
| 44 | - * validates their metadata, and deletes those that are invalid or do not match the | |
| 45 | - * current language website ID. The deletion process is limited by the $counter parameter. | |
| 46 | - * | |
| 47 | - * @param int $counter The maximum number of translations to process and delete. | |
| 48 | - * @return int The remaining counter after processing. | |
| 49 | - */ | |
| 50 | -function wplng_ajax_heartbeat_clear_bad_translations( $counter ) { | |
| 51 | - | |
| 52 | - if ( $counter <= 0 ) { | |
| 53 | - return 0; | |
| 54 | - } | |
| 55 | - | |
| 56 | - $language_website_id = wplng_get_language_website_id(); | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Check translations | |
| 60 | - */ | |
| 61 | - | |
| 62 | - $translation_ids = get_posts( | |
| 63 | - array( | |
| 64 | - 'post_type' => 'wplng_translation', | |
| 65 | - 'posts_per_page' => -1, | |
| 66 | - 'no_found_rows' => true, | |
| 67 | - 'update_post_term_cache' => false, | |
| 68 | - 'update_post_meta_cache' => false, | |
| 69 | - 'cache_results' => false, | |
| 70 | - 'fields' => 'ids', | |
| 71 | - ) | |
| 72 | - ); | |
| 73 | - | |
| 74 | - foreach ( $translation_ids as $id ) { | |
| 75 | - | |
| 76 | - if ( $counter <= 0 ) { | |
| 77 | - break; | |
| 78 | - } | |
| 79 | - | |
| 80 | - $meta = get_post_meta( $id ); | |
| 81 | - | |
| 82 | - // Validate translation metadata | |
| 83 | - if ( empty( $meta['wplng_translation_original'][0] ) | |
| 84 | - || ! is_string( $meta['wplng_translation_original'][0] ) | |
| 85 | - || trim( $meta['wplng_translation_original'][0] ) === '' | |
| 86 | - || empty( $meta['wplng_translation_translations'][0] ) | |
| 87 | - || empty( $meta['wplng_translation_md5'][0] ) | |
| 88 | - ) { | |
| 89 | - | |
| 90 | - --$counter; | |
| 91 | - | |
| 92 | - // Debug (if enabled) | |
| 93 | - if ( true === WPLNG_DEBUG_BEAT ) { | |
| 94 | - $debug = array( | |
| 95 | - 'title' => 'wpLingua HeartBeat debug', | |
| 96 | - 'action' => 'Delete translation - Invalid data', | |
| 97 | - 'title' => get_the_title( $id ), | |
| 98 | - 'id' => $id, | |
| 99 | - ); | |
| 100 | - | |
| 101 | - error_log( var_export( $debug, true ) ); | |
| 102 | - } | |
| 103 | - | |
| 104 | - // Permanently delete the invalid translation | |
| 105 | - wp_delete_post( $id, true ); | |
| 106 | - continue; | |
| 107 | - } | |
| 108 | - | |
| 109 | - // Check language of the translation | |
| 110 | - if ( empty( $meta['wplng_translation_original_language_id'][0] ) | |
| 111 | - || $meta['wplng_translation_original_language_id'][0] !== $language_website_id | |
| 112 | - ) { | |
| 113 | - | |
| 114 | - --$counter; | |
| 115 | - | |
| 116 | - // Debug (if enabled) | |
| 117 | - if ( true === WPLNG_DEBUG_BEAT ) { | |
| 118 | - $debug = array( | |
| 119 | - 'title' => 'wpLingua HeartBeat debug', | |
| 120 | - 'action' => 'Delete translation - Incorrect original language', | |
| 121 | - 'title' => get_the_title( $id ), | |
| 122 | - 'id' => $id, | |
| 123 | - ); | |
| 124 | - | |
| 125 | - error_log( var_export( $debug, true ) ); | |
| 126 | - } | |
| 127 | - | |
| 128 | - // Permanently delete the translation | |
| 129 | - wp_delete_post( $id, true ); | |
| 130 | - continue; | |
| 131 | - } | |
| 132 | - } | |
| 133 | - | |
| 134 | - return $counter; | |
| 135 | -} | |
| 136 | - | |
| 137 | - | |
| 138 | -/** | |
| 139 | - * Clears invalid or incorrect slugs based on metadata validation and language checks. | |
| 140 | - * | |
| 141 | - * This function iterates through all slug posts of type 'wplng_slug', | |
| 142 | - * validates their metadata, and deletes those that are invalid or do not match the | |
| 143 | - * current language website ID. The deletion process is limited by the $counter parameter. | |
| 144 | - * | |
| 145 | - * @param int $counter The maximum number of slugs to process and delete. | |
| 146 | - * @return int The remaining counter after processing. | |
| 147 | - */ | |
| 148 | -function wplng_ajax_heartbeat_clear_bad_slugs( $counter ) { | |
| 149 | - | |
| 150 | - if ( $counter <= 0 ) { | |
| 151 | - return 0; | |
| 152 | - } | |
| 153 | - | |
| 154 | - $language_website_id = wplng_get_language_website_id(); | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Check slugs | |
| 158 | - */ | |
| 159 | - | |
| 160 | - $slug_ids = get_posts( | |
| 161 | - array( | |
| 162 | - 'post_type' => 'wplng_slug', | |
| 163 | - 'posts_per_page' => -1, | |
| 164 | - 'no_found_rows' => true, | |
| 165 | - 'update_post_term_cache' => false, | |
| 166 | - 'update_post_meta_cache' => false, | |
| 167 | - 'cache_results' => false, | |
| 168 | - 'fields' => 'ids', // Retrieve only post IDs | |
| 169 | - ) | |
| 170 | - ); | |
| 171 | - | |
| 172 | - foreach ( $slug_ids as $id ) { | |
| 173 | - | |
| 174 | - if ( $counter <= 0 ) { | |
| 175 | - break; | |
| 176 | - } | |
| 177 | - | |
| 178 | - $meta = get_post_meta( $id ); | |
| 179 | - | |
| 180 | - // Validate translation metadata | |
| 181 | - if ( empty( $meta['wplng_slug_original'][0] ) | |
| 182 | - || ! is_string( $meta['wplng_slug_original'][0] ) | |
| 183 | - || trim( $meta['wplng_slug_original'][0] ) === '' | |
| 184 | - || empty( $meta['wplng_slug_translations'][0] ) | |
| 185 | - || empty( $meta['wplng_slug_md5'][0] ) | |
| 186 | - ) { | |
| 187 | - | |
| 188 | - --$counter; | |
| 189 | - | |
| 190 | - // Debug (if enabled) | |
| 191 | - if ( true === WPLNG_DEBUG_BEAT ) { | |
| 192 | - $debug = array( | |
| 193 | - 'title' => 'wpLingua HeartBeat debug', | |
| 194 | - 'action' => 'Delete slug - Invalid data', | |
| 195 | - 'title' => get_the_title( $id ), | |
| 196 | - 'id' => $id, | |
| 197 | - ); | |
| 198 | - | |
| 199 | - error_log( var_export( $debug, true ) ); | |
| 200 | - } | |
| 201 | - | |
| 202 | - // Permanently delete the invalid slug | |
| 203 | - wp_delete_post( $id, true ); | |
| 204 | - continue; | |
| 205 | - } | |
| 206 | - | |
| 207 | - // Check language of the translation | |
| 208 | - if ( empty( $meta['wplng_slug_original_language_id'][0] ) | |
| 209 | - || $meta['wplng_slug_original_language_id'][0] !== $language_website_id | |
| 210 | - ) { | |
| 211 | - | |
| 212 | - --$counter; | |
| 213 | - | |
| 214 | - // Debug (if enabled) | |
| 215 | - if ( true === WPLNG_DEBUG_BEAT ) { | |
| 216 | - $debug = array( | |
| 217 | - 'title' => 'wpLingua HeartBeat debug', | |
| 218 | - 'action' => 'Delete slug - Incorrect original language', | |
| 219 | - 'title' => get_the_title( $id ), | |
| 220 | - 'id' => $id, | |
| 221 | - ); | |
| 222 | - | |
| 223 | - error_log( var_export( $debug, true ) ); | |
| 224 | - } | |
| 225 | - | |
| 226 | - // Permanently delete the translation | |
| 227 | - wp_delete_post( $id, true ); | |
| 228 | - continue; | |
| 229 | - } | |
| 230 | - } | |
| 231 | - | |
| 232 | - return $counter; | |
| 233 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +// If this file is called directly, abort. | |
| 4 | +if ( ! defined( 'WPINC' ) ) { | |
| 5 | + die; | |
| 6 | +} | |
| 7 | + | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * HeartBeat: | |
| 11 | + * - Slug automatic translation | |
| 12 | + * - Clear bad translations and slugs | |
| 13 | + * | |
| 14 | + * @return void | |
| 15 | + */ | |
| 16 | +function wplng_ajax_heartbeat() { | |
| 17 | + | |
| 18 | + $last_beat = get_option( 'wplng_hb_last_update' ); | |
| 19 | + $now = time(); | |
| 20 | + $counter = 100; | |
| 21 | + | |
| 22 | + // Prevents frequent execution if the last heartbeat was within 10 minutes | |
| 23 | + if ( ! empty( $last_beat ) | |
| 24 | + && ( $last_beat + ( MINUTE_IN_SECONDS * 10 ) ) > $now | |
| 25 | + ) { | |
| 26 | + wp_send_json_success(); | |
| 27 | + return; | |
| 28 | + } | |
| 29 | + | |
| 30 | + // Update last heartbeat timestamp | |
| 31 | + update_option( 'wplng_hb_last_update', $now ); | |
| 32 | + | |
| 33 | + $counter = wplng_ajax_heartbeat_clear_bad_translations( $counter ); | |
| 34 | + $counter = wplng_ajax_heartbeat_clear_bad_slugs( $counter ); | |
| 35 | + | |
| 36 | + wp_send_json_success(); | |
| 37 | +} | |
| 38 | + | |
| 39 | + | |
| 40 | +/** | |
| 41 | + * Clears invalid or incorrect translations based on metadata validation and language checks. | |
| 42 | + * | |
| 43 | + * This function iterates through all translation posts of type 'wplng_translation', | |
| 44 | + * validates their metadata, and deletes those that are invalid or do not match the | |
| 45 | + * current language website ID. The deletion process is limited by the $counter parameter. | |
| 46 | + * | |
| 47 | + * @param int $counter The maximum number of translations to process and delete. | |
| 48 | + * @return int The remaining counter after processing. | |
| 49 | + */ | |
| 50 | +function wplng_ajax_heartbeat_clear_bad_translations( $counter ) { | |
| 51 | + | |
| 52 | + if ( $counter <= 0 ) { | |
| 53 | + return 0; | |
| 54 | + } | |
| 55 | + | |
| 56 | + $language_website_id = wplng_get_language_website_id(); | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Check translations | |
| 60 | + */ | |
| 61 | + | |
| 62 | + $translation_ids = get_posts( | |
| 63 | + array( | |
| 64 | + 'post_type' => 'wplng_translation', | |
| 65 | + 'posts_per_page' => -1, | |
| 66 | + 'no_found_rows' => true, | |
| 67 | + 'update_post_term_cache' => false, | |
| 68 | + 'update_post_meta_cache' => false, | |
| 69 | + 'cache_results' => false, | |
| 70 | + 'fields' => 'ids', | |
| 71 | + ) | |
| 72 | + ); | |
| 73 | + | |
| 74 | + foreach ( $translation_ids as $id ) { | |
| 75 | + | |
| 76 | + if ( $counter <= 0 ) { | |
| 77 | + break; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $meta = get_post_meta( $id ); | |
| 81 | + | |
| 82 | + // Validate translation metadata | |
| 83 | + if ( empty( $meta['wplng_translation_original'][0] ) | |
| 84 | + || ! is_string( $meta['wplng_translation_original'][0] ) | |
| 85 | + || trim( $meta['wplng_translation_original'][0] ) === '' | |
| 86 | + || empty( $meta['wplng_translation_translations'][0] ) | |
| 87 | + || empty( $meta['wplng_translation_md5'][0] ) | |
| 88 | + ) { | |
| 89 | + | |
| 90 | + --$counter; | |
| 91 | + | |
| 92 | + // Debug (if enabled) | |
| 93 | + if ( true === WPLNG_DEBUG_BEAT ) { | |
| 94 | + $debug = array( | |
| 95 | + 'title' => 'wpLingua HeartBeat debug', | |
| 96 | + 'action' => 'Delete translation - Invalid data', | |
| 97 | + 'title_post' => get_the_title( $id ), | |
| 98 | + 'id' => $id, | |
| 99 | + ); | |
| 100 | + | |
| 101 | + error_log( var_export( $debug, true ) ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + // Permanently delete the invalid translation | |
| 105 | + wp_delete_post( $id, true ); | |
| 106 | + continue; | |
| 107 | + } | |
| 108 | + | |
| 109 | + // Check language of the translation | |
| 110 | + if ( empty( $meta['wplng_translation_original_language_id'][0] ) | |
| 111 | + || $meta['wplng_translation_original_language_id'][0] !== $language_website_id | |
| 112 | + ) { | |
| 113 | + | |
| 114 | + --$counter; | |
| 115 | + | |
| 116 | + // Debug (if enabled) | |
| 117 | + if ( true === WPLNG_DEBUG_BEAT ) { | |
| 118 | + $debug = array( | |
| 119 | + 'title' => 'wpLingua HeartBeat debug', | |
| 120 | + 'action' => 'Delete translation - Incorrect original language', | |
| 121 | + 'title_post' => get_the_title( $id ), | |
| 122 | + 'id' => $id, | |
| 123 | + ); | |
| 124 | + | |
| 125 | + error_log( var_export( $debug, true ) ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + // Permanently delete the translation | |
| 129 | + wp_delete_post( $id, true ); | |
| 130 | + continue; | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + return $counter; | |
| 135 | +} | |
| 136 | + | |
| 137 | + | |
| 138 | +/** | |
| 139 | + * Clears invalid or incorrect slugs based on metadata validation and language checks. | |
| 140 | + * | |
| 141 | + * This function iterates through all slug posts of type 'wplng_slug', | |
| 142 | + * validates their metadata, and deletes those that are invalid or do not match the | |
| 143 | + * current language website ID. The deletion process is limited by the $counter parameter. | |
| 144 | + * | |
| 145 | + * @param int $counter The maximum number of slugs to process and delete. | |
| 146 | + * @return int The remaining counter after processing. | |
| 147 | + */ | |
| 148 | +function wplng_ajax_heartbeat_clear_bad_slugs( $counter ) { | |
| 149 | + | |
| 150 | + if ( $counter <= 0 ) { | |
| 151 | + return 0; | |
| 152 | + } | |
| 153 | + | |
| 154 | + $language_website_id = wplng_get_language_website_id(); | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Check slugs | |
| 158 | + */ | |
| 159 | + | |
| 160 | + $slug_ids = get_posts( | |
| 161 | + array( | |
| 162 | + 'post_type' => 'wplng_slug', | |
| 163 | + 'posts_per_page' => -1, | |
| 164 | + 'no_found_rows' => true, | |
| 165 | + 'update_post_term_cache' => false, | |
| 166 | + 'update_post_meta_cache' => false, | |
| 167 | + 'cache_results' => false, | |
| 168 | + 'fields' => 'ids', // Retrieve only post IDs | |
| 169 | + ) | |
| 170 | + ); | |
| 171 | + | |
| 172 | + foreach ( $slug_ids as $id ) { | |
| 173 | + | |
| 174 | + if ( $counter <= 0 ) { | |
| 175 | + break; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $meta = get_post_meta( $id ); | |
| 179 | + | |
| 180 | + // Validate translation metadata | |
| 181 | + if ( empty( $meta['wplng_slug_original'][0] ) | |
| 182 | + || ! is_string( $meta['wplng_slug_original'][0] ) | |
| 183 | + || trim( $meta['wplng_slug_original'][0] ) === '' | |
| 184 | + || empty( $meta['wplng_slug_translations'][0] ) | |
| 185 | + || empty( $meta['wplng_slug_md5'][0] ) | |
| 186 | + ) { | |
| 187 | + | |
| 188 | + --$counter; | |
| 189 | + | |
| 190 | + // Debug (if enabled) | |
| 191 | + if ( true === WPLNG_DEBUG_BEAT ) { | |
| 192 | + $debug = array( | |
| 193 | + 'title' => 'wpLingua HeartBeat debug', | |
| 194 | + 'action' => 'Delete slug - Invalid data', | |
| 195 | + 'title_post' => get_the_title( $id ), | |
| 196 | + 'id' => $id, | |
| 197 | + ); | |
| 198 | + | |
| 199 | + error_log( var_export( $debug, true ) ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + // Permanently delete the invalid slug | |
| 203 | + wp_delete_post( $id, true ); | |
| 204 | + continue; | |
| 205 | + } | |
| 206 | + | |
| 207 | + // Check language of the slug | |
| 208 | + if ( empty( $meta['wplng_slug_original_language_id'][0] ) | |
| 209 | + || $meta['wplng_slug_original_language_id'][0] !== $language_website_id | |
| 210 | + ) { | |
| 211 | + | |
| 212 | + --$counter; | |
| 213 | + | |
| 214 | + // Debug (if enabled) | |
| 215 | + if ( true === WPLNG_DEBUG_BEAT ) { | |
| 216 | + $debug = array( | |
| 217 | + 'title' => 'wpLingua HeartBeat debug', | |
| 218 | + 'action' => 'Delete slug - Incorrect original language', | |
| 219 | + 'title_post' => get_the_title( $id ), | |
| 220 | + 'id' => $id, | |
| 221 | + ); | |
| 222 | + | |
| 223 | + error_log( var_export( $debug, true ) ); | |
| 224 | + } | |
| 225 | + | |
| 226 | + // Permanently delete the slug | |
| 227 | + wp_delete_post( $id, true ); | |
| 228 | + continue; | |
| 229 | + } | |
| 230 | + } | |
| 231 | + | |
| 232 | + return $counter; | |
| 233 | +} | |