Skip to content

Instantly share code, notes, and snippets.

@kyletaylored
Created March 23, 2022 21:25
Show Gist options
  • Save kyletaylored/eba2ca47ec03c981c6569762db6d54b3 to your computer and use it in GitHub Desktop.
Save kyletaylored/eba2ca47ec03c981c6569762db6d54b3 to your computer and use it in GitHub Desktop.
Fetch WPML languages through WP CLI
<?php
/*
Plugin Name: WPML CLI
Plugin URI: https://pantheon.io
Description: WPML CLI demo commands
Author: Kyle Taylor
Version: 1.0.0
Author URI: https://github.com/kyletaylored
*/
/**
* WPML commands for demonstration.
*/
class WPML_CLI {
/**
* Return active languages
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
*/
public function get_languages($args, $assoc_args) {
$languages = apply_filters( 'wpml_active_languages', NULL, 'orderby=id&order=desc' );
$active_count = 0;
$active_lang = [];
foreach($languages as $key => $language) {
if ($language['active'] == 1) {
$active_count++;
$active_lang[] = $language['code'];
} else {
// Filter out inactive languages.
if (!empty($assoc_args['active'])) {
unset($languages[$key]);
}
}
}
// Format output
$data = [
'active_count' => $active_count,
'active' => $active_lang,
'languages' => $languages
];
$languages = json_encode($data, JSON_PRETTY_PRINT);
WP_CLI::log( $languages );
}
}
/**
* Registers our command when cli get's initialized.
*
* @since 1.0.0
* @author Scott Anderson
*/
function wpml_cli_register_commands() {
WP_CLI::add_command( 'wpml', 'WPML_CLI' );
}
add_action( 'cli_init', 'wpml_cli_register_commands' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment