| 493 | * Shutdown action that will add a filter to inject additional postmeta containing translated content if Jetpack is syncing. |
| 494 | * |
| 495 | */ |
| 496 | public function append_meta_for_jetpack() { |
| 497 | // TEMP: only do this for low numbered plugin IDs, till we're sure it works. |
| 498 | if ( get_post()->ID > 200 ) |
| 499 | return; |
| 500 | |
| 501 | // Guess if a Jetpack sync is scheduled to run. It runs during shutdown at a lower priority than this action, so we can get in first. |
| 502 | // Fetching the extra meta to inject is expensive, so we only want to do this if a sync is likely. |
| 503 | if ( class_exists( 'Jetpack' ) && !empty(\Jetpack::init()->sync->sync) ) { |
| 504 | add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ), 10, 2 ); |
| 505 | } |
| 506 | |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Filter for wporg_plugins_custom_meta_fields to inject translated content for ES. |
| 511 | * |
| 512 | * @param array $meta |
| 513 | * @param int $post_id |
| 514 | * @return array |
| 515 | */ |
| 516 | public function filter_post_meta_i18n( $meta, $post_id ) { |
| 517 | // Prevent recursion and repeat runs |
| 518 | remove_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n' ) ); |
| 519 | |
| 520 | if ( get_post()->ID == $post_id ) { |
| 521 | $locales_to_sync = array( 'fr_fr', 'es_es' ); // This should probably be a list of available translations for the plugin readme. |
| 522 | |
| 523 | global $locale; |
| 524 | $_locale = $locale; |
| 525 | foreach ( $locales_to_sync as $locale ) { |
| 526 | $this->i18n_meta[$post_id]['post_title_'.$locale] = $this->translate_post_title( get_the_title(), $post_id ); |
| 527 | $this->i18n_meta[$post_id]['post_excerpt_'.$locale] = $this->translate_post_excerpt( get_the_excerpt() ); |
| 528 | |
| 529 | // Split up the content to translate it in sections |
| 530 | $content = ''; |
| 531 | $sections = $this->split_post_content_into_pages( get_the_content() ); |
| 532 | foreach ( $sections as $section => $section_content ) |
| 533 | $content .= $this->translate_post_content( $section_content, $section ); |
| 534 | $this->i18n_meta[$post_id]['post_content_'.$locale] = $content; |
| 535 | |
| 536 | } |
| 537 | |
| 538 | $locale = $_locale; |
| 539 | |
| 540 | $meta = array_merge( $meta, array_keys( $this->i18n_meta[$post_id] ) ); |
| 541 | } |
| 542 | |
| 543 | add_filter( 'wporg_plugins_custom_meta_fields', array( $this, 'filter_post_meta_i18n'), 10, 2 ); |
| 544 | return $meta; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | /** |