Added localization, WPML ready

This commit is contained in:
Robbert de Kuiper 2017-03-31 14:06:13 +02:00
parent d1769f2f9c
commit a995fbeb6b
9 changed files with 214 additions and 47 deletions

BIN
ajax-filter-posts-nl_NL.mo Normal file

Binary file not shown.

View File

@ -0,0 +1,65 @@
# Copyright (C) 2014 ...
# This file is distributed under the GNU General Public License v2 or later.
msgid ""
msgstr ""
"Project-Id-Version: ajax-filter-posts\n"
"POT-Creation-Date: 2017-03-31 12:12+0200\n"
"PO-Revision-Date: \n"
"Report-Msgid-Bugs-To: Robbert de Kuiper <mail@robbertdekuiper.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Textdomain-Support: yesX-Generator: Poedit 1.6.4\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;esc_html_e;esc_html_x:1,2c;esc_html__;esc_attr_e;esc_attr_x:1,2c;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2\n"
"X-Poedit-Basepath: ..\n"
"Language-Team: \n"
"X-Generator: Poedit 2.0\n"
"Last-Translator: \n"
"Language: nl\n"
"X-Poedit-SearchPath-0: .\n"
#: class-ajax-filter-posts.php:78
msgid "It took to long the get the posts. Please reload the page and try again."
msgstr "Het laden van de berichten duurde te lang. Herlaad de pagina en probeer het nog een keer."
#: class-ajax-filter-posts.php:79
msgid "Got no response. Please reload the page and try again."
msgstr "Geen reactie van de server ontvangen. Herlaad de pagina en probeer het nog een keer."
#: class-ajax-filter-posts.php:172
msgid "Oops, something went wrong. Please reload the page and try again."
msgstr "Oeps, er ging iets verkeerd. Herlaad de pagina en probeer het nog een keer."
#: templates/base.php:4
#, php-format
msgid "Filter %s"
msgstr "Filter %s"
#: templates/base.php:5
#, php-format
msgid "Show %s"
msgstr "Toon %s"
#: templates/base.php:6
msgid "Hide filters"
msgstr "Verberg filters"
#: templates/base.php:17
msgid "Loading"
msgstr "Laden"
#: templates/partials/loop.php:14
msgid "Load more"
msgstr "Meer laden"
#: templates/partials/loop.php:21
#, php-format
msgid "Oh, we couldn't find any %s"
msgstr "Oh, we kunnen geen %s vinden"
#: templates/partials/loop.php:22
#, php-format
msgid "Try different filters or <a %s> reset them all.</a>"
msgstr "Probeer andere filters uit of <a %s> herstel ze allemaal.</a>"

View File

@ -16,7 +16,7 @@
* Plugin Name: Ajax filter posts
* Plugin URI: http://www.robbertdekuiper.com
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area.
* Version: 0.1
* Version: 0.2.0
* Author: Robbert de Kuiper
* Author URI: http://www.robbertdekuiper.com
* License: GPL-2.0+

View File

