Skip to content

Commit

Permalink
feat: store language in metadata (#316)
Browse files Browse the repository at this point in the history
* feat: add language meta field
* feat: show language meta in admin columns
* chore(docs): add readme for convert-language
  • Loading branch information
Ned Zimmerman authored Mar 11, 2020
1 parent 0eebd9f commit ab499a8
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 15 deletions.
16 changes: 14 additions & 2 deletions bin/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# Convert Metadata from Custom Metaboxes 2 to Advanced Custom Fields
# Conversion Scripts

## Convert Metadata from Custom Metaboxes 2 to Advanced Custom Fields

To convert your resource library's metadata from Custom Metaboxes 2 to Advanced Custom Fields, run the following command from the WordPress root using [wp-cli](https://wp-cli.org):

```bash
wp eval-file wp-content/plugins/coop-library-framework/bin/convert-meta.php`
wp eval-file wp-content/plugins/coop-library-framework/bin/convert-meta.php
```

In order to see the updated metadata on the resource library front-end, you'll need to update to the [1.0.0-alpha.2](https://github.com/platform-coop-toolkit/coop-library/milestones/1.0.0-alpha.2) version of the Co-op Library theme.

## Convert Resource Language from Polylang to Advanced Custom Fields

To convert your resources' language from a Polylang language to a metadata field managed through Advanced Custom Fields, run the following command from the WordPress root using [wp-cli](https://wp-cli.org):

```bash
wp eval-file wp-content/plugins/coop-library-framework/bin/convert-language.php
```

In order to see the updated language on the resource library front-end, you'll need to update to the [1.0.0-alpha.2](https://github.com/platform-coop-toolkit/coop-library/milestones/1.0.0-alpha.2) version of the Co-op Library theme.
25 changes: 25 additions & 0 deletions bin/convert-language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Convert languages from Polylang taxonomy to new (1.0.0-alpha.3) format.
*
* @package CoopLibraryFramework
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

$resources = get_posts(
[
'fields' => 'ids',
'posts_per_page' => -1,
'post_type' => 'lc_resource',
'post_status' => 'all',
'lang' => '',
]
);

foreach ( $resources as $post_id ) {
update_post_meta( $post_id, 'language', pll_get_post_language( $post_id ) );
update_post_meta( $post_id, '_language', 'field_5e62c939ddcb6' );
}
137 changes: 136 additions & 1 deletion includes/functions/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use \WP_Error as WP_Error;

use function CoopLibraryFramework\Internationalization\get_localized_language;

/**
* Default setup routine
*
Expand Down Expand Up @@ -39,6 +41,14 @@ function setup() {
add_filter( 'mce_css', $n( 'mce_css' ) );
// Hook to allow async or defer on asset loading.
add_filter( 'script_loader_tag', $n( 'script_loader_tag' ), 10, 2 );
// Ensure resources and topics are translatable.
add_filter( 'pll_get_post_types', $n( 'add_resource_to_pll' ), 10, 2 ); // TODO: Remove this.
add_filter( 'pll_get_taxonomies', $n( 'add_coop_type_to_pll' ), 10, 2 );
add_filter( 'pll_get_taxonomies', $n( 'add_sector_to_pll' ), 10, 2 );
add_filter( 'pll_get_taxonomies', $n( 'add_region_to_pll' ), 10, 2 );
add_filter( 'pll_get_taxonomies', $n( 'add_topic_to_pll' ), 10, 2 );
add_filter( 'pll_get_taxonomies', $n( 'add_goal_to_pll' ), 10, 2 );
add_filter( 'pll_get_taxonomies', $n( 'add_format_to_pll' ), 10, 2 );

// Disable inaccessible sortable JavaScript for term order.
add_filter( 'wp_fancy_term_order', '__return_false' );
Expand Down Expand Up @@ -70,7 +80,10 @@ function resource_init() {
'title',
'language' => [
'title' => __( 'Language', 'coop-library-framework' ),
'taxonomy' => 'language',
'meta_key' => 'language',
'function' => function() {
echo esc_attr( get_localized_language( get_post_meta( get_the_ID(), 'language', true ) ) );
},
],
'format' => [
'title' => __( 'Format', 'coop-library-framework' ),
Expand All @@ -87,6 +100,7 @@ function resource_init() {
'added' => [
'title' => __( 'Date Added', 'coop-library-framework' ),
'post_field' => 'post_date',
'default' => 'DESC',
],
],
'labels' => [
Expand Down Expand Up @@ -129,6 +143,23 @@ function resource_init() {
);
}

/**
* Add the `lc_resource` post type to Polylang, ensuring it is translatable.
*
* @param array $post_types An array of post types.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_resource_to_pll( $post_types, $is_settings ) {
if ( $is_settings ) {
unset( $post_types['lc_resource'] );
} else {
$post_types['lc_resource'] = 'lc_resource';
}
return $post_types;
}

/**
* Registers the `lc_topic` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -177,6 +208,23 @@ function topic_init() {
);
}

/**
* Add the `lc_topic` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_topic_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_topic'] );
} else {
$taxonomies['lc_topic'] = 'lc_topic';
}
return $taxonomies;
}

/**
* Registers the `lc_goal` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -225,6 +273,24 @@ function goal_init() {
);
}

/**
* Add the `lc_goal` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_goal_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_goal'] );
} else {
$taxonomies['lc_goal'] = 'lc_goal';
}
return $taxonomies;
}


/**
* Registers the `lc_region` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -273,6 +339,24 @@ function region_init() {
);
}

/**
* Add the `lc_region` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_region_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_region'] );
} else {
$taxonomies['lc_region'] = 'lc_region';
}
return $taxonomies;
}


/**
* Registers the `lc_sector` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -321,6 +405,23 @@ function sector_init() {
);
}

/**
* Add the `lc_sector` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_sector_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_sector'] );
} else {
$taxonomies['lc_sector'] = 'lc_sector';
}
return $taxonomies;
}

/**
* Registers the `lc_sector` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -369,6 +470,23 @@ function coop_type_init() {
);
}

/**
* Add the `lc_coop_type` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_coop_type_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_coop_type'] );
} else {
$taxonomies['lc_coop_type'] = 'lc_coop_type';
}
return $taxonomies;
}

/**
* Registers the `lc_format` taxonomy,
* for use with 'lc_resource'.
Expand Down Expand Up @@ -417,6 +535,23 @@ function format_init() {
);
}

/**
* Add the `lc_format` taxonomy to Polylang, ensuring it is translatable.
*
* @param array $taxonomies An array of taxonomies.
* @param bool $is_settings Whether or not we are on the settings page.
*
* @return array
*/
function add_format_to_pll( $taxonomies, $is_settings ) {
if ( $is_settings ) {
unset( $taxonomies['lc_format'] );
} else {
$taxonomies['lc_format'] = 'lc_format';
}
return $taxonomies;
}

/**
* Initializes the plugin and fires an action other plugins can hook into.
*
Expand Down
52 changes: 48 additions & 4 deletions includes/functions/internationalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ function copy_post_metas( $metas ) {
*
* @return array $countries An associative array of countries.
*/
function get_country_list( $locale = 'en_US' ) {
$countries = include_once COOP_LIBRARY_FRAMEWORK_PATH . 'vendor/umpirsky/country-list/data/' . $locale . '/country.php';
function get_country_list( $locale = false ) {
if ( ! $locale ) {
$locale = get_locale();
}
$countries = include COOP_LIBRARY_FRAMEWORK_PATH . 'vendor/umpirsky/country-list/data/' . $locale . '/country.php';
return $countries;
}

Expand All @@ -59,7 +62,48 @@ function get_country_list( $locale = 'en_US' ) {
*
* @return array $languages An associative array of languages.
*/
function get_language_list( $locale = 'en_US' ) {
$languages = include_once COOP_LIBRARY_FRAMEWORK_PATH . 'vendor/umpirsky/language-list/data/' . $locale . '/language.php';
function get_language_list( $locale = false ) {
if ( ! $locale ) {
$locale = get_locale();
}
$languages = include COOP_LIBRARY_FRAMEWORK_PATH . 'vendor/umpirsky/language-list/data/' . $locale . '/language.php';
return $languages;
}

/**
* Get language choices for ACF based on what languages are set in Polylang.
*
* @param string $locale The current locale, for example en_US.
*
* @return array $choices
*/
function get_language_choices( $locale = false ) {
if ( ! $locale ) {
$locale = get_locale();
}
$locales = ( function_exists( 'pll_languages_list' ) ) ? pll_languages_list( [ 'fields' => 'slug' ] ) : [ 'en' ];
$languages = get_language_list( $locale );
$choices = [];
foreach ( $locales as $key ) {
$choices[ $key ] = $languages[ $key ];
}
asort( $choices );
return $choices;
}

/**
* Get language choices for ACF based on what languages are set in Polylang.
*
* @param string $slug The language slug to look up.
* @param string $locale The current locale, for example en_US.
*
* @return array $choices
*/
function get_localized_language( $slug, $locale = false ) {
if ( ! $locale ) {
$locale = get_locale();
}
$languages = get_language_list( $locale );

return $languages[ $slug ];
}
Loading

0 comments on commit ab499a8

Please sign in to comment.