-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.php
111 lines (98 loc) · 3.44 KB
/
main.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
<?php
/*
Plugin Name: Loglib
Plugin URI: https://loglib.io/plugin
Description: a web analytics platform.
Version: 1.0.0
Author: Getasew
Author URI: https://www.linkedin.com/in/geta-walelign/
License: GPL2
*/
// Add settings page to WordPress dashboard
function loglib_add_settings_page() {
add_options_page(
'Loglib', // Page title
'Loglib', // Menu title
'manage_options', // Capability required to access the page
'loglib-settings', // Menu slug
'loglib_render_settings_page' // Callback function to render the page content
);
}
add_action('admin_menu', 'loglib_add_settings_page');
// Render the settings page content
function loglib_render_settings_page() {
?>
<div class="wrap">
<h1>Loglib Settings</h1>
<form method="post" action="<?php echo plugin_dir_url(__FILE__) . 'handle_form.php'; ?>">
<?php
// Output the settings fields
settings_fields('loglib-settings-group');
do_settings_sections('loglib-settings');
submit_button();
?>
</form>
</div>
<?php
}
// Register settings and fields
function loglib_register_settings() {
// Register a settings group
register_setting(
'loglib-settings-group', // Option group
'loglib-settings', // Option name
'loglib_validate_settings' // Validation callback (optional)
);
// Add a settings section
add_settings_section(
'loglib-settings-section', // Section ID
'LogLib Settings', // Section title
'loglib_render_section', // Callback function to render section content
'loglib-settings' // Page slug
);
// Add a settings field
add_settings_field(
'loglib_host', // Field ID
'LogLib Host', // Field label
'loglib_render_field', // Callback function to render field content
'loglib-settings', // Page slug
'loglib-settings-section' // Section ID
);
add_settings_field(
'loglib_id', // Field ID
'LogLib id', // Field label
'loglib_render_id_field', // Callback function to render field content
'loglib-settings', // Page slug
'loglib-settings-section' // Section ID
);
}
add_action('admin_init', 'loglib_register_settings');
// Render the settings section content
function loglib_render_section() {
echo 'Loglib setting section to enter your Loglib id and Loglib host.';
}
// Render the settings field content
function loglib_render_field() {
$value = get_option('loglib-settings');
echo '<input type="text" name="loglib_host" value="' . esc_attr($value['loglib_host']) . '" required/>';
}
// Render the settings field content
function loglib_render_id_field() {
$value = get_option('loglib-settings');
echo '<input type="text" name="loglib_id" value="' . esc_attr($value['loglib_id']) . '"/>';
}
// Validate the settings
function loglib_validate_settings($input) {
return $input;
}
// Inject HTML tag into header
function loglib_inject_html_tag() {
$jsonFilePath = plugin_dir_path(__FILE__) . 'data.json';
$jsonData = file_get_contents($jsonFilePath);
$data = json_decode($jsonData);
$host=isset($data->host)?$data->host:"https://loglib.io";
$id=isset($data->id)?$data->id:"";
// https://loglib.io
echo '<script src="https://cdn.jsdelivr.net/npm/@loglib/tracker@latest/dist/index.global.js" data-id="'.$id.'"></script>';
}
add_action('wp_head', 'loglib_inject_html_tag');