mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
Compare commits
119 Commits
Author | SHA1 | Date | |
---|---|---|---|
5c1a186456 | |||
7a0075e5ed | |||
392d552dab | |||
797a08cfb6 | |||
c5279a37f3 | |||
99e58d0382 | |||
dc1949ba4c | |||
3269ccca1a | |||
2d91350cc9 | |||
e7fd47a346 | |||
8d4e81878e | |||
418dc829d0 | |||
a75d3a3915 | |||
d2fb67b5bc | |||
a9f5205d78 | |||
25c76e4998 | |||
6157cf3988 | |||
8644a7103c | |||
4fd516bcf8 | |||
238fdcf261 | |||
eaf1ffa0c8 | |||
48477a158e | |||
9b4778b494 | |||
60363d8a5c | |||
51ae9b9616 | |||
637142d00d | |||
163d0d27a6 | |||
d0ada74642 | |||
b242c33733 | |||
cac233ec45 | |||
9e394ac837 | |||
b2811ad169 | |||
40f351efbf | |||
ad0a9f77f8 | |||
76a2217582 | |||
742b16808c | |||
41419e6550 | |||
ab7e7274d0 | |||
b7742d3803 | |||
e5dc313f6c | |||
3b4c53da81 | |||
87434fc1c1 | |||
81c4759a9e | |||
ad5f1753cb | |||
6cde75b65c | |||
5a3550568c | |||
23390db0a9 | |||
4d5acc3714 | |||
72df79b092 | |||
d1be3b169d | |||
5af00fc85e | |||
f76653f05c | |||
995f6681e6 | |||
a6f0bd9584 | |||
59970db3b8 | |||
d2e3c55e1a | |||
99d97c5a5e | |||
64a61426e0 | |||
3f09fbb563 | |||
3360a9e7a7 | |||
c042851184 | |||
09a47029f3 | |||
977d7e57c6 | |||
e84a4cfc73 | |||
eb10341337 | |||
f769d5d8c1 | |||
f54126babc | |||
7dab9d71c6 | |||
0479deca56 | |||
c51610b054 | |||
367a1c97b2 | |||
01c5f19e39 | |||
42c2c945ba | |||
9311fb42bb | |||
ba3b069527 | |||
87a55e5302 | |||
dd42ebf712 | |||
0c6bf3d3a4 | |||
77b58ccf07 | |||
184d5627a3 | |||
ec12889815 | |||
4270145a55 | |||
3ed5129cd2 | |||
6dca75b2ce | |||
647629b5a9 | |||
fa91324f18 | |||
c47ed3385d | |||
740e59ea66 | |||
a287d93b9f | |||
688868cbe9 | |||
924aa095ff | |||
876c3e3840 | |||
d5419448e1 | |||
53d08e7174 | |||
d32320a540 | |||
acf86e42f5 | |||
35e29cf793 | |||
b0f4ea8d7a | |||
34aaaa7db9 | |||
80dd0c9c50 | |||
f8583423a4 | |||
86ed058363 | |||
2033cc7328 | |||
fad14485bd | |||
8d4735f138 | |||
2b9cde92ac | |||
51686efe93 | |||
78f7c693e8 | |||
632cdfdb0d | |||
af46fe974f | |||
4417ae78b6 | |||
b487013cac | |||
2725707296 | |||
0b7021942e | |||
0ed7a57a01 | |||
4abde347a4 | |||
036fd1da41 | |||
d1617fed00 | |||
fada60e0b8 |
@ -3,29 +3,50 @@
|
||||
"browser": true,
|
||||
"es2020": true
|
||||
},
|
||||
"globals": {
|
||||
"MOBILIZON_CONNECTOR": "readonly"
|
||||
},
|
||||
"parser": "@babel/eslint-parser",
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:ava/recommended"
|
||||
"plugin:ava/recommended",
|
||||
"plugin:@wordpress/eslint-plugin/recommended"
|
||||
],
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 11,
|
||||
"sourceType": "module"
|
||||
"requireConfigFile": false,
|
||||
"sourceType": "module",
|
||||
"babelOptions": {
|
||||
"presets": ["@babel/preset-react"]
|
||||
}
|
||||
},
|
||||
"plugins": [
|
||||
"ava"
|
||||
],
|
||||
"ignorePatterns": [
|
||||
"gulpfile.js"
|
||||
"ava",
|
||||
"jsx"
|
||||
],
|
||||
"rules": {
|
||||
"import/no-unresolved": [
|
||||
"off"
|
||||
],
|
||||
"indent": [
|
||||
"error",
|
||||
2
|
||||
],
|
||||
"no-console": [
|
||||
"error", {
|
||||
"allow": ["error"]
|
||||
}
|
||||
],
|
||||
"prettier/prettier": [
|
||||
"off"
|
||||
],
|
||||
"quotes": [
|
||||
"error",
|
||||
"single"
|
||||
],
|
||||
"react/jsx-key": [
|
||||
"off"
|
||||
],
|
||||
"semi": [
|
||||
"error",
|
||||
"never"
|
||||
|
58
.github/workflows/codeql-analysis.yml
vendored
58
.github/workflows/codeql-analysis.yml
vendored
@ -9,14 +9,14 @@
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "CodeQL"
|
||||
name: 'CodeQL'
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
schedule:
|
||||
- cron: '39 19 * * 4'
|
||||
|
||||
@ -32,40 +32,40 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'javascript' ]
|
||||
language: ['javascript']
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
||||
# Learn more:
|
||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v1
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v1
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v1
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
#- name: Autobuild
|
||||
# uses: github/codeql-action/autobuild@v1
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
||||
# and modify them (or add more) to build your code if your project
|
||||
# uses a compiled language
|
||||
|
||||
#- run: |
|
||||
# make bootstrap
|
||||
# make release
|
||||
- run: |
|
||||
npm install
|
||||
npm run build-prod
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
|
23
.github/workflows/test.yml
vendored
23
.github/workflows/test.yml
vendored
@ -5,25 +5,26 @@ name: Node.js CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [14.x]
|
||||
node-version: [18.x]
|
||||
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm ci
|
||||
- run: npm run build-prod
|
||||
- uses: actions/checkout@v2
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v2
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- name: Upgrade NPM
|
||||
run: npm install -g npm
|
||||
- run: npm ci --ignore-scripts
|
||||
- run: npm run build-prod
|
||||
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,3 +3,6 @@ build/
|
||||
coverage/
|
||||
node_modules/
|
||||
vendor/
|
||||
|
||||
.phpunit.cache
|
||||
.phpunit.result.cache
|
||||
|
5
.gulp.json
Normal file
5
.gulp.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"flags": {
|
||||
"gulpfile": "gulpfile.cjs"
|
||||
}
|
||||
}
|
2
.husky/pre-commit
Executable file
2
.husky/pre-commit
Executable file
@ -0,0 +1,2 @@
|
||||
npm test
|
||||
npx lint-staged
|
8
.prettierignore
Normal file
8
.prettierignore
Normal file
@ -0,0 +1,8 @@
|
||||
.idea
|
||||
build
|
||||
coverage
|
||||
node_modules
|
||||
vendor
|
||||
.eslintrc.json
|
||||
composer.lock
|
||||
LICENSE.md
|
4
.prettierrc.json
Normal file
4
.prettierrc.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"semi": false,
|
||||
"singleQuote": true
|
||||
}
|
29
README.md
29
README.md
@ -4,34 +4,47 @@ Connector for Mobilizon allows you to display the upcoming events of [Mobilizon]
|
||||
|
||||
More details can be found in the [WordPress Plugin Directory](https://wordpress.org/plugins/connector-mobilizon/).
|
||||
|
||||
The current changelog can be found under [source/changelog.txt](source/changelog.txt).
|
||||
|
||||
## Development
|
||||
|
||||
### Setup
|
||||
|
||||
1. Make sure `npm` and `composer` are installed.
|
||||
2. Run: `npm install`
|
||||
3. Run: `php composer.phar install`
|
||||
3. Run: `composer install`
|
||||
|
||||
### Development build
|
||||
|
||||
1. Build: `npm run build-dev`
|
||||
2. Make sure to keep `changelog.txt` up-to-date.
|
||||
|
||||
### Release procedure
|
||||
1. Make sure `changelog.txt` is up-to-date. Use a new version number and copy over the new section into `readme.txt`.
|
||||
2. Update `package.json` with the same version number.
|
||||
3. Update the `package-lock.json`: `npm i --package-lock-only`
|
||||
4. Build: `npm run build-prod`
|
||||
5. Determine minimum PHP version for code and update package.json if needed: `./vendor/bin/phpcompatinfo analyser:run ./source`
|
||||
|
||||
1. Make sure `changelog.txt` is up-to-date.
|
||||
2. Use a new version number and copy over the new section into `readme.txt`.
|
||||
3. Update `package.json` with the same version number.
|
||||
4. Update the `package-lock.json`: `npm i --package-lock-only`
|
||||
5. Build: `npm run build-prod`
|
||||
6. Make sure screenshots are up-to-date.
|
||||
7. Copy the built plugin into `/trunk` of SVN.
|
||||
8. Create a new tag of the new version: `svn cp trunk tags/<version>`
|
||||
9. Check the version number occurrences in both folders.
|
||||
10. Commit everything together to the release SVN: `svn ci -m "release version <version>"`
|
||||
10. Commit everything together to the release SVN: `svn ci -m "release version <version>"` Make sure to add new files beforehand.
|
||||
11. Commit the new version in git with the same message.
|
||||
12. Tag the new version: `git tag v<version>`
|
||||
13. Push the new tag to the repository: `git push --tags`
|
||||
14. Append `-next` to the version number in `package.json`.
|
||||
15. Update the `package-lock.json`: `npm i --package-lock-only`
|
||||
16. Commit: `git commit -am "prepare next release"`
|
||||
|
||||
### Other commands
|
||||
|
||||
- Run ESLint: `npm run eslint`
|
||||
- Run JavaScript code coverage with tests: `npm run coverage`
|
||||
- Run tests: `npm test`
|
||||
- Delete build folder: `gulp clean`
|
||||
- Delete build folder: `npm run clean`
|
||||
- Update PHP dependencies: `composer update`
|
||||
- Check for direct PHP dependency updates: `composer outdated --direct`
|
||||
- Format code with prettier: `npm run format`
|
||||
- Generate `vendor/autoload.php` file after creating new class: `composer dump-autoload`
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 60 KiB After Width: | Height: | Size: 76 KiB |
BIN
assets/screenshot-5.png
Normal file
BIN
assets/screenshot-5.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 81 KiB |
3
babel.config.json
Normal file
3
babel.config.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"presets": ["@babel/preset-env", "@babel/preset-react"]
|
||||
}
|
@ -1,5 +1,10 @@
|
||||
{
|
||||
"require": {
|
||||
"bartlett/php-compatinfo": "^5.4"
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"MobilizonConnector\\": "source/includes/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.6"
|
||||
}
|
||||
}
|
||||
|
4218
composer.lock
generated
4218
composer.lock
generated
File diff suppressed because it is too large
Load Diff
49
gulpfile.cjs
Normal file
49
gulpfile.cjs
Normal file
@ -0,0 +1,49 @@
|
||||
/* eslint-disable no-undef */
|
||||
const { dest, src } = require('gulp')
|
||||
|
||||
const replace = require('gulp-replace')
|
||||
|
||||
const PACKAGE = require('./package.json')
|
||||
|
||||
const FOLDER_BUILD = './build'
|
||||
|
||||
function injectMetadata() {
|
||||
return src(
|
||||
[
|
||||
FOLDER_BUILD + '/front/block-events-loader.js',
|
||||
FOLDER_BUILD + '/' + PACKAGE.name + '.php',
|
||||
FOLDER_BUILD + '/includes/Constants.php',
|
||||
FOLDER_BUILD + '/readme.txt',
|
||||
],
|
||||
{ base: './' },
|
||||
)
|
||||
.pipe(replace('<wordpress-author-name>', PACKAGE.author.name))
|
||||
.pipe(replace('<wordpress-author-url>', PACKAGE.author.url))
|
||||
.pipe(replace('<wordpress-description>', PACKAGE.description))
|
||||
.pipe(replace('<wordpress-donation-link>', PACKAGE.funding.url))
|
||||
.pipe(replace('<wordpress-license>', PACKAGE.license))
|
||||
.pipe(
|
||||
replace(
|
||||
'<wordpress-minimum-version>',
|
||||
PACKAGE.additionalDetails.wordpressMinimumVersion,
|
||||
),
|
||||
)
|
||||
.pipe(replace('<wordpress-name>', PACKAGE.name))
|
||||
.pipe(replace('<wordpress-nice-name>', PACKAGE.additionalDetails.niceName))
|
||||
.pipe(
|
||||
replace(
|
||||
'<wordpress-php-minimum-version>',
|
||||
PACKAGE.additionalDetails.phpMinimumVersion,
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
replace(
|
||||
'<wordpress-tested-up-to-version>',
|
||||
PACKAGE.additionalDetails.wordpressTestedUpToVersion,
|
||||
),
|
||||
)
|
||||
.pipe(replace('<wordpress-version>', PACKAGE.version))
|
||||
.pipe(dest('.'))
|
||||
}
|
||||
|
||||
exports.inject = injectMetadata
|
80
gulpfile.js
80
gulpfile.js
@ -1,80 +0,0 @@
|
||||
const { dest, series, src } = require('gulp');
|
||||
|
||||
const del = require('del');
|
||||
const replace = require('gulp-replace');
|
||||
const webpack = require('webpack-stream');
|
||||
|
||||
const PACKAGE = require('./package.json');
|
||||
|
||||
const FOLDER_SOURCE = './source'
|
||||
const FOLDER_BUILD = './build';
|
||||
|
||||
let mode = 'development';
|
||||
|
||||
|
||||
function clean(cb) {
|
||||
del(FOLDER_BUILD);
|
||||
cb();
|
||||
}
|
||||
|
||||
const eventsLoaderOutputPath = PACKAGE.name + '/front/events-loader';
|
||||
const eventsLoaderInputPath = FOLDER_SOURCE + '/' + PACKAGE.name + '/front/events-loader.js';
|
||||
|
||||
function bundleFrontend() {
|
||||
return src(FOLDER_SOURCE + '/' + PACKAGE.name + '/front/events-loader.js')
|
||||
.pipe(webpack({
|
||||
mode,
|
||||
entry: {
|
||||
[eventsLoaderOutputPath]: eventsLoaderInputPath,
|
||||
},
|
||||
output: {
|
||||
filename: '[name].js',
|
||||
},
|
||||
}))
|
||||
.pipe(dest(FOLDER_BUILD));
|
||||
}
|
||||
|
||||
function copyBackend() {
|
||||
return src([
|
||||
FOLDER_SOURCE + '/**/*.php',
|
||||
FOLDER_SOURCE + '/**/*.txt'
|
||||
])
|
||||
.pipe(dest(FOLDER_BUILD));
|
||||
}
|
||||
|
||||
function injectMetadata() {
|
||||
return src([
|
||||
FOLDER_BUILD + '/' + eventsLoaderOutputPath + '.js',
|
||||
FOLDER_BUILD + '/' + PACKAGE.name + '/' + PACKAGE.name + '.php',
|
||||
FOLDER_BUILD + '/' + PACKAGE.name + '/includes/constants.php',
|
||||
FOLDER_BUILD + '/' + PACKAGE.name + '/readme.txt'
|
||||
], { base: './' })
|
||||
.pipe(replace('<wordpress-author-name>', PACKAGE.author.name))
|
||||
.pipe(replace('<wordpress-author-url>', PACKAGE.author.url))
|
||||
.pipe(replace('<wordpress-description>', PACKAGE.description))
|
||||
.pipe(replace('<wordpress-donation-link>', PACKAGE.funding.url))
|
||||
.pipe(replace('<wordpress-license>', PACKAGE.license))
|
||||
.pipe(replace('<wordpress-minimum-version>', PACKAGE.additionalDetails.wordpressMinimumVersion))
|
||||
.pipe(replace('<wordpress-name>', PACKAGE.name))
|
||||
.pipe(replace('<wordpress-nice-name>', PACKAGE.additionalDetails.niceName))
|
||||
.pipe(replace('<wordpress-php-minimum-version>', PACKAGE.additionalDetails.phpMinimumVersion))
|
||||
.pipe(replace('<wordpress-tested-up-to-version>', PACKAGE.additionalDetails.wordpressTestedUpToVersion))
|
||||
.pipe(replace('<wordpress-version>', PACKAGE.version))
|
||||
.pipe(dest('.'));
|
||||
}
|
||||
|
||||
exports.front = bundleFrontend;
|
||||
exports.copy = copyBackend;
|
||||
exports.inject = injectMetadata;
|
||||
|
||||
const build = series(clean, bundleFrontend, copyBackend, injectMetadata);
|
||||
|
||||
const buildDev = series((cb) => { mode = 'development'; cb(); }, build);
|
||||
|
||||
const buildProd = series((cb) => { mode = 'production'; cb(); }, build);
|
||||
|
||||
|
||||
exports.clean = clean;
|
||||
exports.dev = buildDev;
|
||||
exports.default = buildDev;
|
||||
exports.prod = buildProd;
|
32854
package-lock.json
generated
32854
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
64
package.json
64
package.json
@ -1,14 +1,18 @@
|
||||
{
|
||||
"name": "connector-mobilizon",
|
||||
"version": "0.6.2",
|
||||
"version": "1.0.0",
|
||||
"description": "Display Mobilizon events in WordPress.",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build-dev": "gulp dev",
|
||||
"build-prod": "ava && gulp prod",
|
||||
"build-dev": "webpack --mode=development && gulp inject",
|
||||
"build-prod": "rimraf -- build && ava && webpack --mode=production && gulp inject",
|
||||
"clean": "rimraf -- build",
|
||||
"coverage": "c8 --all --reporter=html --reporter=text --include=source/**/*.js ava",
|
||||
"eslint": "npx eslint source/**/*.js",
|
||||
"test": "ava"
|
||||
"format": "npx prettier --write .",
|
||||
"prepare": "husky",
|
||||
"test": "ava && ./vendor/bin/phpunit"
|
||||
},
|
||||
"author": {
|
||||
"name": "Daniel Waxweiler",
|
||||
@ -20,37 +24,47 @@
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"graphql": "^15.5.1",
|
||||
"graphql-request": "^3.5.0",
|
||||
"luxon": "^2.0.2",
|
||||
"object-hash": "^2.2.0"
|
||||
"graphql": "16.8.1",
|
||||
"luxon": "3.4.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^3.15.0",
|
||||
"c8": "^7.8.0",
|
||||
"del": "^6.0.0",
|
||||
"eslint": "^7.32.0",
|
||||
"eslint-plugin-ava": "^12.0.0",
|
||||
"esm": "^3.2.25",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-replace": "^1.1.3",
|
||||
"jsdom": "^17.0.0",
|
||||
"webpack": "^5.51.1",
|
||||
"webpack-cli": "^4.8.0",
|
||||
"webpack-stream": "^6.1.2"
|
||||
"@babel/core": "7.24.4",
|
||||
"@babel/eslint-parser": "7.24.1",
|
||||
"@babel/preset-env": "7.24.4",
|
||||
"@babel/preset-react": "7.24.1",
|
||||
"@wordpress/eslint-plugin": "17.12.0",
|
||||
"ava": "6.1.2",
|
||||
"babel-loader": "9.1.3",
|
||||
"browser-env": "3.3.0",
|
||||
"c8": "9.1.0",
|
||||
"copy-webpack-plugin": "12.0.2",
|
||||
"eslint": "8.57.0",
|
||||
"eslint-plugin-ava": "14.0.0",
|
||||
"eslint-plugin-jsx": "0.1.0",
|
||||
"eslint-plugin-react": "7.34.1",
|
||||
"esm": "3.2.25",
|
||||
"gulp": "4.0.2",
|
||||
"gulp-replace": "1.1.4",
|
||||
"husky": "9.0.11",
|
||||
"lint-staged": "15.2.2",
|
||||
"prettier": "3.2.5",
|
||||
"rimraf": "5.0.5",
|
||||
"webpack": "5.91.0",
|
||||
"webpack-cli": "5.1.4"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"./source/**/*test.js"
|
||||
],
|
||||
"require": [
|
||||
"esm"
|
||||
]
|
||||
},
|
||||
"additionalDetails": {
|
||||
"niceName": "Connector for Mobilizon",
|
||||
"phpMinimumVersion": 5.4,
|
||||
"phpMinimumVersion": 7.4,
|
||||
"wordpressMinimumVersion": 5.6,
|
||||
"wordpressTestedUpToVersion": 5.8
|
||||
"wordpressTestedUpToVersion": "6.5"
|
||||
},
|
||||
"lint-staged": {
|
||||
"source/**/*.js": "eslint",
|
||||
"**/*": "prettier --write --ignore-unknown"
|
||||
}
|
||||
}
|
||||
|
27
phpunit.xml
Normal file
27
phpunit.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
cacheResultFile=".phpunit.cache/test-results"
|
||||
executionOrder="depends,defects"
|
||||
forceCoversAnnotation="false"
|
||||
beStrictAboutCoversAnnotation="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
convertDeprecationsToExceptions="true"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
verbose="true">
|
||||
<testsuites>
|
||||
<testsuite name="default">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<coverage cacheDirectory=".phpunit.cache/code-coverage"
|
||||
processUncoveredFiles="true">
|
||||
<include>
|
||||
<directory suffix=".php">source/includes</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
2
qodana.yaml
Normal file
2
qodana.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
version: '1.0'
|
||||
linter: jetbrains/qodana-js:2022.3-eap
|
160
source/changelog.txt
Normal file
160
source/changelog.txt
Normal file
@ -0,0 +1,160 @@
|
||||
### [Unreleased]
|
||||
#### Added
|
||||
#### Changed
|
||||
#### Deprecated
|
||||
#### Removed
|
||||
#### Fixed
|
||||
#### Security
|
||||
|
||||
### [1.0.0]
|
||||
#### Added
|
||||
- Display name of group when it cannot be found
|
||||
#### Changed
|
||||
- Let backend do requests to API of Mobilizon instance for increased privacy
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Fix displaying more than one block in the editor
|
||||
|
||||
### [0.11.5]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.5
|
||||
|
||||
### [0.11.4]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.4
|
||||
- Update dependencies
|
||||
|
||||
### [0.11.3]
|
||||
#### Fixed
|
||||
- Clean up distributed files
|
||||
|
||||
### [0.11.2]
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.3
|
||||
|
||||
### [0.11.1]
|
||||
#### Fixed
|
||||
- Revert minimum PHP version to 7.4 to allow some more time for upgrading PHP
|
||||
|
||||
### [0.11.0]
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.2
|
||||
#### Security
|
||||
- Set minimum PHP version to oldest stable 8.0
|
||||
|
||||
### [0.10.1]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.1
|
||||
- Update dependencies
|
||||
|
||||
### [0.10.0]
|
||||
#### Added
|
||||
- Add Gutenberg events list block
|
||||
- Show loading indicator during request
|
||||
#### Changed
|
||||
- Set list style type to none and left padding to zero for all occurences
|
||||
- Move shortcut usage description into installation section in `readme.txt`
|
||||
- Update dependencies
|
||||
|
||||
### [0.9.1] - 2020-05-19
|
||||
#### Fixed
|
||||
- Fix WordPress compatibility version number
|
||||
|
||||
### [0.9.0] - 2020-05-19
|
||||
#### Added
|
||||
- Improve explanation of group name filter
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.0
|
||||
#### Fixed
|
||||
- Fix displaying error message for the case the group is not found
|
||||
|
||||
### [0.8.0] - 2022-01-09
|
||||
#### Added
|
||||
- Add support for older browsers using babel
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.9
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Use ES modules correctly
|
||||
- Trim events' location
|
||||
|
||||
### [0.7.0] - 2021-12-23
|
||||
#### Added
|
||||
- Add specific error message for the case the group is not found
|
||||
- Add code formatter prettier
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Simplify build process
|
||||
#### Fixed
|
||||
- Fix Invalid DateTime on event end time being null
|
||||
#### Security
|
||||
- Set minimum PHP version to oldest stable 7.4
|
||||
|
||||
### [0.6.2] - 2021-08-24
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Fix empty WordPress timezone_string option resulting in Invalid DateTime
|
||||
|
||||
### [0.6.1] - 2021-07-13
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.8
|
||||
- Update dependencies
|
||||
|
||||
### [0.6.0] - 2021-06-02
|
||||
#### Added
|
||||
- Optionally display the current offset as short name after the time via the general plugin settings
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Capitalise Mobilizon name in description
|
||||
|
||||
### [0.5.0] - 2021-05-06
|
||||
#### Added
|
||||
- Localise dates based on the WordPress locale and time zone
|
||||
#### Changed
|
||||
- Clearly list features in `readme.txt` description
|
||||
- Update dev dependencies c8, eslint, gulp-replace, webpack
|
||||
#### Fixed
|
||||
- Improve translatability
|
||||
|
||||
### [0.4.0] - 2021-04-20
|
||||
#### Added
|
||||
- Show events' location if set: `description` and `locality` fields
|
||||
- Plugin icon
|
||||
#### Changed
|
||||
- Update dev dependencies eslint, jsdom, webpack
|
||||
|
||||
### [0.3.0] - 2021-04-05
|
||||
#### Added
|
||||
- Donation link to WordPress Plugin Directory sidebar and to `package.json`
|
||||
- Cache requests for 2 minutes
|
||||
- Set up ESLint static code analysis
|
||||
#### Changed
|
||||
- Update luxon dependency
|
||||
- Update dev dependencies jsdom, webpack, webpack-cli
|
||||
|
||||
### [0.2.2] - 2021-03-10
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.7
|
||||
- Update graphql dependency
|
||||
- Update dev dependencies jsdom, webpack, webpack-cli, webpack-stream
|
||||
|
||||
### [0.2.1] - 2021-01-15
|
||||
#### Fixed
|
||||
- Add missing backtick to `readme.txt`
|
||||
|
||||
### [0.2.0] - 2021-01-15
|
||||
#### Added
|
||||
- `changelog.txt`
|
||||
- Changelog maintenance steps to `README.md`
|
||||
- Link to Github repository in `readme.txt`
|
||||
- Option to show events of a specific group by indicating its name
|
||||
#### Changed
|
||||
- Use same Markdown style in `README.md` as in other documents
|
||||
|
||||
### [0.1.0] - 2021-01-09
|
||||
initial release
|
90
source/connector-mobilizon.php
Normal file
90
source/connector-mobilizon.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: <wordpress-nice-name>
|
||||
* Author: <wordpress-author-name>
|
||||
* Author URI: <wordpress-author-url>
|
||||
* Description: <wordpress-description>
|
||||
* Version: <wordpress-version>
|
||||
* Requires at least: <wordpress-minimum-version>
|
||||
* Requires PHP: <wordpress-php-minimum-version>
|
||||
* License: <wordpress-license>
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/includes/exceptions/GeneralException.php';
|
||||
require_once __DIR__ . '/includes/exceptions/GroupNotFoundException.php';
|
||||
require_once __DIR__ . '/includes/Constants.php';
|
||||
require_once __DIR__ . '/includes/Api.php';
|
||||
require_once __DIR__ . '/includes/EventsCache.php';
|
||||
require_once __DIR__ . '/includes/Settings.php';
|
||||
require_once __DIR__ . '/includes/DateTimeWrapper.php';
|
||||
require_once __DIR__ . '/includes/Formatter.php';
|
||||
require_once __DIR__ . '/includes/GraphQlClient.php';
|
||||
require_once __DIR__ . '/includes/EventsListBlock.php';
|
||||
require_once __DIR__ . '/includes/EventsListShortcut.php';
|
||||
require_once __DIR__ . '/includes/EventsListWidget.php';
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
final class Mobilizon_Connector {
|
||||
|
||||
private function __construct() {
|
||||
add_action('init', [$this, 'register_api']);
|
||||
add_action('init', [$this, 'register_blocks']);
|
||||
add_action('init', [$this, 'register_settings'], 1); // required for register_blocks
|
||||
add_action('init', [$this, 'register_shortcut']);
|
||||
add_action('widgets_init', [$this, 'register_widget']);
|
||||
register_activation_hook(__FILE__, [$this, 'enable_activation']);
|
||||
}
|
||||
|
||||
public static function init() {
|
||||
// Create singleton instance.
|
||||
static $instance = false;
|
||||
if(!$instance) {
|
||||
$instance = new self();
|
||||
}
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function enable_activation() {
|
||||
MobilizonConnector\Settings::setDefaultOptions();
|
||||
}
|
||||
|
||||
private function load_settings_globally_before_script($scriptName) {
|
||||
$settings = array(
|
||||
'isShortOffsetNameShown' => MobilizonConnector\Settings::isShortOffsetNameShown(),
|
||||
'locale' => str_replace('_', '-', get_locale()),
|
||||
'timeZone' => wp_timezone_string()
|
||||
);
|
||||
wp_add_inline_script($scriptName, 'var MOBILIZON_CONNECTOR = ' . json_encode($settings), 'before');
|
||||
}
|
||||
|
||||
public function register_api() {
|
||||
MobilizonConnector\Api::init();
|
||||
}
|
||||
|
||||
public function register_blocks() {
|
||||
$scriptName = MobilizonConnector\EventsListBlock::initAndReturnScriptName();
|
||||
$this->load_settings_globally_before_script($scriptName);
|
||||
}
|
||||
|
||||
public function register_settings() {
|
||||
MobilizonConnector\Settings::init();
|
||||
}
|
||||
|
||||
public function register_shortcut() {
|
||||
MobilizonConnector\EventsListShortcut::init();
|
||||
}
|
||||
|
||||
public function register_widget() {
|
||||
register_widget('MobilizonConnector\EventsListWidget');
|
||||
}
|
||||
}
|
||||
|
||||
function mobilizon_connector_run_plugin() {
|
||||
return Mobilizon_Connector::init();
|
||||
}
|
||||
|
||||
mobilizon_connector_run_plugin();
|
@ -1,73 +0,0 @@
|
||||
### [Unreleased]
|
||||
#### Added
|
||||
#### Changed
|
||||
#### Deprecated
|
||||
#### Removed
|
||||
#### Fixed
|
||||
#### Security
|
||||
|
||||
### [0.6.2] - 2021-08-24
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Fix empty WordPress timezone_string option resulting in Invalid DateTime
|
||||
|
||||
### [0.6.1] - 2021-07-13
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.8
|
||||
- Update dependencies
|
||||
|
||||
### [0.6.0] - 2021-06-02
|
||||
#### Added
|
||||
- Optionally display the current offset as short name after the time via the general plugin settings
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Capitalise Mobilizon name in description
|
||||
|
||||
### [0.5.0] - 2021-05-06
|
||||
#### Added
|
||||
- Localise dates based on the WordPress locale and time zone
|
||||
#### Changed
|
||||
- Clearly list features in `readme.txt` description
|
||||
- Update dev dependencies c8, eslint, gulp-replace, webpack
|
||||
#### Fixed
|
||||
- Improve translatability
|
||||
|
||||
### [0.4.0] - 2021-04-20
|
||||
#### Added
|
||||
- Show events' location if set: `description` and `locality` fields
|
||||
- Plugin icon
|
||||
#### Changed
|
||||
- Update dev dependencies eslint, jsdom, webpack
|
||||
|
||||
### [0.3.0] - 2021-04-05
|
||||
#### Added
|
||||
- Donation link to WordPress Plugin Directory sidebar and to `package.json`
|
||||
- Cache requests for 2 minutes
|
||||
- Set up ESLint static code analysis
|
||||
#### Changed
|
||||
- Update luxon dependency
|
||||
- Update dev dependencies jsdom, webpack, webpack-cli
|
||||
|
||||
### [0.2.2] - 2021-03-10
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.7
|
||||
- Update graphql dependency
|
||||
- Update dev dependencies jsdom, webpack, webpack-cli, webpack-stream
|
||||
|
||||
### [0.2.1] - 2021-01-15
|
||||
#### Fixed
|
||||
- Add missing backtick to `readme.txt`
|
||||
|
||||
### [0.2.0] - 2021-01-15
|
||||
#### Added
|
||||
- `changelog.txt`
|
||||
- Changelog maintenance steps to `README.md`
|
||||
- Link to Github repository in `readme.txt`
|
||||
- Option to show events of a specific group by indicating its name
|
||||
#### Changed
|
||||
- Use same Markdown style in `README.md` as in other documents
|
||||
|
||||
### [0.1.0] - 2021-01-09
|
||||
initial release
|
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: <wordpress-nice-name>
|
||||
* Author: <wordpress-author-name>
|
||||
* Author URI: <wordpress-author-url>
|
||||
* Description: <wordpress-description>
|
||||
* Version: <wordpress-version>
|
||||
* Requires at least: <wordpress-minimum-version>
|
||||
* Requires PHP: <wordpress-php-minimum-version>
|
||||
* License: <wordpress-license>
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/includes/constants.php';
|
||||
require_once __DIR__ . '/includes/settings.php';
|
||||
require_once __DIR__ . '/includes/events-list-shortcut.php';
|
||||
require_once __DIR__ . '/includes/events-list-widget.php';
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
function mobilizon_connector_activate() {
|
||||
MobilizonConnector\Settings::setDefaultOptions();
|
||||
}
|
||||
register_activation_hook(__FILE__, 'mobilizon_connector_activate');
|
||||
|
||||
function mobilizon_connector_initialize() {
|
||||
MobilizonConnector\Settings::init();
|
||||
MobilizonConnector\EventsListShortcut::init();
|
||||
}
|
||||
add_action('init', 'mobilizon_connector_initialize');
|
||||
|
||||
function mobilizon_connector_load_scripts() {
|
||||
wp_enqueue_script(MobilizonConnector\NAME . '-js', plugins_url('front/events-loader.js', __FILE__ ));
|
||||
}
|
||||
add_action('wp_enqueue_scripts', 'mobilizon_connector_load_scripts');
|
||||
|
||||
function mobilizon_connector_register_events_list_widget() {
|
||||
register_widget('MobilizonConnector\EventsListWidget');
|
||||
}
|
||||
add_action('widgets_init', 'mobilizon_connector_register_events_list_widget');
|
@ -1,61 +0,0 @@
|
||||
import test from 'ava'
|
||||
import { JSDOM } from 'jsdom'
|
||||
|
||||
import { displayEvents, displayErrorMessage } from './events-displayer'
|
||||
|
||||
let document
|
||||
|
||||
test.before(() => {
|
||||
document = new JSDOM().window.document
|
||||
})
|
||||
|
||||
test.beforeEach(t => {
|
||||
t.context.list = document.createElement('ul')
|
||||
t.context.list.setAttribute('data-locale', 'en-GB')
|
||||
t.context.list.setAttribute('data-maximum', '2')
|
||||
t.context.list.setAttribute('data-time-zone', 'utc')
|
||||
const listElement = document.createElement('li')
|
||||
listElement.setAttribute('style', 'display: none;')
|
||||
t.context.list.appendChild(listElement)
|
||||
})
|
||||
|
||||
test('#displayEvents one event', t => {
|
||||
const list = t.context.list
|
||||
const data = {
|
||||
events: {
|
||||
elements: [
|
||||
{
|
||||
title: 'a',
|
||||
url: 'b',
|
||||
beginsOn: '2021-04-15T10:30:00Z',
|
||||
endsOn: '2021-04-15T15:30:00Z',
|
||||
physicalAddress: {
|
||||
description: 'c',
|
||||
locality: 'd'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
displayEvents({ data, document, list })
|
||||
t.is(list.children.length, 2)
|
||||
t.is(list.children[1].childNodes[0].tagName, 'A')
|
||||
t.is(list.children[1].childNodes[0].getAttribute('href'), 'b')
|
||||
t.is(list.children[1].childNodes[0].childNodes[0].nodeValue, 'a')
|
||||
t.is(list.children[1].childNodes[1].tagName, 'BR')
|
||||
t.is(list.children[1].childNodes[2].nodeValue, '15/04/2021 10:30 - 15:30')
|
||||
t.is(list.children[1].childNodes[3].tagName, 'BR')
|
||||
t.is(list.children[1].childNodes[4].nodeValue, 'c, d')
|
||||
})
|
||||
|
||||
test('#displayErrorMessage no children added', t => {
|
||||
const list = t.context.list
|
||||
displayErrorMessage({ data: '', list })
|
||||
t.is(list.children.length, 1)
|
||||
})
|
||||
|
||||
test('#displayErrorMessage error message display', t => {
|
||||
const list = t.context.list
|
||||
displayErrorMessage({ data: '', list })
|
||||
t.is(list.children[0].style.display, 'block')
|
||||
})
|
@ -1,51 +0,0 @@
|
||||
import Formatter from './formatter'
|
||||
import { createAnchorElement } from './html-creator'
|
||||
|
||||
export function displayEvents({ data, document, list }) {
|
||||
const locale = list.getAttribute('data-locale')
|
||||
const maxEventsCount = list.getAttribute('data-maximum')
|
||||
const timeZone = list.getAttribute('data-time-zone')
|
||||
const isShortOffsetNameShown = list.hasAttribute('data-is-short-offset-name-shown')
|
||||
const events = data.events ? data.events.elements : data.group.organizedEvents.elements
|
||||
const eventsCount = Math.min(maxEventsCount, events.length)
|
||||
for (let i = 0; i < eventsCount; i++) {
|
||||
const li = document.createElement('li')
|
||||
|
||||
const a = createAnchorElement({ document, text: events[i].title, url: events[i].url })
|
||||
li.appendChild(a)
|
||||
|
||||
const br = document.createElement('br')
|
||||
li.appendChild(br)
|
||||
|
||||
const date = Formatter.formatDate({
|
||||
locale,
|
||||
start: events[i].beginsOn,
|
||||
end: events[i].endsOn,
|
||||
timeZone,
|
||||
isShortOffsetNameShown,
|
||||
})
|
||||
const textnode = document.createTextNode(date)
|
||||
li.appendChild(textnode)
|
||||
|
||||
if (events[i].physicalAddress) {
|
||||
const location = Formatter.formatLocation({
|
||||
description: events[i].physicalAddress.description,
|
||||
locality: events[i].physicalAddress.locality
|
||||
})
|
||||
if (location) {
|
||||
const brBeforeLocation = document.createElement('br')
|
||||
li.appendChild(brBeforeLocation)
|
||||
|
||||
const textnodeLocation = document.createTextNode(location)
|
||||
li.appendChild(textnodeLocation)
|
||||
}
|
||||
}
|
||||
|
||||
list.appendChild(li)
|
||||
}
|
||||
}
|
||||
|
||||
export function displayErrorMessage({ data, list }) {
|
||||
console.error(data)
|
||||
list.children[0].style.display = 'block'
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
import { displayEvents, displayErrorMessage } from './events-displayer'
|
||||
import * as GraphqlWrapper from './graphql-wrapper'
|
||||
|
||||
const NAME = '<wordpress-name>'
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const eventLists = document.getElementsByClassName(NAME + '_events-list')
|
||||
for (let list of eventLists) {
|
||||
const url = list.getAttribute('data-url') + '/api'
|
||||
const limit = parseInt(list.getAttribute('data-maximum'))
|
||||
const groupName = list.getAttribute('data-group-name')
|
||||
if (groupName) {
|
||||
GraphqlWrapper.getUpcomingEventsByGroupName({ url, limit, groupName })
|
||||
.then((data) => displayEvents({ data, document, list }))
|
||||
.catch((data) => displayErrorMessage({ data, list }))
|
||||
} else {
|
||||
GraphqlWrapper.getUpcomingEvents({ url, limit })
|
||||
.then((data) => displayEvents({ data, document, list }))
|
||||
.catch((data) => displayErrorMessage({ data, list }))
|
||||
}
|
||||
}
|
||||
})
|
@ -1,37 +0,0 @@
|
||||
import test from 'ava'
|
||||
import Formatter from './formatter'
|
||||
|
||||
test('#formatDate one date', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-15T15:30:00Z' })
|
||||
t.is(date, '15/04/2021 10:30 - 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate one date with short offset name', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-15T15:30:00Z', isShortOffsetNameShown: true })
|
||||
t.is(date, '15/04/2021 10:30 - 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatDate two dates', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-16T15:30:00Z' })
|
||||
t.is(date, '15/04/2021 10:30 - 16/04/2021 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate two dates with short offset name', t => {
|
||||
const date = Formatter.formatDate({ start: '2021-04-15T10:30:00Z', end: '2021-04-16T15:30:00Z', isShortOffsetNameShown: true })
|
||||
t.is(date, '15/04/2021 10:30 (UTC) - 16/04/2021 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatLocation both parameters', t => {
|
||||
const date = Formatter.formatLocation({ description: 'a', locality: 'b' })
|
||||
t.is(date, 'a, b')
|
||||
})
|
||||
|
||||
test('#formatLocation description only', t => {
|
||||
const date = Formatter.formatLocation({ description: 'a' })
|
||||
t.is(date, 'a')
|
||||
})
|
||||
|
||||
test('#formatLocation locality only', t => {
|
||||
const date = Formatter.formatLocation({ locality: 'a' })
|
||||
t.is(date, 'a')
|
||||
})
|
@ -1,39 +0,0 @@
|
||||
import DateTimeWrapper from './date-time-wrapper'
|
||||
|
||||
export default class Formatter {
|
||||
|
||||
static formatDate({ locale, timeZone, start, end, isShortOffsetNameShown }) {
|
||||
const startDateTime = new DateTimeWrapper({ locale, text: start, timeZone })
|
||||
const endDateTime = new DateTimeWrapper({ locale, text: end, timeZone })
|
||||
let dateText = startDateTime.getShortDate()
|
||||
dateText += ' ' + startDateTime.get24Time()
|
||||
if (!startDateTime.equalsDate(endDateTime)) {
|
||||
if (isShortOffsetNameShown) {
|
||||
dateText += ' (' + startDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
dateText += ' - '
|
||||
dateText += endDateTime.getShortDate() + ' '
|
||||
} else {
|
||||
dateText += ' - '
|
||||
}
|
||||
dateText += endDateTime.get24Time()
|
||||
if (isShortOffsetNameShown) {
|
||||
dateText += ' (' + endDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
return dateText
|
||||
}
|
||||
|
||||
static formatLocation({ description, locality }) {
|
||||
let location = ''
|
||||
if (description) {
|
||||
location += description
|
||||
}
|
||||
if (location && locality) {
|
||||
location += ', '
|
||||
}
|
||||
if (locality) {
|
||||
location += locality
|
||||
}
|
||||
return location
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
import SessionCache from './session-cache'
|
||||
import { request } from 'graphql-request'
|
||||
import DateTimeWrapper from './date-time-wrapper'
|
||||
|
||||
export function getUpcomingEvents({ url, limit }) {
|
||||
const query = `
|
||||
query ($limit: Int) {
|
||||
events(limit: $limit) {
|
||||
elements {
|
||||
id,
|
||||
title,
|
||||
url,
|
||||
beginsOn,
|
||||
endsOn,
|
||||
physicalAddress {
|
||||
description,
|
||||
locality
|
||||
}
|
||||
},
|
||||
total
|
||||
}
|
||||
}
|
||||
`
|
||||
const dataInCache = SessionCache.get(sessionStorage, { url, query, variables: { limit }})
|
||||
if (dataInCache !== null)
|
||||
return Promise.resolve(dataInCache)
|
||||
return request(url, query, { limit })
|
||||
.then((data) => {
|
||||
SessionCache.add(sessionStorage, { url, query, variables: { limit }}, data)
|
||||
return Promise.resolve(data)
|
||||
})
|
||||
}
|
||||
|
||||
export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
|
||||
const query = `
|
||||
query ($afterDatetime: DateTime, $groupName: String, $limit: Int) {
|
||||
group(preferredUsername: $groupName) {
|
||||
organizedEvents(afterDatetime: $afterDatetime, limit: $limit) {
|
||||
elements {
|
||||
id,
|
||||
title,
|
||||
url,
|
||||
beginsOn,
|
||||
endsOn,
|
||||
physicalAddress {
|
||||
description,
|
||||
locality
|
||||
}
|
||||
},
|
||||
total
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
|
||||
const dataInCache = SessionCache.get(sessionStorage, { url, query, variables: { afterDatetime, groupName, limit }})
|
||||
if (dataInCache !== null)
|
||||
return Promise.resolve(dataInCache)
|
||||
return request(url, query, { afterDatetime, groupName, limit })
|
||||
.then((data) => {
|
||||
SessionCache.add(sessionStorage, { url, query, variables: { afterDatetime, groupName, limit }}, data)
|
||||
return Promise.resolve(data)
|
||||
})
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import test from 'ava'
|
||||
import hash from './object-hash-wrapper'
|
||||
|
||||
test('#hash object', t => {
|
||||
t.is(hash({foo: 'bar'}), 'a75c05bdca7d704bdfcd761913e5a4e4636e956b')
|
||||
})
|
@ -1,5 +0,0 @@
|
||||
import objectHash from 'object-hash'
|
||||
|
||||
export default function hash(object) {
|
||||
return objectHash(object)
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
import test from 'ava'
|
||||
import SessionCache from './session-cache'
|
||||
|
||||
const fakeStorage = {
|
||||
|
||||
elements: {},
|
||||
|
||||
clear() {
|
||||
this.elements = {}
|
||||
},
|
||||
|
||||
getItem(key) {
|
||||
const value = this.elements[key]
|
||||
if (value === undefined) return null
|
||||
return value
|
||||
},
|
||||
|
||||
setItem(key, value) {
|
||||
this.elements[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
test.afterEach(() => {
|
||||
fakeStorage.clear()
|
||||
})
|
||||
|
||||
test('#add & #get', t => {
|
||||
SessionCache.add(fakeStorage, { a: 'b' }, { c: 'd' })
|
||||
t.deepEqual(SessionCache.get(fakeStorage, { a: 'b' }), { c: 'd' })
|
||||
})
|
||||
|
||||
test('#get no entry', t => {
|
||||
t.is(SessionCache.get(fakeStorage, { a: 'bb' }), null)
|
||||
})
|
@ -1,24 +0,0 @@
|
||||
import hash from './object-hash-wrapper'
|
||||
|
||||
const MAX_AGE_IN_MS = 120000
|
||||
|
||||
export default class SessionCache {
|
||||
|
||||
static add(storage, parameters, data) {
|
||||
const key = hash(parameters)
|
||||
const timestamp = Date.now()
|
||||
const value = {
|
||||
data,
|
||||
timestamp,
|
||||
}
|
||||
storage.setItem(key, JSON.stringify(value))
|
||||
}
|
||||
|
||||
static get(storage, parameters) {
|
||||
const key = hash(parameters)
|
||||
const value = JSON.parse(storage.getItem(key))
|
||||
if (value && value.timestamp && value.timestamp > Date.now() - MAX_AGE_IN_MS)
|
||||
return value.data
|
||||
return null
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<ul class="<?php echo esc_attr($classNamePrefix); ?>_events-list"
|
||||
data-url="<?php echo esc_attr($url); ?>"
|
||||
data-locale="<?php echo esc_attr($locale); ?>"
|
||||
data-maximum="<?php echo esc_attr($eventsCount); ?>"
|
||||
data-group-name="<?php echo esc_attr($groupName); ?>"
|
||||
data-time-zone="<?php echo esc_attr($timeZone); ?>"
|
||||
<?php echo $isShortOffsetNameShown ? 'data-is-short-offset-name-shown' : ''; ?>>
|
||||
<li style="display: none;"><?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?></li>
|
||||
</ul>
|
1
source/front/block-events-loader.js
Normal file
1
source/front/block-events-loader.js
Normal file
@ -0,0 +1 @@
|
||||
import './blocks/events-list/index.js'
|
113
source/front/blocks/events-list/edit.js
Normal file
113
source/front/blocks/events-list/edit.js
Normal file
@ -0,0 +1,113 @@
|
||||
/* eslint-disable @wordpress/i18n-ellipsis */
|
||||
import {
|
||||
clearEventsList,
|
||||
displayErrorMessage,
|
||||
displayEvents,
|
||||
hideErrorMessages,
|
||||
showLoadingIndicator,
|
||||
} from '../../events-displayer.js'
|
||||
|
||||
const { InspectorControls, useBlockProps } = wp.blockEditor
|
||||
const { Panel, PanelBody } = wp.components
|
||||
const { useEffect } = wp.element
|
||||
const { __ } = wp.i18n
|
||||
|
||||
const NAME = '<wordpress-name>'
|
||||
|
||||
export default ({ attributes, setAttributes }) => {
|
||||
let timer
|
||||
const blockProps = useBlockProps({
|
||||
className: NAME + '_events-list',
|
||||
})
|
||||
function reloadEventList(eventsCount, groupName) {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
timer = setTimeout(async () => {
|
||||
const container = document.getElementById(blockProps.id)
|
||||
if (container) {
|
||||
hideErrorMessages(container)
|
||||
clearEventsList(container)
|
||||
showLoadingIndicator(container)
|
||||
let url = `/wp-json/connector-mobilizon/v1/events?eventsCount=${eventsCount}`
|
||||
if (groupName) {
|
||||
url += `&groupName=${groupName}`
|
||||
}
|
||||
await fetch(url)
|
||||
.then((response) => response.text())
|
||||
.then((data) => {
|
||||
const events = JSON.parse(data)
|
||||
displayEvents({
|
||||
events,
|
||||
document,
|
||||
container,
|
||||
maxEventsCount: eventsCount,
|
||||
})
|
||||
})
|
||||
.catch((data) => {
|
||||
displayErrorMessage({ data, container })
|
||||
})
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
useEffect(() => {
|
||||
reloadEventList(attributes.eventsCount, attributes.groupName)
|
||||
}, [])
|
||||
function updateEventsCount(event) {
|
||||
let newValue = Number(event.target.value)
|
||||
if (newValue < 1) newValue = 1
|
||||
setAttributes({ eventsCount: newValue })
|
||||
reloadEventList(newValue, attributes.groupName)
|
||||
}
|
||||
function updateGroupName(event) {
|
||||
const newValue = event.target.value
|
||||
setAttributes({ groupName: newValue })
|
||||
reloadEventList(attributes.eventsCount, newValue)
|
||||
}
|
||||
return [
|
||||
<InspectorControls>
|
||||
<Panel>
|
||||
<PanelBody title={__('Events List Settings', '<wordpress-name>')}>
|
||||
<label
|
||||
className="components-base-control__label"
|
||||
htmlFor={NAME + '_events-count'}
|
||||
>
|
||||
{__('Number of events to show', '<wordpress-name>')}
|
||||
</label>
|
||||
<input
|
||||
className="components-text-control__input"
|
||||
type="number"
|
||||
value={attributes.eventsCount}
|
||||
onChange={updateEventsCount}
|
||||
id={NAME + '_events-count'}
|
||||
/>
|
||||
<label
|
||||
className="components-base-control__label"
|
||||
htmlFor={NAME + '_group-name'}
|
||||
>
|
||||
{__('Group name (optional)', '<wordpress-name>')}
|
||||
</label>
|
||||
<input
|
||||
className="components-text-control__input"
|
||||
type="text"
|
||||
value={attributes.groupName}
|
||||
onChange={updateGroupName}
|
||||
id={NAME + '_group-name'}
|
||||
/>
|
||||
</PanelBody>
|
||||
</Panel>
|
||||
</InspectorControls>,
|
||||
<div {...blockProps}>
|
||||
<div className="general-error" style={{ display: 'none' }}>
|
||||
{__('The events could not be loaded!', '<wordpress-name>')}
|
||||
</div>
|
||||
<div className="group-not-found" style={{ display: 'none' }}>
|
||||
{__('The group could not be found!', '<wordpress-name>')}
|
||||
</div>
|
||||
<div className="loading-indicator" style={{ display: 'none' }}>
|
||||
{__('Loading...', '<wordpress-name>')}
|
||||
</div>
|
||||
<ul style={{ 'list-style-type': 'none', 'padding-left': 0 }}></ul>
|
||||
</div>,
|
||||
]
|
||||
}
|
7
source/front/blocks/events-list/index.js
Normal file
7
source/front/blocks/events-list/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
import edit from './edit.js'
|
||||
|
||||
const { registerBlockType } = wp.blocks
|
||||
|
||||
const NAME = '<wordpress-name>'
|
||||
|
||||
registerBlockType(NAME + '/events-list', { edit })
|
@ -1,67 +1,76 @@
|
||||
import test from 'ava'
|
||||
import DateTimeWrapper from './date-time-wrapper'
|
||||
import DateTimeWrapper from './date-time-wrapper.js'
|
||||
|
||||
test('#getShortDate usual date', t => {
|
||||
test('#getShortDate usual date', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with timezone string', t => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: 'Europe/Rome' })
|
||||
test('#getShortDate usual date with timezone string', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: 'Europe/Rome',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with fixed offset', t => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: 'UTC+02:00' })
|
||||
test('#getShortDate usual date with fixed offset', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: 'UTC+02:00',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with fixed offset without UTC prefix', t => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: '+02:00' })
|
||||
test('#getShortDate usual date with fixed offset without UTC prefix', (t) => {
|
||||
const d = new DateTimeWrapper({
|
||||
text: '2020-12-24T16:45:00Z',
|
||||
timeZone: '+02:00',
|
||||
})
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#getShortDate usual date with empty time zone', t => {
|
||||
test('#getShortDate usual date with empty time zone', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z', timeZone: '' })
|
||||
t.is(d.getShortDate(), '24/12/2020')
|
||||
})
|
||||
|
||||
test('#get24Time usual time', t => {
|
||||
test('#get24Time usual time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.get24Time(), '16:45')
|
||||
})
|
||||
|
||||
test('#equalsDate same date, different time', t => {
|
||||
test('#equalsDate same date, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-12-24T17:46:01Z' })
|
||||
t.true(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different date, different time', t => {
|
||||
test('#equalsDate different date, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2021-11-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different day, different time', t => {
|
||||
test('#equalsDate different day, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-12-25T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different month, different time', t => {
|
||||
test('#equalsDate different month, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2020-11-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#equalsDate different year, different time', t => {
|
||||
test('#equalsDate different year, different time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
const e = new DateTimeWrapper({ text: '2021-12-24T17:46:01Z' })
|
||||
t.false(d.equalsDate(e))
|
||||
})
|
||||
|
||||
test('#getCurrentDatetimeAsString correct format', t => {
|
||||
test('#getCurrentDatetimeAsString correct format', (t) => {
|
||||
const d = DateTimeWrapper.getCurrentDatetimeAsString()
|
||||
t.is(d[4], '-')
|
||||
t.is(d[7], '-')
|
||||
@ -69,10 +78,10 @@ test('#getCurrentDatetimeAsString correct format', t => {
|
||||
t.is(d[13], ':')
|
||||
t.is(d[16], ':')
|
||||
t.is(d[19], '.')
|
||||
t.is(d[d.length-3], ':')
|
||||
t.is(d[d.length - 3], ':')
|
||||
})
|
||||
|
||||
test('#getShortOffsetName usual time', t => {
|
||||
test('#getShortOffsetName usual time', (t) => {
|
||||
const d = new DateTimeWrapper({ text: '2020-12-24T16:45:00Z' })
|
||||
t.is(d.getShortOffsetName(), 'UTC')
|
||||
})
|
@ -1,12 +1,14 @@
|
||||
import { DateTime } from 'luxon'
|
||||
|
||||
export default class DateTimeWrapper {
|
||||
|
||||
constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) {
|
||||
if (!timeZone) {
|
||||
timeZone = 'utc'
|
||||
}
|
||||
if (timeZone.includes(':') && timeZone.substring(0, 3).toUpperCase() !== 'UTC') {
|
||||
if (
|
||||
timeZone.includes(':') &&
|
||||
timeZone.substring(0, 3).toUpperCase() !== 'UTC'
|
||||
) {
|
||||
timeZone = 'UTC' + timeZone
|
||||
}
|
||||
this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone })
|
||||
@ -25,11 +27,13 @@ export default class DateTimeWrapper {
|
||||
}
|
||||
|
||||
equalsDate(other) {
|
||||
return this.dateTime &&
|
||||
return (
|
||||
this.dateTime &&
|
||||
other.dateTime &&
|
||||
this.dateTime.day === other.dateTime.day &&
|
||||
this.dateTime.month === other.dateTime.month &&
|
||||
this.dateTime.year === other.dateTime.year
|
||||
)
|
||||
}
|
||||
|
||||
static getCurrentDatetimeAsString() {
|
117
source/front/events-displayer-test.js
Normal file
117
source/front/events-displayer-test.js
Normal file
@ -0,0 +1,117 @@
|
||||
import test from 'ava'
|
||||
import browserEnv from 'browser-env'
|
||||
|
||||
import {
|
||||
displayEvents,
|
||||
displayErrorMessage,
|
||||
hideErrorMessages,
|
||||
showLoadingIndicator,
|
||||
} from './events-displayer.js'
|
||||
|
||||
test.before(() => {
|
||||
browserEnv()
|
||||
window.MOBILIZON_CONNECTOR = {
|
||||
locale: 'en-GB',
|
||||
timeZone: 'utc',
|
||||
}
|
||||
})
|
||||
|
||||
test.beforeEach((t) => {
|
||||
t.context.container = document.createElement('div')
|
||||
|
||||
const errorMessageGeneral = document.createElement('div')
|
||||
errorMessageGeneral.setAttribute('class', 'general-error')
|
||||
errorMessageGeneral.setAttribute('style', 'display: none;')
|
||||
t.context.container.appendChild(errorMessageGeneral)
|
||||
|
||||
const errorMessageGroupNotFound = document.createElement('div')
|
||||
errorMessageGroupNotFound.setAttribute('class', 'group-not-found')
|
||||
errorMessageGroupNotFound.setAttribute('style', 'display: none;')
|
||||
t.context.container.appendChild(errorMessageGroupNotFound)
|
||||
|
||||
const loadingIndicator = document.createElement('div')
|
||||
loadingIndicator.setAttribute('class', 'loading-indicator')
|
||||
loadingIndicator.setAttribute('style', 'display: none;')
|
||||
t.context.container.appendChild(loadingIndicator)
|
||||
|
||||
const list = document.createElement('ul')
|
||||
t.context.container.appendChild(list)
|
||||
})
|
||||
|
||||
test('#displayEvents one event', (t) => {
|
||||
const events = [
|
||||
{
|
||||
title: 'a',
|
||||
url: 'b',
|
||||
beginsOn: '2021-04-15T10:30:00Z',
|
||||
endsOn: '2021-04-15T15:30:00Z',
|
||||
physicalAddress: {
|
||||
description: 'c',
|
||||
locality: 'd',
|
||||
},
|
||||
},
|
||||
]
|
||||
const container = t.context.container
|
||||
displayEvents({ events, document, container, maxEventsCount: 2 })
|
||||
const list = container.querySelector('ul')
|
||||
t.is(list.children[0].childNodes[0].tagName, 'A')
|
||||
t.is(list.children[0].childNodes[0].getAttribute('href'), 'b')
|
||||
t.is(list.children[0].childNodes[0].childNodes[0].nodeValue, 'a')
|
||||
t.is(list.children[0].childNodes[1].tagName, 'BR')
|
||||
t.is(list.children[0].childNodes[2].nodeValue, '15/04/2021 10:30 - 15:30')
|
||||
t.is(list.children[0].childNodes[3].tagName, 'BR')
|
||||
t.is(list.children[0].childNodes[4].nodeValue, 'c, d')
|
||||
})
|
||||
|
||||
test('#displayErrorMessage no list entries shown', (t) => {
|
||||
const container = t.context.container
|
||||
displayErrorMessage({ data: '', container })
|
||||
const list = container.querySelector('ul')
|
||||
t.is(list.children.length, 0)
|
||||
})
|
||||
|
||||
test('#displayErrorMessage general error message display', (t) => {
|
||||
const container = t.context.container
|
||||
displayErrorMessage({ data: '', container })
|
||||
t.is(container.querySelector('.general-error').style.display, 'block')
|
||||
t.is(container.querySelector('.group-not-found').style.display, 'none')
|
||||
t.is(container.querySelector('.loading-indicator').style.display, 'none')
|
||||
})
|
||||
|
||||
test('#displayErrorMessage group not found error message display', (t) => {
|
||||
const container = t.context.container
|
||||
const data = {
|
||||
response: {
|
||||
errors: [
|
||||
{
|
||||
code: 'group_not_found',
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
displayErrorMessage({ data, container })
|
||||
t.is(container.querySelector('.general-error').style.display, 'none')
|
||||
t.is(container.querySelector('.group-not-found').style.display, 'block')
|
||||
t.is(container.querySelector('.loading-indicator').style.display, 'none')
|
||||
})
|
||||
|
||||
test('#showLoadingIndicator remove events', (t) => {
|
||||
const container = t.context.container
|
||||
const loadingIndicator = container.querySelector('.loading-indicator')
|
||||
t.is(loadingIndicator.style.display, 'none')
|
||||
showLoadingIndicator(container)
|
||||
t.is(loadingIndicator.style.display, 'block')
|
||||
})
|
||||
|
||||
test('#hideErrorMessages remove events', (t) => {
|
||||
const container = t.context.container
|
||||
const generalErrorMessage = container.querySelector('.general-error')
|
||||
const groupNotFoundErrorMessage = container.querySelector('.group-not-found')
|
||||
generalErrorMessage.style.display = 'block'
|
||||
groupNotFoundErrorMessage.style.display = 'block'
|
||||
t.is(generalErrorMessage.style.display, 'block')
|
||||
t.is(groupNotFoundErrorMessage.style.display, 'block')
|
||||
hideErrorMessages(container)
|
||||
t.is(generalErrorMessage.style.display, 'none')
|
||||
t.is(groupNotFoundErrorMessage.style.display, 'none')
|
||||
})
|
91
source/front/events-displayer.js
Normal file
91
source/front/events-displayer.js
Normal file
@ -0,0 +1,91 @@
|
||||
import Formatter from './formatter.js'
|
||||
import { createAnchorElement } from './html-creator.js'
|
||||
|
||||
export function clearEventsList(container) {
|
||||
const list = container.querySelector('ul')
|
||||
list.replaceChildren()
|
||||
}
|
||||
|
||||
export function displayEvents({ events, document, container, maxEventsCount }) {
|
||||
hideLoadingIndicator(container)
|
||||
|
||||
const isShortOffsetNameShown =
|
||||
window.MOBILIZON_CONNECTOR.isShortOffsetNameShown
|
||||
const locale = window.MOBILIZON_CONNECTOR.locale
|
||||
const timeZone = window.MOBILIZON_CONNECTOR.timeZone
|
||||
|
||||
const eventsCount = Math.min(maxEventsCount, events.length)
|
||||
const list = container.querySelector('ul')
|
||||
for (let i = 0; i < eventsCount; i++) {
|
||||
const li = document.createElement('li')
|
||||
|
||||
const a = createAnchorElement({
|
||||
document,
|
||||
text: events[i].title,
|
||||
url: events[i].url,
|
||||
})
|
||||
li.appendChild(a)
|
||||
|
||||
const br = document.createElement('br')
|
||||
li.appendChild(br)
|
||||
|
||||
const date = Formatter.formatDate({
|
||||
locale,
|
||||
start: events[i].beginsOn,
|
||||
end: events[i].endsOn,
|
||||
timeZone,
|
||||
isShortOffsetNameShown,
|
||||
})
|
||||
const textnode = document.createTextNode(date)
|
||||
li.appendChild(textnode)
|
||||
|
||||
if (events[i].physicalAddress) {
|
||||
const location = Formatter.formatLocation({
|
||||
description: events[i].physicalAddress.description,
|
||||
locality: events[i].physicalAddress.locality,
|
||||
})
|
||||
if (location) {
|
||||
const brBeforeLocation = document.createElement('br')
|
||||
li.appendChild(brBeforeLocation)
|
||||
|
||||
const textnodeLocation = document.createTextNode(location)
|
||||
li.appendChild(textnodeLocation)
|
||||
}
|
||||
}
|
||||
|
||||
list.appendChild(li)
|
||||
}
|
||||
}
|
||||
|
||||
export function displayErrorMessage({ data, container }) {
|
||||
hideLoadingIndicator(container)
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(data, 'response') &&
|
||||
Object.prototype.hasOwnProperty.call(data.response, 'errors') &&
|
||||
data.response.errors.length > 0 &&
|
||||
Object.prototype.hasOwnProperty.call(data.response.errors[0], 'code') &&
|
||||
data.response.errors[0].code === 'group_not_found'
|
||||
) {
|
||||
const message = container.querySelector('.group-not-found')
|
||||
message.style.display = 'block'
|
||||
} else {
|
||||
const message = container.querySelector('.general-error')
|
||||
message.style.display = 'block'
|
||||
console.error(data)
|
||||
}
|
||||
}
|
||||
|
||||
export function showLoadingIndicator(container) {
|
||||
const indicator = container.querySelector('.loading-indicator')
|
||||
indicator.style.display = 'block'
|
||||
}
|
||||
|
||||
function hideLoadingIndicator(container) {
|
||||
const indicator = container.querySelector('.loading-indicator')
|
||||
indicator.style.display = 'none'
|
||||
}
|
||||
|
||||
export function hideErrorMessages(container) {
|
||||
container.querySelector('.group-not-found').style.display = 'none'
|
||||
container.querySelector('.general-error').style.display = 'none'
|
||||
}
|
73
source/front/formatter-test.js
Normal file
73
source/front/formatter-test.js
Normal file
@ -0,0 +1,73 @@
|
||||
import test from 'ava'
|
||||
import Formatter from './formatter.js'
|
||||
|
||||
test('#formatDate one date', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-15T15:30:00Z',
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate one date with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-15T15:30:00Z',
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatDate two dates', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-16T15:30:00Z',
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 16/04/2021 15:30')
|
||||
})
|
||||
|
||||
test('#formatDate two dates with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: '2021-04-16T15:30:00Z',
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 - 16/04/2021 15:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatDate second date is null', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: null,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30')
|
||||
})
|
||||
|
||||
test('#formatDate second date is null with short offset name', (t) => {
|
||||
const date = Formatter.formatDate({
|
||||
start: '2021-04-15T10:30:00Z',
|
||||
end: null,
|
||||
isShortOffsetNameShown: true,
|
||||
})
|
||||
t.is(date, '15/04/2021 10:30 (UTC)')
|
||||
})
|
||||
|
||||
test('#formatLocation both parameters', (t) => {
|
||||
const location = Formatter.formatLocation({ description: 'a', locality: 'b' })
|
||||
t.is(location, 'a, b')
|
||||
})
|
||||
|
||||
test('#formatLocation description only', (t) => {
|
||||
const location = Formatter.formatLocation({ description: 'a' })
|
||||
t.is(location, 'a')
|
||||
})
|
||||
|
||||
test('#formatLocation description with space only', (t) => {
|
||||
const location = Formatter.formatLocation({ description: ' ' })
|
||||
t.is(location, '')
|
||||
})
|
||||
|
||||
test('#formatLocation locality only', (t) => {
|
||||
const location = Formatter.formatLocation({ locality: 'a' })
|
||||
t.is(location, 'a')
|
||||
})
|
45
source/front/formatter.js
Normal file
45
source/front/formatter.js
Normal file
@ -0,0 +1,45 @@
|
||||
import DateTimeWrapper from './date-time-wrapper.js'
|
||||
|
||||
export default class Formatter {
|
||||
static formatDate({ locale, timeZone, start, end, isShortOffsetNameShown }) {
|
||||
const startDateTime = new DateTimeWrapper({
|
||||
locale,
|
||||
text: start,
|
||||
timeZone,
|
||||
})
|
||||
let dateText = startDateTime.getShortDate()
|
||||
dateText += ' ' + startDateTime.get24Time()
|
||||
if (!end && isShortOffsetNameShown) {
|
||||
dateText += ' (' + startDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
|
||||
if (end) {
|
||||
const endDateTime = new DateTimeWrapper({ locale, text: end, timeZone })
|
||||
if (!startDateTime.equalsDate(endDateTime)) {
|
||||
dateText += ' - '
|
||||
dateText += endDateTime.getShortDate() + ' '
|
||||
} else {
|
||||
dateText += ' - '
|
||||
}
|
||||
dateText += endDateTime.get24Time()
|
||||
if (isShortOffsetNameShown) {
|
||||
dateText += ' (' + endDateTime.getShortOffsetName() + ')'
|
||||
}
|
||||
}
|
||||
return dateText
|
||||
}
|
||||
|
||||
static formatLocation({ description, locality }) {
|
||||
let location = ''
|
||||
if (description && description.trim()) {
|
||||
location += description.trim()
|
||||
}
|
||||
if (location && locality) {
|
||||
location += ', '
|
||||
}
|
||||
if (locality) {
|
||||
location += locality
|
||||
}
|
||||
return location
|
||||
}
|
||||
}
|
@ -1,15 +1,13 @@
|
||||
import test from 'ava'
|
||||
import { JSDOM } from 'jsdom'
|
||||
import browserEnv from 'browser-env'
|
||||
|
||||
import { createAnchorElement } from './html-creator'
|
||||
|
||||
let document
|
||||
import { createAnchorElement } from './html-creator.js'
|
||||
|
||||
test.beforeEach(() => {
|
||||
document = new JSDOM().window.document
|
||||
browserEnv()
|
||||
})
|
||||
|
||||
test('#createAnchorElement usual parameters', t => {
|
||||
test('#createAnchorElement usual parameters', (t) => {
|
||||
const a = createAnchorElement({ document, text: 'a', url: 'b' })
|
||||
t.is(a.tagName, 'A')
|
||||
t.is(a.innerHTML, 'a')
|
53
source/includes/Api.php
Normal file
53
source/includes/Api.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
class Api {
|
||||
public static function init() {
|
||||
add_action('rest_api_init', 'MobilizonConnector\Api::init_api');
|
||||
}
|
||||
|
||||
public static function init_api() {
|
||||
register_rest_route(
|
||||
NAME . '/v1',
|
||||
'/events',
|
||||
[
|
||||
'methods' => 'GET',
|
||||
'callback' => 'MobilizonConnector\Api::get_events',
|
||||
'args' => [
|
||||
'eventsCount' => [
|
||||
'required' => true,
|
||||
'validate_callback' => function($param, $request, $key) {
|
||||
return is_numeric($param) && $param > 0;
|
||||
}
|
||||
],
|
||||
'groupName' => [
|
||||
'validate_callback' => function($param, $request, $key) {
|
||||
return !is_numeric($param);
|
||||
}
|
||||
]
|
||||
],
|
||||
'permission_callback' => '__return_true',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function get_events($request) {
|
||||
$eventsCount = $request['eventsCount'];
|
||||
$groupName = isset($request['groupName']) ? $request['groupName'] : '';
|
||||
|
||||
$url = Settings::getUrl();
|
||||
|
||||
try {
|
||||
if ($groupName) {
|
||||
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
|
||||
} else {
|
||||
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||
}
|
||||
return $events;
|
||||
} catch (GeneralException $e) {
|
||||
return new \WP_Error('events_not_loading', 'The events could not be loaded!', array('status' => 500));
|
||||
} catch (GroupNotFoundException $e) {
|
||||
return new \WP_Error('group_not_found', sprintf('The group "%s" could not be found!', $groupName), array('status' => 404));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
const DEFAULT_EVENTS_COUNT = 5;
|
||||
const NAME = '<wordpress-name>';
|
||||
const NICE_NAME = '<wordpress-nice-name>';
|
34
source/includes/DateTimeWrapper.php
Normal file
34
source/includes/DateTimeWrapper.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class DateTimeWrapper {
|
||||
private $dateTime;
|
||||
private $locale;
|
||||
private $timeZone;
|
||||
|
||||
public function __construct(string $text, string $locale = 'en-GB', string $timeZone = 'utc') {
|
||||
if (!$locale) {
|
||||
$locale = 'en-GB';
|
||||
}
|
||||
if (!$timeZone) {
|
||||
$timeZone = 'utc';
|
||||
}
|
||||
$this->dateTime = new \DateTime($text);
|
||||
$this->locale = $locale;
|
||||
$this->timeZone = new \DateTimeZone($timeZone);
|
||||
}
|
||||
|
||||
public function get24Time(): string {
|
||||
$formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT, $this->timeZone);
|
||||
return $formatter->format($this->dateTime);
|
||||
}
|
||||
|
||||
public function getShortDate(): string {
|
||||
$formatter = \IntlDateFormatter::create($this->locale, \IntlDateFormatter::SHORT, \IntlDateFormatter::NONE, $this->timeZone);
|
||||
return $formatter->format($this->dateTime);
|
||||
}
|
||||
|
||||
public function getTimeZoneName(): string {
|
||||
return $this->timeZone->getName();
|
||||
}
|
||||
}
|
19
source/includes/EventsCache.php
Normal file
19
source/includes/EventsCache.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class EventsCache {
|
||||
|
||||
private static $MAX_AGE_IN_S = 120;
|
||||
|
||||
public static function set(array $parameters, mixed $data): void {
|
||||
// md5 is used as key must be 172 characters or fewer in length.
|
||||
$key = md5(json_encode($parameters));
|
||||
set_transient($key, $data, self::$MAX_AGE_IN_S);
|
||||
}
|
||||
|
||||
public static function get(array $parameters): mixed {
|
||||
$key = md5(json_encode($parameters));
|
||||
$data = get_transient($key);
|
||||
return $data;
|
||||
}
|
||||
}
|
65
source/includes/EventsListBlock.php
Normal file
65
source/includes/EventsListBlock.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
class EventsListBlock {
|
||||
|
||||
public static function initAndReturnScriptName(): string {
|
||||
$scriptName = NAME . '-block-starter';
|
||||
wp_register_script($scriptName, plugins_url(NAME . '/front/block-events-loader.js'), [
|
||||
'wp-block-editor',
|
||||
'wp-blocks',
|
||||
'wp-components',
|
||||
'wp-i18n'
|
||||
]);
|
||||
register_block_type(NAME . '/events-list', [
|
||||
'api_version' => 2,
|
||||
'title' => __('Events List', 'connector-mobilizon'),
|
||||
'description' => __('A list of the upcoming events of the connected Mobilizon instance.', 'connector-mobilizon'),
|
||||
'category' => 'widgets',
|
||||
'icon' => 'list-view',
|
||||
'supports' => [
|
||||
'html' => false
|
||||
],
|
||||
'attributes' => [
|
||||
'eventsCount' => [
|
||||
'type' => 'number',
|
||||
'default' => 3,
|
||||
],
|
||||
'groupName' => [
|
||||
'type' => 'string',
|
||||
],
|
||||
],
|
||||
'editor_script' => $scriptName,
|
||||
'render_callback' => 'MobilizonConnector\EventsListBlock::render',
|
||||
]);
|
||||
return $scriptName;
|
||||
}
|
||||
|
||||
public static function render($block_attributes, $content) {
|
||||
$url = Settings::getUrl();
|
||||
$eventsCount = $block_attributes['eventsCount'];
|
||||
$groupName = isset($block_attributes['groupName']) ? $block_attributes['groupName'] : '';
|
||||
|
||||
ob_start();
|
||||
try {
|
||||
if ($groupName) {
|
||||
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
|
||||
} else {
|
||||
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||
}
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$locale = get_locale();
|
||||
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
|
||||
$timeZone = wp_timezone_string();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-not-loaded.php';
|
||||
} catch (GroupNotFoundException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-group-not-found.php';
|
||||
}
|
||||
$output = ob_get_clean();
|
||||
return $output;
|
||||
}
|
||||
}
|
@ -1,11 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class EventsListShortcut {
|
||||
|
||||
public static function init() {
|
||||
@ -24,16 +19,29 @@ class EventsListShortcut {
|
||||
), $atts
|
||||
);
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $atts_with_overriden_defaults['events-count'];
|
||||
$locale = str_replace('_', '-', get_locale());
|
||||
$groupName = $atts_with_overriden_defaults['group-name'];
|
||||
$url = Settings::getUrl();
|
||||
$timeZone = wp_timezone_string();
|
||||
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
|
||||
$eventsCount = $atts_with_overriden_defaults['events-count'];
|
||||
$groupName = $atts_with_overriden_defaults['group-name'];
|
||||
|
||||
ob_start();
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
try {
|
||||
if ($groupName) {
|
||||
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
|
||||
} else {
|
||||
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||
}
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$locale = get_locale();
|
||||
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
|
||||
$timeZone = wp_timezone_string();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-not-loaded.php';
|
||||
} catch (GroupNotFoundException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-group-not-found.php';
|
||||
}
|
||||
$output = ob_get_clean();
|
||||
return $output;
|
||||
}
|
@ -1,11 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class EventsListWidget extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
@ -25,15 +20,28 @@ class EventsListWidget extends \WP_Widget {
|
||||
echo $args['before_title'].apply_filters('widget_title', $options['title']).$args['after_title'];
|
||||
}
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $options['eventsCount'];
|
||||
$locale = str_replace('_', '-', get_locale());
|
||||
$groupName = isset($options['groupName']) ? $options['groupName'] : '';
|
||||
$url = Settings::getUrl();
|
||||
$timeZone = wp_timezone_string();
|
||||
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
|
||||
$eventsCount = $options['eventsCount'];
|
||||
$groupName = isset($options['groupName']) ? $options['groupName'] : '';
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
try {
|
||||
if ($groupName) {
|
||||
$events = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName);
|
||||
} else {
|
||||
$events = GraphQlClient::get_upcoming_events($url, (int) $eventsCount);
|
||||
}
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$locale = get_locale();
|
||||
$isShortOffsetNameShown = Settings::isShortOffsetNameShown();
|
||||
$timeZone = wp_timezone_string();
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
} catch (GeneralException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-not-loaded.php';
|
||||
} catch (GroupNotFoundException $e) {
|
||||
require dirname(__DIR__) . '/view/events-list-group-not-found.php';
|
||||
}
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
42
source/includes/Formatter.php
Normal file
42
source/includes/Formatter.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class Formatter
|
||||
{
|
||||
public static function format_date(string $locale, string $timeZone, string $start, ?string $end, bool $isShortOffsetNameShown): string {
|
||||
$startDateTime = new DateTimeWrapper($start, $locale, $timeZone);
|
||||
$dateText = $startDateTime->getShortDate();
|
||||
$dateText .= ' ' . $startDateTime->get24Time();
|
||||
if (!$end && $isShortOffsetNameShown) {
|
||||
$dateText .= ' (' . $startDateTime->getTimeZoneName() . ')';
|
||||
}
|
||||
if ($end) {
|
||||
$endDateTime = new DateTimeWrapper($end, $locale, $timeZone);
|
||||
if ($startDateTime->getShortDate() != $endDateTime->getShortDate()) {
|
||||
$dateText .= ' - ';
|
||||
$dateText .= $endDateTime->getShortDate() . ' ';
|
||||
} else {
|
||||
$dateText .= ' - ';
|
||||
}
|
||||
$dateText .= $endDateTime->get24Time();
|
||||
if ($isShortOffsetNameShown) {
|
||||
$dateText .= ' (' . $endDateTime->getTimeZoneName() . ')';
|
||||
}
|
||||
}
|
||||
return $dateText;
|
||||
}
|
||||
|
||||
public static function format_location(string $description, string $locality): string {
|
||||
$location = '';
|
||||
if ($description && trim($description)) {
|
||||
$location .= trim($description);
|
||||
}
|
||||
if ($location && $locality) {
|
||||
$location .= ', ';
|
||||
}
|
||||
if ($locality) {
|
||||
$location .= $locality;
|
||||
}
|
||||
return $location;
|
||||
}
|
||||
}
|
113
source/includes/GraphQlClient.php
Normal file
113
source/includes/GraphQlClient.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
final class GraphQlClient {
|
||||
|
||||
public static function query(string $endpoint, string $query, array $variables = [], ?string $token = null): array
|
||||
{
|
||||
$headers = ['Content-Type: application/json'];
|
||||
if ($token !== null) {
|
||||
$headers[] = "Authorization: bearer $token";
|
||||
}
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'POST',
|
||||
'header' => $headers,
|
||||
'content' => json_encode(['query' => $query, 'variables' => $variables]),
|
||||
]
|
||||
]);
|
||||
$data = @file_get_contents($endpoint, false, $context);
|
||||
|
||||
if ($data === false) {
|
||||
$error = error_get_last();
|
||||
throw new \ErrorException($error['message'], $error['type']);
|
||||
}
|
||||
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
public static function get_upcoming_events(string $url, int $limit): array {
|
||||
$query = <<<'GRAPHQL'
|
||||
query ($limit: Int) {
|
||||
events(limit: $limit) {
|
||||
elements {
|
||||
id,
|
||||
title,
|
||||
url,
|
||||
beginsOn,
|
||||
endsOn,
|
||||
physicalAddress {
|
||||
description,
|
||||
locality
|
||||
}
|
||||
},
|
||||
total
|
||||
}
|
||||
}
|
||||
GRAPHQL;
|
||||
|
||||
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'limit' => $limit]);
|
||||
if ($cachedEvents !== false) {
|
||||
return $cachedEvents;
|
||||
}
|
||||
|
||||
$endpoint = $url . '/api';
|
||||
$data = self::query($endpoint, $query, ['limit' => $limit]);
|
||||
self::checkData($data);
|
||||
|
||||
$events = $data['data']['events']['elements'];
|
||||
EventsCache::set(['url' => $url, 'query' => $query, 'limit' => $limit], $events);
|
||||
return $events;
|
||||
}
|
||||
|
||||
public static function get_upcoming_events_by_group_name(string $url, int $limit, string $groupName): array {
|
||||
$query = <<<'GRAPHQL'
|
||||
query ($afterDatetime: DateTime, $groupName: String!, $limit: Int) {
|
||||
group(preferredUsername: $groupName) {
|
||||
organizedEvents(afterDatetime: $afterDatetime, limit: $limit) {
|
||||
elements {
|
||||
id,
|
||||
title,
|
||||
url,
|
||||
beginsOn,
|
||||
endsOn,
|
||||
physicalAddress {
|
||||
description,
|
||||
locality
|
||||
}
|
||||
},
|
||||
total
|
||||
}
|
||||
}
|
||||
}
|
||||
GRAPHQL;
|
||||
|
||||
$afterDatetime = date(\DateTime::ISO8601);
|
||||
|
||||
$cachedEvents = EventsCache::get(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
|
||||
if ($cachedEvents !== false) {
|
||||
return $cachedEvents;
|
||||
}
|
||||
|
||||
$endpoint = $url . '/api';
|
||||
$data = self::query($endpoint, $query, ['afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]);
|
||||
self::checkData($data);
|
||||
|
||||
$events = $data['data']['group']['organizedEvents']['elements'];
|
||||
EventsCache::set(['url' => $url, 'query' => $query, 'afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit], $events);
|
||||
return $events;
|
||||
}
|
||||
|
||||
private static function checkData($data) {
|
||||
if (isset($data['errors'])) {
|
||||
if (count($data['errors']) > 0 &&
|
||||
isset($data['errors'][0]['code']) &&
|
||||
$data['errors'][0]['code'] === 'group_not_found') {
|
||||
throw new GroupNotFoundException(serialize($data['errors'][0]));
|
||||
} else {
|
||||
throw new GeneralException(serialize($data['errors']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Settings {
|
||||
|
||||
private static $DEFAULT_OPTION_URL = 'https://mobilizon.fr';
|
12
source/includes/exceptions/GeneralException.php
Normal file
12
source/includes/exceptions/GeneralException.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
class GeneralException extends \Exception {
|
||||
public function __construct($message, $code = 0, Throwable $previous = null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
|
||||
}
|
||||
}
|
12
source/includes/exceptions/GroupNotFoundException.php
Normal file
12
source/includes/exceptions/GroupNotFoundException.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
class GroupNotFoundException extends \Exception {
|
||||
public function __construct($message, $code = 0, Throwable $previous = null) {
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
public function __toString() {
|
||||
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
|
||||
}
|
||||
}
|
@ -15,27 +15,119 @@ License: <wordpress-license>
|
||||
<wordpress-nice-name> allows you to display the upcoming events of [Mobilizon](https://joinmobilizon.org/), which is a federated event listing platform, on your WordPress website.
|
||||
|
||||
Features
|
||||
- Display events as widget and as shortcut
|
||||
- Display events' title, date, and location if available
|
||||
- Cache requests' responses for 2 minutes in the browser's `sessionStorage`
|
||||
- Configure number of events to show per widget and per shortcut
|
||||
- Optionally filter events by a specific group per widget and per shortcut
|
||||
- Display events as Gutenberg block, as widget and as shortcut
|
||||
- Display events' title with link, date, and location, if available
|
||||
- Cache requests' responses for 2 minutes in the database
|
||||
- Configure number of events to show per block, per widget and per shortcut
|
||||
- Optionally filter events by a specific public group per block, per widget and per shortcut
|
||||
- Set the URL of the Mobilizon instance in the settings
|
||||
- Toggle adding named offset in brackets after the time in the settings
|
||||
|
||||
Shortcut format with limiting the number of events to show to 3 for example: `[<wordpress-name>-events-list events-count=3]`
|
||||
Optionally, you can only show the events of a specific group by indicatings its name: `[<wordpress-name>-events-list events-count=3 group-name="mygroup"]`
|
||||
|
||||
The source code is available on [Github](https://github.com/dwaxweiler/connector-mobilizon).
|
||||
|
||||
## Installation
|
||||
|
||||
Shortcut format with limiting the number of events to show to 3 for example: `[<wordpress-name>-events-list events-count=3]`
|
||||
|
||||
Optionally, you can only show the events of a specific group by indicatings its name: `[<wordpress-name>-events-list events-count=3 group-name="mygroup"]`
|
||||
You have to use their username, e.g. `@nosliensvivants`, and append the name of their instance if they use a different one, e.g. `@yaam_berlin@mobilize.berlin`.
|
||||
|
||||
## Screenshots
|
||||
1. Events list
|
||||
2. General settings
|
||||
3. Widget creation
|
||||
4. Shortcut creation
|
||||
5. Gutenberg block in editor
|
||||
|
||||
## Changelog
|
||||
|
||||
### [1.0.0]
|
||||
#### Added
|
||||
- Display name of group when it cannot be found
|
||||
#### Changed
|
||||
- Let backend do requests to API of Mobilizon instance for increased privacy
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Fix displaying more than one block in the editor
|
||||
|
||||
### [0.11.5]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.5
|
||||
|
||||
### [0.11.4]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.4
|
||||
- Update dependencies
|
||||
|
||||
### [0.11.3]
|
||||
#### Fixed
|
||||
- Clean up distributed files
|
||||
|
||||
### [0.11.2]
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.3
|
||||
|
||||
### [0.11.1]
|
||||
#### Fixed
|
||||
- Revert minimum PHP version to 7.4 to allow some more time for upgrading PHP
|
||||
|
||||
### [0.11.0]
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.2
|
||||
#### Security
|
||||
- Set minimum PHP version to oldest stable 8.0
|
||||
|
||||
### [0.10.1]
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 6.1
|
||||
- Update dependencies
|
||||
|
||||
### [0.10.0]
|
||||
#### Added
|
||||
- Add Gutenberg events list block
|
||||
- Show loading indicator during request
|
||||
#### Changed
|
||||
- Set list style type to none and left padding to zero for all occurences
|
||||
- Move shortcut usage description into installation section in `readme.txt`
|
||||
- Update dependencies
|
||||
|
||||
### [0.9.1] - 2020-05-19
|
||||
#### Fixed
|
||||
- Fix WordPress compatibility version number
|
||||
|
||||
### [0.9.0] - 2020-05-19
|
||||
#### Added
|
||||
- Improve explanation of group name filter
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Confirm compatibility with WordPress 6.0
|
||||
#### Fixed
|
||||
- Fix displaying error message for the case the group is not found
|
||||
|
||||
### [0.8.0] - 2022-01-09
|
||||
#### Added
|
||||
- Add support for older browsers using babel
|
||||
#### Changed
|
||||
- Confirm compatibility with WordPress 5.9
|
||||
- Update dependencies
|
||||
#### Fixed
|
||||
- Use ES modules correctly
|
||||
- Trim events' location
|
||||
|
||||
### [0.7.0] - 2021-12-23
|
||||
#### Added
|
||||
- Add specific error message for the case the group is not found
|
||||
- Add code formatter prettier
|
||||
#### Changed
|
||||
- Update dependencies
|
||||
- Simplify build process
|
||||
#### Fixed
|
||||
- Fix Invalid DateTime on event end time being null
|
||||
#### Security
|
||||
- Set minimum PHP version to oldest stable 7.4
|
||||
|
||||
### [0.6.2] - 2021-08-24
|
||||
#### Changed
|
||||
- Update dependencies
|
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/includes/constants.php';
|
||||
require_once __DIR__ . '/includes/settings.php';
|
||||
require_once __DIR__ . '/includes/Constants.php';
|
||||
require_once __DIR__ . '/includes/Settings.php';
|
||||
|
||||
// If uninstall.php is not called by WordPress, exit.
|
||||
if (!defined('WP_UNINSTALL_PLUGIN')) {
|
11
source/view/events-list-group-not-found.php
Normal file
11
source/view/events-list-group-not-found.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list">
|
||||
<?php echo esc_html(sprintf(__('The group "%s" could not be found!', 'connector-mobilizon'), $groupName)); ?>
|
||||
</div>
|
11
source/view/events-list-not-loaded.php
Normal file
11
source/view/events-list-not-loaded.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list">
|
||||
<?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?>
|
||||
</div>
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
23
source/view/events-list.php
Normal file
23
source/view/events-list.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list">
|
||||
<ul style="list-style-type: none; padding-left: 0;">
|
||||
<?php foreach($events as $event) { ?>
|
||||
<li>
|
||||
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html_e($event['title']); ?></a>
|
||||
<br>
|
||||
<?php echo esc_html_e(Formatter::format_date($locale, $timeZone, $event['beginsOn'], $event['endsOn'], $isShortOffsetNameShown)); ?>
|
||||
<?php if (isset($event['physicalAddress'])) { ?>
|
||||
<br>
|
||||
<?php echo esc_html_e(Formatter::format_location($event['physicalAddress']['description'], $event['physicalAddress']['locality'])) ?>
|
||||
<?php } ?>
|
||||
</li>
|
||||
<?php } ?>
|
||||
</ul>
|
||||
</div>
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
47
tests/DateTimeWrapperTest.php
Normal file
47
tests/DateTimeWrapperTest.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\DateTimeWrapper;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class DateTimeWrapperTest extends TestCase {
|
||||
public function testCanGet24TimeForUsualTime(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z');
|
||||
$this->assertSame('16:45', $d->get24Time());
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDate(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate());
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDateWithLocaleWithUnderscore(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate(), 'en_GB');
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDateWithTimezoneString(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', 'Europe/Rome');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate());
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDateWithNamedOffset(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', 'UTC');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate());
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDateWithOffset(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', '+02:00');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate());
|
||||
}
|
||||
|
||||
public function testCanGetShortDateForUsualDateWithEmptyTimezone(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z', 'en-GB', '');
|
||||
$this->assertSame('24/12/2020', $d->getShortDate());
|
||||
}
|
||||
|
||||
public function testCanGetShortOffsetNameForUsualTime(): void {
|
||||
$d = new DateTimeWrapper('2020-12-24T16:45:00Z');
|
||||
$this->assertSame('UTC', $d->getTimeZoneName());
|
||||
}
|
||||
}
|
52
tests/FormatterTest.php
Normal file
52
tests/FormatterTest.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use MobilizonConnector\Formatter;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class FormatterTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testCanDateFormatOneDate(): void {
|
||||
$this->assertSame('15/04/2021 10:30 - 15:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false));
|
||||
}
|
||||
|
||||
public function testCanDateFormatOneDateWithOffset(): void {
|
||||
$this->assertSame('15/04/2021 10:30 - 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true));
|
||||
}
|
||||
|
||||
public function testCanDateFormatOneDateWithTimeZoneOffset(): void {
|
||||
$this->assertSame('15/04/2021 11:30 - 16:30', Formatter::format_date('en-GB', '+01:00', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false));
|
||||
}
|
||||
|
||||
public function testCanDateFormatTwoDates(): void {
|
||||
$this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', false));
|
||||
}
|
||||
|
||||
public function testCanDateFormatTwoDatesWithOffset(): void {
|
||||
$this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true));
|
||||
}
|
||||
|
||||
public function testCanDateFormatWhenSecondDateIsNull(): void {
|
||||
$this->assertSame('15/04/2021 10:30', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, false));
|
||||
}
|
||||
|
||||
public function testCanDateFormatWhenSecondDateIsNullWithOffset(): void {
|
||||
$this->assertSame('15/04/2021 10:30 (UTC)', Formatter::format_date('en-GB', 'UTC', '2021-04-15T10:30:00Z', null, true));
|
||||
}
|
||||
|
||||
public function testCanLocationFormatBothParameters(): void {
|
||||
$this->assertSame('a, b', Formatter::format_location('a', 'b'));
|
||||
}
|
||||
|
||||
public function testLocationFormatDescriptionOnly(): void {
|
||||
$this->assertSame('a', Formatter::format_location('a', ''));
|
||||
}
|
||||
|
||||
public function testLocationFormatDescriptionWithSpaceOnly(): void {
|
||||
$this->assertSame('', Formatter::format_location(' ', ''));
|
||||
}
|
||||
|
||||
public function testLocationFormatLocalityOnly(): void {
|
||||
$this->assertSame('a', Formatter::format_location('', 'a'));
|
||||
}
|
||||
}
|
46
webpack.config.cjs
Normal file
46
webpack.config.cjs
Normal file
@ -0,0 +1,46 @@
|
||||
/* eslint-disable no-undef */
|
||||
const path = require('path')
|
||||
|
||||
const CopyPlugin = require('copy-webpack-plugin')
|
||||
|
||||
const FOLDER_SOURCE = './source'
|
||||
|
||||
module.exports = {
|
||||
entry: {
|
||||
'block-events-loader': FOLDER_SOURCE + '/front/block-events-loader.js',
|
||||
},
|
||||
output: {
|
||||
filename: '[name].js',
|
||||
path: path.resolve(__dirname, 'build/front'),
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.m?js$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'babel-loader',
|
||||
options: {
|
||||
presets: [['@babel/preset-env', { targets: 'defaults' }]],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
plugins: [
|
||||
new CopyPlugin({
|
||||
patterns: [
|
||||
{
|
||||
context: FOLDER_SOURCE,
|
||||
from: '**/*.php',
|
||||
to: '../',
|
||||
},
|
||||
{
|
||||
context: FOLDER_SOURCE,
|
||||
from: '**/*.txt',
|
||||
to: '../',
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
}
|
Reference in New Issue
Block a user