links = &$rest_api->links; foreach ( $content_types as $type => $args ) { $args = wp_parse_args( $args, array_fill_keys( array( 'lang', 'translations' ), true ) ); if ( $args['lang'] ) { register_rest_field( $this->get_rest_field_type( $type ), 'lang', array( 'get_callback' => array( $this, 'get_language' ), 'update_callback' => array( $this, 'set_language' ), 'schema' => array( 'lang' => __( 'Language', 'polylang-pro' ), 'type' => 'string', ), ) ); } if ( $args['translations'] ) { register_rest_field( $this->get_rest_field_type( $type ), 'translations', array( 'get_callback' => array( $this, 'get_translations' ), 'update_callback' => array( $this, 'save_translations' ), 'schema' => array( 'translations' => __( 'Translations', 'polylang-pro' ), 'type' => 'object', ), ) ); } } } /** * Returns the object language. * * @since 2.2 * * @param array $object Post or Term array. * @return string|false Language slug. False if no language is assigned to the object. */ public function get_language( $object ) { $language = $this->model->{$this->type}->get_language( $object[ $this->getter_id_name ] ); return empty( $language ) ? false : $language->slug; } /** * Sets the object language. * * @since 2.2 * * @param string $lang Language code. * @param object $object Instance of WP_Post or WP_Term. * @return bool */ public function set_language( $lang, $object ) { if ( isset( $object->{$this->setter_id_name} ) ) { // Test to avoid a warning with WooCommerce $this->model->{$this->type}->set_language( $object->{$this->setter_id_name}, $lang ); } return true; } /** * Returns the object translations. * * @since 2.2 * * @param array $object Post or Term array. * @return array */ public function get_translations( $object ) { return $this->model->{$this->type}->get_translations( $object[ $this->getter_id_name ] ); } /** * Save translations. * * @since 2.2 * * @param array $translations Array of translations with language codes as keys and object ids as values. * @param object $object Instance of WP_Post or WP_Term. * @return bool */ public function save_translations( $translations, $object ) { if ( isset( $object->{$this->setter_id_name} ) ) { // Test to avoid a warning with WooCommerce. $this->model->{$this->type}->save_translations( $object->{$this->setter_id_name}, $translations ); } return true; } }