-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-plugin.php
121 lines (105 loc) · 3.3 KB
/
modal-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Plugin Name: Modal by Influactive
* Description: Ce plugin affiche une modal lors du chargement des pages.
* Version: 1.1
* Author: Influactive
* Author URI: https://www.influactive.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: influactive-modal
* Domain Path: /languages
**/
if (!defined('ABSPATH')) {
throw new RuntimeException("WordPress environment not loaded. Exiting...");
}
/**
* Enqueues the CSS and JavaScript assets required for the modal plugin.
*
* @return void
*/
function modal_plugin_assets(): void
{
wp_enqueue_style('modal-plugin-style', plugin_dir_url(__FILE__) . 'assets/css/style.min.css', array(), '1.1');
wp_enqueue_script('modal-plugin-script', plugin_dir_url(__FILE__) . 'assets/js/script.min.js', array(), '1.1', true);
}
add_action('wp_enqueue_scripts', 'modal_plugin_assets');
/**
* Enqueues the CSS asset required for the admin settings page of the modal plugin.
*
* @param string $hook The current admin page hook.
* @return void
*/
function modal_plugin_admin_assets(string $hook): void
{
if ($hook !== 'settings_page_modal-plugin') {
return;
}
wp_enqueue_style('modal-plugin-admin-style', plugin_dir_url(__FILE__) . 'assets/css/admin-style.min.css', array(), '1.1');
}
add_action('admin_enqueue_scripts', 'modal_plugin_admin_assets');
/**
* Adds an option page for the Modal Plugin to the WordPress admin menu.
*
* @return void
*/
function modal_plugin_menu(): void
{
add_options_page('Modal Plugin Settings', 'Modal Plugin', 'manage_options', 'modal-plugin', 'modal_plugin_options');
}
add_action('admin_menu', 'modal_plugin_menu');
/**
* Loads the admin settings page for the modal plugin.
*
* @return void
*/
function modal_plugin_options(): void
{
include 'admin-settings.php';
}
/**
* Displays the modal dialog on the page.
*
* @return void
*/
function display_modal(): void
{
$modal_title = get_option('modal_title') ?? '';
$modal_content = get_option('modal_content') ?? '';
echo '<dialog id="modal-dialog">
<h2>' . $modal_title . '</h2>
<p>' . $modal_content . '</p>
<button id="close-btn">X</button>
</dialog>';
}
add_action('wp_footer', 'display_modal');
/**
* Returns an array of plugin action links to be displayed on the plugins page.
*
* @param array $links An array of existing plugin action links.
* @return array An array of updated plugin action links with the "Settings" link added.
*/
function modal_plugin_settings_link(array $links): array
{
$settings_url = 'options-general.php?page=modal-plugin';
$settings_link = ["<a href='{$settings_url}'>Settings</a>"];
return array_merge($links, $settings_link);
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_{$plugin}", 'modal_plugin_settings_link');
/**
* Checks if the current user has the permission to edit posts.
*
* @return bool True if the current user has the permission to edit posts, false otherwise.
* @throws RuntimeException If the WordPress environment is not loaded.
*/
function checkUserPermissions(): bool
{
if (!defined('ABSPATH')) {
throw new RuntimeException("WordPress environment not loaded. Exiting...");
}
if (!current_user_can('edit_posts')) {
return false;
}
return true;
}