@ -120,6 +120,10 @@
border: 0;
}
.ajax-posts__load-more {
flex: 0 0 100%;
}
/* On small screen we can't see the posts when the filer is visible */
@media screen and (max-width: 34rem) {
.ajax-posts.is-expanded-filters .ajax-posts__show-recipes-text {

View File

@ -1,37 +1,36 @@
(function (){
var queryParams = null;
var container = document.querySelector('.js-container-async');
var filterTogglers = container.getElementsByClassName('js-toggle-filters');
var content = container.querySelector('.ajax-posts__posts');
var status = container.querySelector('.ajax-posts__status');
var queryParams = {
'page' : null,
'tax' : {},
'quantity' : 0,
'postType': 'post',
}
if (container) {
var filterTogglers = container.getElementsByClassName('js-toggle-filters');
var content = container.querySelector('.ajax-posts__posts');
var status = container.querySelector('.ajax-posts__status');
}
function init() {
if (container) {
addEventListeners();
setDefaults();
}
}
/**
* Set initial params, and add event listeners
* Add event listeners
*/
function init() {
if (container) {
// Set the amoun of post per page
queryParams.quantity = parseInt(container.dataset.quantity, 10);
queryParams.postType = container.dataset.postType;
// Add event listeners with event propagination
on(container,'click', 'a[data-filter]', function(event) {
handleFilterEvent(event.target);
event.preventDefault();
return true;
function addEventListeners() {
// Add event listeners with event propagination
on(container,'click', 'a[data-filter]', function(event) {
handleFilterEvent(event.target);
event.preventDefault();
return true;
});
on(container, 'click', '.js-load-more', function(event) {
handleLoadMoreEvent(event.target);
event.preventDefault();
return true;
});
handleLoadMoreEvent(event.target);
event.preventDefault();
return true;
});
on(container, 'click', '.js-reset-filters', function() {
resetFilters();
@ -43,8 +42,20 @@
[].slice.call(filterTogglers).forEach(function(button){
button.addEventListener('click', toggleFilters)
});
}
}
}
/**
* Set default query variables
*/
function setDefaults() {
queryParams = {
'page' : null,
'tax' : {},
'quantity': parseInt(container.dataset.quantity, 10) || 0,
'postType': container.dataset.postType || 'post',
'language': filterPosts.language || null,
};
}
/**
* Update query and get posts after filter change
@ -169,10 +180,12 @@
request.onload = function() {
console.log(this.response);
//remove load more button
var loadMoreButton = content.querySelector('.js-load-more');
if (loadMoreButton) {
content.removeChild(loadMoreButton);
content.removeChild(loadMoreButton.parentNode);
}
var response = JSON.parse(this.response);
if (this.status === 200) {

View File

@ -7,7 +7,7 @@
* public-facing side of the site and the admin area.
*
* @link http://www.robbertdekuiper.com
* @since 0.1
* @since 0.1.0
*
* @package Ajax_Filter_Posts
*/
@ -20,7 +20,7 @@
* Also maintains the unique identifier of this plugin as well as the current
* version of the plugin.
*
* @since 0.1
* @since 0.1.0
* @package Ajax_Filter_Posts
* @author Robbert de Kuiper <mail@robbertdekuiper.com>
*/
@ -40,6 +40,7 @@ class Ajax_Filter_Posts {
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
@ -50,7 +51,8 @@ class Ajax_Filter_Posts {
public function __construct() {
$this->plugin_name = 'ajax-filter-posts';
$this->version = '0.1';
$this->version = '0.2.0';
add_action( 'plugins_loaded', [$this, 'load_textdomain'] );
add_action( 'wp_enqueue_scripts', [$this,'add_scripts'] );
add_action('wp_ajax_process_filter_change', [$this, 'process_filter_change']);
@ -64,20 +66,29 @@ class Ajax_Filter_Posts {
public function load_textdomain() {
load_muplugin_textdomain( 'ajax-filter-posts', basename( dirname( __FILE__ )) . '/languages' );
}
/**
* Load the required assets for this plugin.
*
*/
public function add_scripts() {
public function add_scripts() {
$script_variables = [
'nonce' => wp_create_nonce( 'filter-posts-nonce' ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'timeoutMessage' => __('It took to long the get the posts. Please reload the page and try again.', 'ajax-filter-posts'),
'serverErrorMessage' => __('Got no response. Please reload the page and try again.', 'ajax-filter-posts'),
];
// IF WPML is installed add language variable to set variable later during the query
// WPML can't figure out which language to query, when posts are loaded via AJAX.
if (ICL_LANGUAGE_CODE) {
$script_variables['language'] = ICL_LANGUAGE_CODE;
}
wp_enqueue_script( 'ajax-filter', plugins_url('/assets/js/ajax-filter-posts.js', __FILE__), [], '', true );
wp_enqueue_style( 'ajax-filter', plugins_url('/assets/css/ajax-filter-posts.css', __FILE__), []);
wp_localize_script( 'ajax-filter', 'filterPosts', array(
'nonce' => wp_create_nonce( 'filter-posts-nonce' ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'timeoutMessage' => __('It took to long the get the posts. Please reload the page and try again.', 'ajax-filter-posts'),
'serverErrorMessage' => __('Got no response. Please reload the page and try again.', 'ajax-filter-posts'),
)
);
wp_localize_script( 'ajax-filter', 'filterPosts', $script_variables);
}
/**
@ -155,6 +166,7 @@ class Ajax_Filter_Posts {
$tax = $this->get_tax_query_vars($_POST['params']['tax']);
$page = intval($_POST['params']['page']);
$quantity = intval($_POST['params']['quantity']);
$language = sanitize_text_field($_POST['params']['language']);
$args = [
'paged' => $page,
@ -163,7 +175,7 @@ class Ajax_Filter_Posts {
'tax_query' => $tax
];
$response = $this->get_filter_posts($args);
$response = $this->get_filter_posts($args, $language);
if ($response) {
wp_send_json_success($response);
@ -191,7 +203,7 @@ class Ajax_Filter_Posts {
* @return Boolean true if is last page
*/
private function is_last_page($query) {
return $query->get( 'paged' ) >= $query->max_num_pages;
return $this->get_page_number($query) >= $query->max_num_pages;
}
/**
@ -251,8 +263,14 @@ class Ajax_Filter_Posts {
* @param array $args Arguments for the WordPress Query
* @return string HTMl to be sent via Ajax
*/
public function get_filter_posts($args) {
public function get_filter_posts($args, $language) {
if (!empty($language)) {
global $sitepress;
$sitepress->switch_lang( $language );
}
$query = new WP_Query($args);
$response = [];

Binary file not shown.

View File

@ -0,0 +1,65 @@
# Copyright (C) 2014 ...
# This file is distributed under the GNU General Public License v2 or later.
msgid ""
msgstr ""
"Project-Id-Version: ajax-filter-posts\n"
"POT-Creation-Date: 2017-03-31 12:12+0200\n"
"PO-Revision-Date: \n"
"Report-Msgid-Bugs-To: Robbert de Kuiper <mail@robbertdekuiper.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Textdomain-Support: yesX-Generator: Poedit 1.6.4\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: __;_e;esc_html_e;esc_html_x:1,2c;esc_html__;esc_attr_e;esc_attr_x:1,2c;esc_attr__;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;_x:1,2c;_n:1,2;_n_noop:1,2;__ngettext:1,2;__ngettext_noop:1,2;_c,_nc:4c,1,2\n"
"X-Poedit-Basepath: ..\n"
"Language-Team: \n"
"X-Generator: Poedit 2.0\n"
"Last-Translator: \n"
"Language: nl\n"
"X-Poedit-SearchPath-0: .\n"
#: class-ajax-filter-posts.php:78
msgid "It took to long the get the posts. Please reload the page and try again."
msgstr "Het laden van de berichten duurde te lang. Herlaad de pagina en probeer het nog een keer."
#: class-ajax-filter-posts.php:79
msgid "Got no response. Please reload the page and try again."
msgstr "Geen reactie van de server ontvangen. Herlaad de pagina en probeer het nog een keer."
#: class-ajax-filter-posts.php:172
msgid "Oops, something went wrong. Please reload the page and try again."
msgstr "Oeps, er ging iets verkeerd. Herlaad de pagina en probeer het nog een keer."
#: templates/base.php:4
#, php-format
msgid "Filter %s"
msgstr "Filter %s"
#: templates/base.php:5
#, php-format
msgid "Show %s"
msgstr "Toon %s"
#: templates/base.php:6
msgid "Hide filters"
msgstr "Verberg filters"
#: templates/base.php:17
msgid "Loading"
msgstr "Laden"
#: templates/partials/loop.php:14
msgid "Load more"
msgstr "Meer laden"
#: templates/partials/loop.php:21
#, php-format
msgid "Oh, we couldn't find any %s"
msgstr "Oh, we kunnen geen %s vinden"
#: templates/partials/loop.php:22
#, php-format
msgid "Try different filters or <a %s> reset them all.</a>"
msgstr "Probeer andere filters uit of <a %s> herstel ze allemaal.</a>"

View File

@ -10,9 +10,11 @@
</div>
<?php endwhile; ?>
<?php if (!$this->is_last_page($query)) : ?>
<button class="js-load-more" data-page="<?= $this->get_page_number($query) + 1; ?>">
<?php _e('Load more', 'ajax-filter-posts') ?>
</button>
<div class="ajax-posts__load-more">
<button class="js-load-more" data-page="<?= $this->get_page_number($query) + 1; ?>">
<?php _e('Load more', 'ajax-filter-posts') ?>
</button>
</div>
<?php endif; ?>
<?php else: ?>
<div class="ajax-posts-message ajax-posts-message--empty">