3 abstract class WPML_Slug_Translation_Records {
5 const CONTEXT_DEFAULT = 'default';
6 const CONTEXT_WORDPRESS = 'WordPress';
11 /** @var WPML_WP_Cache_Factory $cache_factory*/
12 private $cache_factory;
14 public function __construct( wpdb $wpdb, WPML_WP_Cache_Factory $cache_factory ) {
16 $this->cache_factory = $cache_factory;
22 * @return WPML_ST_Slug
24 public function get_slug( $type ) {
25 $cache_item = $this->cache_factory->create_cache_item( $this->get_cache_group(), $type );
27 if ( ! $cache_item->exists() ) {
28 $slug = new WPML_ST_Slug();
30 /** @var \stdClass $original */
31 $original = $this->wpdb->get_row(
33 "SELECT id, value, language, context, name
34 FROM {$this->wpdb->prefix}icl_strings
36 AND (context = %s OR context = %s)",
37 $this->get_string_name( $type ),
38 self::CONTEXT_DEFAULT,
39 self::CONTEXT_WORDPRESS
44 $slug->set_lang_data( $original );
46 /** @var array<\stdClass> $translations */
47 $translations = $this->wpdb->get_results(
49 "SELECT value, language, status
50 FROM {$this->wpdb->prefix}icl_string_translations
57 if ( $translations ) {
58 foreach ( $translations as $translation ) {
59 $slug->set_lang_data( $translation );
64 $cache_item->set( $slug );
68 return $cache_item->get();
72 private function get_cache_group() {
73 return __CLASS__ . '::' . $this->get_element_type();
76 private function flush_cache() {
77 $cache_group = $this->cache_factory->create_cache_group( $this->get_cache_group() );
78 $cache_group->flush_group_cache();
82 * @deprecated use `get_slug` instead.
89 public function get_translation( $type, $lang ) {
90 $slug = $this->get_slug( $type );
92 if ( $slug->is_translation_complete( $lang ) ) {
93 return $slug->get_value( $lang );
100 * @deprecated use `get_slug` instead.
102 * @param string $type
103 * @param string $lang
105 * @return null|string
107 public function get_original( $type, $lang = '' ) {
108 $slug = $this->get_slug( $type );
110 if ( ! $lang || $slug->get_original_lang() === $lang ) {
111 return $slug->get_original_value();
118 * @deprecated use `get_slug` instead.
120 * @param string $type
124 public function get_slug_id( $type ) {
125 $slug = $this->get_slug( $type );
127 if ( $slug->get_original_id() ) {
128 return $slug->get_original_id();
135 * @param string $type
136 * @param string $slug
140 public function register_slug( $type, $slug ) {
141 $string_id = icl_register_string(
142 self::CONTEXT_WORDPRESS,
143 $this->get_string_name( $type ),
147 $this->flush_cache();
153 * @param string $type
154 * @param string $slug
156 public function update_original_slug( $type, $slug ) {
158 $this->wpdb->prefix . 'icl_strings',
159 array( 'value' => $slug ),
160 array( 'name' => $this->get_string_name( $type ) )
163 $this->flush_cache();
167 * @deprecated use `get_slug` instead.
169 * @param string $type
171 * @return null|stdClass
173 public function get_original_slug_and_lang( $type ) {
174 $original_slug_and_lang = null;
176 $slug = $this->get_slug( $type );
178 if ( $slug->get_original_id() ) {
179 $original_slug_and_lang = (object) array(
180 'value' => $slug->get_original_value(),
181 'language' => $slug->get_original_lang(),
185 return $original_slug_and_lang;
189 * @deprecated use `get_slug` instead.
191 * @param string $type
192 * @param bool $only_status_complete
196 public function get_element_slug_translations( $type, $only_status_complete = true ) {
197 $slug = $this->get_slug( $type );
201 foreach ( $slug->get_language_codes() as $lang ) {
202 if ( $slug->get_original_lang() === $lang
203 || ( $only_status_complete && ! $slug->is_translation_complete( $lang ) )
208 $rows[] = (object) array(
209 'value' => $slug->get_value( $lang ),
211 'status' => $slug->get_status( $lang ),
219 * @deprecated use `get_slug` instead.
221 * @param array $types
225 public function get_all_slug_translations( $types ) {
228 foreach ( $types as $type ) {
229 $slug = $this->get_slug( $type );
231 foreach ( $slug->get_language_codes() as $lang ) {
232 if ( $slug->get_original_lang() !== $lang ) {
233 $rows[] = (object) array(
234 'value' => $slug->get_value( $lang ),
235 'name' => $slug->get_name(),
245 * @deprecated use `get_slug` instead.
247 * @param string $type
251 public function get_slug_translation_languages( $type ) {
252 $languages = array();
253 $slug = $this->get_slug( $type );
255 foreach ( $slug->get_language_codes() as $lang ) {
256 if ( $slug->is_translation_complete( $lang ) ) {
257 $languages[] = $lang;
265 * Use `WPML_ST_String` only for updating the values in the DB
266 * because it does not have any caching feature.
268 * @param string $type
270 * @return null|WPML_ST_String
272 public function get_slug_string( $type ) {
273 $string_id = $this->get_slug_id( $type );
276 return new WPML_ST_String( $string_id, $this->wpdb );
283 * @param string $slug
287 abstract protected function get_string_name( $slug );
289 /** @return string */
290 abstract protected function get_element_type();