| 1 |
<?php |
| 2 |
|
| 3 |
namespace Barn2\Plugin\Posts_Table_Search_Sort; |
| 4 |
|
| 5 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Plugin\Plugin; |
| 6 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Registerable; |
| 7 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Service; |
| 8 |
use Barn2\Plugin\Posts_Table_Search_Sort\Dependencies\Lib\Util; |
| 9 |
|
| 10 |
/** |
| 11 |
* Registers the frontend styles and scripts for the post tables. |
| 12 |
* |
| 13 |
* @package Barn2\posts-table-search-sort |
| 14 |
* @author Barn2 Plugins <support@barn2.com> |
| 15 |
* @license GPL-3.0 |
| 16 |
* @copyright Barn2 Media Ltd |
| 17 |
*/ |
| 18 |
class Frontend_Scripts implements Registerable, Service { |
| 19 |
|
| 20 |
const DATATABLES_VERSION = '1.11.3'; |
| 21 |
|
| 22 |
private $plugin; |
| 23 |
|
| 24 |
public function __construct( Plugin $plugin ) { |
| 25 |
$this->plugin = $plugin; |
| 26 |
} |
| 27 |
|
| 28 |
public function register() { |
| 29 |
add_action( 'wp_enqueue_scripts', [ $this, 'register_styles' ] ); |
| 30 |
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] ); |
| 31 |
} |
| 32 |
|
| 33 |
public function register_styles() { |
| 34 |
$suffix = Util::get_script_suffix(); |
| 35 |
|
| 36 |
wp_register_style( 'jquery-datatables-pss', plugins_url( 'assets/js/datatables/datatables.min.css', $this->plugin->get_file() ), [], self::DATATABLES_VERSION ); |
| 37 |
wp_register_style( 'posts-data-table', plugins_url( "assets/css/posts-data-table-main.css", $this->plugin->get_file() ), [ 'jquery-datatables-pss' ], $this->plugin->get_version() ); |
| 38 |
} |
| 39 |
|
| 40 |
public function register_scripts() { |
| 41 |
$suffix = Util::get_script_suffix(); |
| 42 |
|
| 43 |
wp_register_script( 'jquery-datatables-pss', plugins_url( "assets/js/datatables/datatables{$suffix}.js", $this->plugin->get_file() ), [ 'jquery' ], self::DATATABLES_VERSION, true ); |
| 44 |
wp_register_script( 'posts-data-table', plugins_url( "assets/js/posts-data-table-main.js", $this->plugin->get_file() ), [ 'jquery', 'jquery-datatables-pss' ], $this->plugin->get_version(), true ); |
| 45 |
|
| 46 |
$locale = get_locale(); |
| 47 |
$supported_locales = $this->get_supported_locales(); |
| 48 |
|
| 49 |
// Add language file to script if locale is supported (English file is not added as this is the default language) |
| 50 |
if ( array_key_exists( $locale, $supported_locales ) ) { |
| 51 |
wp_localize_script( |
| 52 |
'posts-data-table', |
| 53 |
'posts_data_table', |
| 54 |
[ |
| 55 |
'langurl' => $supported_locales[ $locale ] |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns an array of locales supported by the plugin. |
| 63 |
* The array returned uses the locale as the array key mapped to the URL of the corresponding translation file. |
| 64 |
* |
| 65 |
* @return array The supported locales |
| 66 |
*/ |
| 67 |
private function get_supported_locales() { |
| 68 |
$lang_file_base_url = plugins_url( 'languages/data-tables/', $this->plugin->get_file() ); |
| 69 |
|
| 70 |
return apply_filters( |
| 71 |
'posts_data_table_supported_languages', |
| 72 |
[ |
| 73 |
'es_ES' => $lang_file_base_url . 'Spanish.json', |
| 74 |
'fr_FR' => $lang_file_base_url . 'French.json', |
| 75 |
'fr_BE' => $lang_file_base_url . 'French.json', |
| 76 |
'fr_CA' => $lang_file_base_url . 'French.json', |
| 77 |
'de_DE' => $lang_file_base_url . 'German.json', |
| 78 |
'de_CH' => $lang_file_base_url . 'German.json', |
| 79 |
'el' => $lang_file_base_url . 'Greek.json', |
| 80 |
'el_EL' => $lang_file_base_url . 'Greek.json', |
| 81 |
'zh_TW' => $lang_file_base_url . 'Taiwan.json', |
| 82 |
] |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|