-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwpct-bookmarks.php
More file actions
147 lines (121 loc) · 3.93 KB
/
wpct-bookmarks.php
File metadata and controls
147 lines (121 loc) · 3.93 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Plugin Name: Wpct BookMarks
* Plugin URI: https://git.coopdevs.org/codeccoop/wp/plugins/wpct-bookmarks
* Description: WordPress bookmarks
* Author: Còdec Cooperativa
* Author URI: https://www.codeccoop.org
* Text Domain: wpct-bm
* Domain Path: /languages
* Version: 1.0.0
*/
namespace WPCT_BM;
use Exception;
define("WPCT_BM_VERSION", "1.0.0");
require_once('includes/class-model-bookmark.php');
require_once('includes/class-model-list.php');
require_once('includes/shortcodes/BookMarkList.php');
require_once('includes/shortcodes/BookMark.php');
require_once('includes/shortcodes/UserLists.php');
require_once('includes/ajax/add-list.php');
require_once('includes/ajax/drop-list.php');
require_once('includes/ajax/save-bookmark.php');
require_once('includes/ajax/drop-bookmark.php');
require_once('includes/ajax/user-lists.php');
class Plugin
{
private $shortcodes = [];
public static function activate()
{
BookMarkList::create_table();
BookMark::create_table();
}
public static function deactivate()
{
}
public function on_load()
{
add_action('init', [$this, 'register_shortcodes']);
// add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']);
add_action('init', [$this, 'register_scripts']);
}
public function register_shortcodes()
{
$this->shortcodes[Shortcodes\BookMark::$tag] = new Shortcodes\BookMark();
$this->shortcodes[Shortcodes\BookMarkList::$tag] = new Shortcodes\BookMarkList();
$this->shortcodes[Shortcodes\UserLists::$tag] = new Shortcodes\UserLists();
}
public function register_scripts()
{
wp_register_script(
'wpct-bookmarks',
plugin_dir_url(__FILE__) . 'assets/js/index.js',
array(),
WPCT_BM_VERSION,
);
wp_localize_script(
'wpct-bookmarks',
'ajaxBookmarks',
[
'nonce' => wp_create_nonce('ajax-bookmarks'),
'ajax_url' => admin_url('admin-ajax.php'),
]
);
wp_register_style(
'wpct-bookmarks',
plugin_dir_url(__FILE__) . 'assets/css/index.css',
[],
WPCT_BM_VERSION,
);
}
}
add_action('wp_head', function () {
?>
<style>.wpct-bm-bookmark{visibility:hidden;pointer-events:none;}</style>
<?php
});
register_activation_hook(__FILE__, function () {
Plugin::activate();
});
register_deactivation_hook(__FILE__, function () {
Plugin::deactivate();
});
add_action('plugins_loaded', function () {
$plugin = new Plugin();
$plugin->on_load();
});
add_action('wpct_bm_drop_list', function ($list_id) {
BookMark::delete_by_list($list_id);
}, 10, 1);
add_action('delete_user', function ($user_id) {
BookMark::delete_by_user($user_id);
BookMarkList::delete_by_user($user_id);
}, 10, 1);
add_action('delete_post', function ($post_id) {
try {
BookMark::delete_by_post($post_id);
} catch (Exception) {
// do nothing
}
}, 10, 1);
add_filter('wpct_bm_user_lists', function ($lists, $user_id) {
try {
return BookMarkList::get_by_user($user_id);
} catch (Exception $e) {
if ($e->getCode() !== 404) {
throw $e;
}
}
$list = (new BookMarkList(['name' => 'favorites', 'user_id' => $user_id]))->save();
return [$list];
}, 10, 2);
add_action('init', function () {
load_plugin_textdomain('wpct-bm', false, dirname(plugin_basename(__FILE__)) . '/languages');
});
add_filter('load_textdomain_mofile', function ($mofile, $domain) {
if ('wpct-bm' === $domain && false !== strpos($mofile, WP_LANG_DIR . '/plugins/')) {
$locale = apply_filters('plugin_locale', determine_locale(), $domain);
$mofile = WP_PLUGIN_DIR . '/' . dirname(plugin_basename(__FILE__)) . '/languages/' . $domain . '-' . $locale . '.mo';
}
return $mofile;
}, 10, 2);