Issues (1358)

modules/Blogs/events.php (9 issues)

1
<?php
2
/**
3
 * @package  Blogs
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs\modules\Blogs;
9
use
10
	cs\Cache,
11
	cs\Config,
12
	cs\DB,
13
	cs\Event,
14
	cs\ExitException,
15
	cs\Language\Prefix,
16
	cs\Menu,
17
	cs\Request,
18
	cs\User;
19
20
Event::instance()
21
	->on(
22
		'System/Request/routing_replace/after',
23
		function ($data) {
24
			if (!Config::instance()->module('Blogs')->enabled()) {
25
				return;
26
			}
27
			$route = &$data['route'];
28
			if ($data['current_module'] != 'Blogs' || !$data['regular_path']) {
29
				return;
30
			}
31
			if (!isset($route[0])) {
32
				$route[0] = 'latest_posts';
33
			}
34
			$L = new Prefix('blogs_');
35
			switch ($route[0]) {
36
				case 'latest_posts':
37
				case 'section':
38
				case 'tag':
39
				case 'new_post':
40
				case 'edit_post':
41
				case 'drafts':
42
				case 'post':
43
				case 'atom.xml':
44
					break;
45
				case path($L->latest_posts):
0 ignored issues
show
Bug Best Practice introduced by
The property latest_posts does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
46
					$route[0] = 'latest_posts';
47
					break;
48
				case path($L->section):
0 ignored issues
show
Bug Best Practice introduced by
The property section does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
49
					$route[0] = 'section';
50
					break;
51
				case path($L->tag):
0 ignored issues
show
Bug Best Practice introduced by
The property tag does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
52
					$route[0] = 'tag';
53
					break;
54
				case path($L->new_post):
0 ignored issues
show
Bug Best Practice introduced by
The property new_post does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
					$route[0] = 'new_post';
56
					break;
57
				case path($L->drafts):
0 ignored issues
show
Bug Best Practice introduced by
The property drafts does not exist on cs\Language\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
58
					$route[0] = 'drafts';
59
					break;
60
				default:
61
					if (mb_strpos($route[0], ':') !== false) {
62
						$route[1] = $route[0];
63
						$route[0] = 'post';
64
					} else {
65
						throw new ExitException(404);
66
					}
67
			}
68
		}
69
	)
70
	->on(
71
		'api/Comments/add',
72
		function ($data) {
73
			$module_data = Config::instance()->module('Blogs');
74
			if (
75
				$module_data->enabled() &&
76
				$data['module'] == 'Blogs' &&
77
				$module_data->enable_comments &&
0 ignored issues
show
Bug Best Practice introduced by
The property enable_comments does not exist on cs\Config\Module_Properties. Since you implemented __get, consider adding a @property annotation.
Loading history...
78
				User::instance()->user() &&
79
				Posts::instance()->get($data['item'])
80
			) {
81
				$data['allow'] = true;
82
				return false;
83
			}
84
		}
85
	)
86
	->on(
87
		'api/Comments/edit',
88
		function ($data) {
89
			$User        = User::instance();
90
			$module_data = Config::instance()->module('Blogs');
91
			if (
92
				$module_data->enabled() &&
93
				$data['module'] == 'Blogs' &&
94
				$module_data->enable_comments &&
0 ignored issues
show
Bug Best Practice introduced by
The property enable_comments does not exist on cs\Config\Module_Properties. Since you implemented __get, consider adding a @property annotation.
Loading history...
95
				$User->user() &&
96
				($data['user'] == $User->id || $User->admin())
97
			) {
98
				$data['allow'] = true;
99
				return false;
100
			}
101
		}
102
	)
103
	->on(
104
		'api/Comments/delete',
105
		function ($data) {
106
			$User        = User::instance();
107
			$module_data = Config::instance()->module('Blogs');
108
			if (
109
				$module_data->enabled() &&
110
				$data['module'] == 'Blogs' &&
111
				$module_data->enable_comments &&
0 ignored issues
show
Bug Best Practice introduced by
The property enable_comments does not exist on cs\Config\Module_Properties. Since you implemented __get, consider adding a @property annotation.
Loading history...
112
				$User->user() &&
113
				($data['user'] == $User->id || $User->admin())
114
			) {
115
				$data['allow'] = true;
116
				return false;
117
			}
118
		}
119
	)
120
	->on(
121
		'admin/System/Menu',
122
		function () {
123
			$L       = new Prefix('blogs_');
124
			$Menu    = Menu::instance();
125
			$Request = Request::instance();
126
			foreach (['browse_sections', 'browse_posts', 'general'] as $section) {
127
				$Menu->add_item(
128
					'Blogs',
129
					$L->$section,
130
					[
131
						'href'    => "admin/Blogs/$section",
132
						'primary' => $Request->route_path(0) == $section
133
					]
134
				);
135
			}
136
		}
137
	)
138
	->on(
139
		'admin/System/modules/uninstall/before',
140
		function ($data) {
141
			if ($data['name'] != 'Blogs') {
142
				return;
143
			}
144
			time_limit_pause();
145
			$Posts    = Posts::instance();
146
			$Sections = Sections::instance();
147
			foreach (Sections::instance()->get_all() as $section) {
148
				$Sections->del($section['id']);
149
			}
150
			unset($section);
151
			$posts = DB::instance()->db(Config::instance()->module('Blogs')->db('posts'))->qfas(
0 ignored issues
show
The method qfas() does not exist on cs\False_class. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			$posts = DB::instance()->db(Config::instance()->module('Blogs')->db('posts'))->/** @scrutinizer ignore-call */ qfas(
Loading history...
152
				'SELECT `id`
153
				FROM `[prefix]blogs_posts`'
154
			) ?: [];
155
			foreach ($posts as $post) {
156
				$Posts->del($post);
157
			}
158
			Cache::instance()->del('Blogs');
159
			time_limit_pause(false);
160
		}
161
	)
162
	->on(
163
		'admin/System/modules/install/after',
164
		function ($data) {
165
			if ($data['name'] != 'Blogs') {
166
				return;
167
			}
168
			Config::instance()->module('Blogs')->set(
169
				[
170
					'posts_per_page'                => 10,
171
					'max_sections'                  => 3,
172
					'enable_comments'               => 1,
173
					'new_posts_only_from_admins'    => 1,
174
					'allow_iframes_without_content' => 1
175
				]
176
			);
177
		}
178
	);
179