Making WordPress.org


Ignore:
Timestamp:
06/20/2019 01:15:50 PM (4 years ago)
Author:
ocean90
Message:

Translate, Discussion: Retrieve REST API URL by locale.

See #4518.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-discussions/inc/class-plugin.php

    r8969 r8972  
    3232        add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
    3333    }
     34
     35    /**
     36     * Initializes the plugin.
     37     */
     38    public function plugins_loaded() {
     39        // Temporarily hide for public while in development.
     40        if ( ! is_caped() ) {
     41            return;
     42        }
     43
     44        add_action( 'wporg_translate_meta', [ $this, 'show_discussion_meta' ] );
     45    }
     46
     47    /**
     48     * Adds discussion items.
     49     *
     50     * @param object $entry Current translation row entry.
     51     */
     52    public function show_discussion_meta( $entry ) {
     53        $set     = GP::$translation_set->get( $entry->translation_set_id );
     54        $blog_id = $this->get_blog_id($set->locale  );
     55        if ( ! $blog_id ) {
     56            return;
     57        }
     58
     59        $rest_url = get_rest_url( $blog_id );
     60
     61
     62        echo '📝';
     63    }
     64
     65    /**
     66     * Returns the blog ID of a locale.
     67     *
     68     * @param string $locale_slug Slug of GlotPress locale.
     69     * @return int Blog ID on success, 0 on failure.
     70     */
     71    public function get_blog_id( $locale_slug ) {
     72        $gp_locale = GP_Locales::by_slug( $locale_slug );
     73        if ( ! $gp_locale || ! isset( $gp_locale->wp_locale ) ) {
     74            return false;
     75        }
     76
     77        $wp_locale = $gp_locale->wp_locale;
     78
     79        $result = get_sites( [
     80            'network_id' => get_current_network_id(),
     81            'path'       => '/support/',
     82            'number'     => 1,
     83            'locale'    => $wp_locale,
     84        ] );
     85        $site = array_shift( $result );
     86
     87        return $site ? (int) $site->blog_id : 0;
     88    }
    3489}
     90
Note: See TracChangeset for help on using the changeset viewer.