mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
first commit
This commit is contained in:
12
source/connector-mobilizon/includes/constants.php
Normal file
12
source/connector-mobilizon/includes/constants.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?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>';
|
||||
const TEXT_DOMAIN = '<wordpress-name>';
|
36
source/connector-mobilizon/includes/events-list-shortcut.php
Normal file
36
source/connector-mobilizon/includes/events-list-shortcut.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class EventsListShortcut {
|
||||
|
||||
public static function init() {
|
||||
add_shortcode(NAME . '-events-list', 'MobilizonConnector\EventsListShortcut::inflate');
|
||||
}
|
||||
|
||||
public static function inflate($atts = [], $content = null) {
|
||||
// Normalize attribute keys, lowercase.
|
||||
$atts = array_change_key_case((array) $atts, CASE_LOWER);
|
||||
|
||||
// Override default attributes with user attributes.
|
||||
$atts_with_overriden_defaults = shortcode_atts(
|
||||
array(
|
||||
'events-count' => DEFAULT_EVENTS_COUNT,
|
||||
), $atts
|
||||
);
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $atts_with_overriden_defaults['events-count'];
|
||||
$url = Settings::getUrl();
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
|
||||
ob_start();
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
$output = ob_get_clean();
|
||||
return $output;
|
||||
}
|
||||
}
|
55
source/connector-mobilizon/includes/events-list-widget.php
Normal file
55
source/connector-mobilizon/includes/events-list-widget.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class EventsListWidget extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
NAME . '-events-list',
|
||||
NICE_NAME . ' ' . esc_html__('Events List', TEXT_DOMAIN),
|
||||
array(
|
||||
'description' => __('A list of the upcoming events of the connected Mobilizon instance.', TEXT_DOMAIN),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $options) {
|
||||
echo $args['before_widget'];
|
||||
|
||||
if (!empty($options['title'])) {
|
||||
echo $args['before_title'].apply_filters('widget_title', $options['title']).$args['after_title'];
|
||||
}
|
||||
|
||||
$classNamePrefix = NAME;
|
||||
$eventsCount = $options['eventsCount'];
|
||||
$url = Settings::getUrl();
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list.php';
|
||||
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($options) {
|
||||
$title = !empty($options['title']) ? $options['title'] : esc_html__('Events', TEXT_DOMAIN);
|
||||
$eventsCount = !empty($options['eventsCount']) ? $options['eventsCount'] : DEFAULT_EVENTS_COUNT;
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
|
||||
require dirname(__DIR__) . '/view/events-list-widget/form.php';
|
||||
}
|
||||
|
||||
public function update($new_options, $old_options) {
|
||||
if (!current_user_can('edit_theme_options')) {
|
||||
return;
|
||||
}
|
||||
$options = array();
|
||||
$options['title'] = !empty($new_options['title']) ? sanitize_text_field($new_options['title']) : '';
|
||||
$options['eventsCount'] = !empty($new_options['eventsCount']) ? sanitize_text_field($new_options['eventsCount']) : 5;
|
||||
return $options;
|
||||
}
|
||||
}
|
100
source/connector-mobilizon/includes/settings.php
Normal file
100
source/connector-mobilizon/includes/settings.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace MobilizonConnector;
|
||||
|
||||
// Exit if this file is called directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Settings {
|
||||
|
||||
private static $DEFAULT_OPTION_URL = 'https://mobilizon.fr';
|
||||
private static $PAGE_NAME = 'wordpress_mobilizon';
|
||||
private static $OPTIONS_GROUP_NAME = 'wordpress_mobilizon';
|
||||
private static $OPTION_NAME_URL = 'wordpress_mobilizon_url';
|
||||
private static $SETTING_FIELD_NAME_URL = 'wordpress_mobilizon_field_url';
|
||||
private static $SETTINGS_SECTION_NAME = 'wordpress_mobilizon_section_general';
|
||||
|
||||
public static function init() {
|
||||
add_action('admin_init', 'MobilizonConnector\Settings::init_settings');
|
||||
add_action('admin_menu', 'MobilizonConnector\Settings::register_settings_page');
|
||||
}
|
||||
|
||||
public static function init_settings() {
|
||||
register_setting(
|
||||
self::$OPTIONS_GROUP_NAME,
|
||||
self::$OPTION_NAME_URL,
|
||||
'MobilizonConnector\Settings::validate_field_url'
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
self::$SETTINGS_SECTION_NAME,
|
||||
__('General Settings', TEXT_DOMAIN),
|
||||
'',
|
||||
self::$PAGE_NAME
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
self::$SETTING_FIELD_NAME_URL,
|
||||
__('URL', TEXT_DOMAIN),
|
||||
'MobilizonConnector\Settings::output_field_url',
|
||||
self::$PAGE_NAME,
|
||||
self::$SETTINGS_SECTION_NAME,
|
||||
array(
|
||||
'label_for' => self::$SETTING_FIELD_NAME_URL
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function output_field_url($args) {
|
||||
$textDomain = TEXT_DOMAIN;
|
||||
$url = self::getUrl();
|
||||
require dirname(__DIR__) . '/view/settings/url-field.php';
|
||||
}
|
||||
|
||||
public static function validate_field_url($input) {
|
||||
$validated = esc_url_raw($input);
|
||||
if ($validated !== $input) {
|
||||
add_settings_error(
|
||||
self::$OPTION_NAME_URL,
|
||||
'wordpress_mobilizon_field_url_error',
|
||||
__('The URL is invalid.', TEXT_DOMAIN),
|
||||
'error'
|
||||
);
|
||||
}
|
||||
if(substr($validated, -1) == '/') {
|
||||
$validated = substr($validated, 0, strlen($validated) - 1);
|
||||
}
|
||||
return $validated;
|
||||
}
|
||||
|
||||
public static function register_settings_page() {
|
||||
add_options_page(
|
||||
NICE_NAME . ' ' . __('Settings', TEXT_DOMAIN),
|
||||
NICE_NAME,
|
||||
'manage_options',
|
||||
NAME . '-settings',
|
||||
'MobilizonConnector\Settings::output_settings_page'
|
||||
);
|
||||
}
|
||||
|
||||
public static function output_settings_page() {
|
||||
if (!current_user_can('manage_options')) {
|
||||
return;
|
||||
}
|
||||
require dirname(__DIR__) . '/view/settings/page.php';
|
||||
}
|
||||
|
||||
public static function getUrl() {
|
||||
return get_option(self::$OPTION_NAME_URL);
|
||||
}
|
||||
|
||||
public static function setDefaultOptions() {
|
||||
add_option(self::$OPTION_NAME_URL, self::$DEFAULT_OPTION_URL);
|
||||
}
|
||||
|
||||
public static function deleteAllOptions() {
|
||||
delete_option(self::$OPTION_NAME_URL);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user