PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/polylang-pro/include/translated-term.php

https://bitbucket.org/studioparkers/spinozacentre.2018.nl
PHP | 168 lines | 68 code | 19 blank | 81 comment | 7 complexity | 5f74f29471dad412bb1ec54f4779b82d MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Setups the taxonomies languages and translations model
  4. *
  5. * @since 1.8
  6. */
  7. class PLL_Translated_Term extends PLL_Translated_Object {
  8. /**
  9. * Constructor
  10. *
  11. * @since 1.8
  12. *
  13. * @param object $model
  14. */
  15. public function __construct( &$model ) {
  16. $this->object_type = 'term';
  17. $this->tax_language = 'term_language';
  18. $this->tax_translations = 'term_translations';
  19. $this->tax_tt = 'tl_term_taxonomy_id';
  20. parent::__construct( $model );
  21. // filters to prime terms cache
  22. add_filter( 'get_terms', array( &$this, '_prime_terms_cache' ), 10, 2 );
  23. add_filter( 'wp_get_object_terms', array( &$this, 'wp_get_object_terms' ), 10, 3 );
  24. }
  25. /**
  26. * Stores the term language in the database
  27. *
  28. * @since 0.6
  29. *
  30. * @param int $term_id term id
  31. * @param int|string|object $lang language ( term_id or slug or object )
  32. */
  33. public function set_language( $term_id, $lang ) {
  34. $term_id = (int) $term_id;
  35. wp_set_object_terms( $term_id, $lang ? $this->model->get_language( $lang )->tl_term_id : '', 'term_language' );
  36. // add translation group for correct WXR export
  37. $translations = $this->get_translations( $term_id );
  38. if ( $slug = array_search( $term_id, $translations ) ) {
  39. unset( $translations[ $slug ] );
  40. }
  41. $this->save_translations( $term_id, $translations );
  42. }
  43. /**
  44. * Removes the term language in database
  45. *
  46. * @since 0.5
  47. *
  48. * @param int $term_id term id
  49. */
  50. public function delete_language( $term_id ) {
  51. wp_delete_object_term_relationships( $term_id, 'term_language' );
  52. }
  53. /**
  54. * Returns the language of a term
  55. *
  56. * @since 0.1
  57. *
  58. * @param int|string $value term id or term slug
  59. * @param string $taxonomy optional taxonomy needed when the term slug is passed as first parameter
  60. * @return bool|object PLL_Language object, false if no language is associated to that term
  61. */
  62. public function get_language( $value, $taxonomy = '' ) {
  63. if ( is_numeric( $value ) ) {
  64. $term_id = $value;
  65. }
  66. // get_term_by still not cached in WP 3.5.1 but internally, the function is always called by term_id
  67. elseif ( is_string( $value ) && $taxonomy ) {
  68. $term_id = get_term_by( 'slug', $value , $taxonomy )->term_id;
  69. }
  70. // get the language and make sure it is a PLL_Language object
  71. return isset( $term_id ) && ( $lang = $this->get_object_term( $term_id, 'term_language' ) ) ? $this->model->get_language( $lang->term_id ) : false;
  72. }
  73. /**
  74. * Tells the parent class to always store a translation term
  75. *
  76. * @since 1.8
  77. *
  78. * @param array $translations: an associative array of translations with language code as key and translation id as value
  79. */
  80. protected function keep_translation_group( $translations ) {
  81. return true;
  82. }
  83. /**
  84. * Deletes a translation
  85. *
  86. * @since 0.5
  87. *
  88. * @param int $id term id
  89. */
  90. public function delete_translation( $id ) {
  91. global $wpdb;
  92. $slug = array_search( $id, $this->get_translations( $id ) ); // in case some plugin stores the same value with different key
  93. parent::delete_translation( $id );
  94. if ( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( * ) FROM $wpdb->terms WHERE term_id = %d;", $id ) ) ) {
  95. // always keep a group for terms to allow relationships remap when importing from a WXR file
  96. $translations[ $slug ] = $id;
  97. wp_insert_term( $group = uniqid( 'pll_' ), 'term_translations', array( 'description' => serialize( $translations ) ) );
  98. wp_set_object_terms( $id, $group, 'term_translations' );
  99. }
  100. }
  101. /**
  102. * A join clause to add to sql queries when filtering by language is needed directly in query
  103. *
  104. * @since 1.2
  105. *
  106. * @return string join clause
  107. */
  108. public function join_clause() {
  109. global $wpdb;
  110. return " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = t.term_id";
  111. }
  112. /**
  113. * Cache language and translations when terms are queried by get_terms
  114. *
  115. * @since 1.2
  116. *
  117. * @param array $terms queried terms
  118. * @param array $taxonomies queried taxonomies
  119. * @return array unmodified $terms
  120. */
  121. public function _prime_terms_cache( $terms, $taxonomies ) {
  122. if ( $this->model->is_translated_taxonomy( $taxonomies ) ) {
  123. foreach ( $terms as $term ) {
  124. $term_ids[] = is_object( $term ) ? $term->term_id : (int) $term;
  125. }
  126. }
  127. if ( ! empty( $term_ids ) ) {
  128. update_object_term_cache( array_unique( $term_ids ), 'term' ); // adds language and translation of terms to cache
  129. }
  130. return $terms;
  131. }
  132. /**
  133. * When terms are found for posts, add their language and translations to cache
  134. *
  135. * @since 1.2
  136. *
  137. * @param array $terms terms found
  138. * @param array $object_ids not used
  139. * @param array $taxonomies terms taxonomies
  140. * @return array unmodified $terms
  141. */
  142. public function wp_get_object_terms( $terms, $object_ids, $taxonomies ) {
  143. $taxonomies = explode( "', '", trim( $taxonomies, "'" ) );
  144. if ( ! in_array( 'term_translations', $taxonomies ) ) {
  145. $this->_prime_terms_cache( $terms, $taxonomies );
  146. }
  147. return $terms;
  148. }
  149. }