4
4
* Plugin name: Disable Media Pages
5
5
*/
6
6
7
- add_filter ('wp_unique_post_slug ' , function ($ slug , $ post_ID , $ post_status , $ post_type , $ post_parent , $ original_slug ) {
8
- if ($ post_type === 'attachment ' ) {
9
- return str_replace ('- ' , '' , wp_generate_uuid4 ());
7
+ class DisableMediaPages {
8
+ public function __construct () {
9
+ add_filter ('wp_unique_post_slug ' , [$ this , 'unique_slug ' ], 10 , 6 );
10
+ add_filter ('template_redirect ' , [$ this , 'set_404 ' ]);
11
+ add_filter ('redirect_canonical ' , [$ this , 'set_404 ' ], 0 );
12
+ add_filter ('attachment_link ' , [$ this , 'change_attachment_link ' ], 10 , 2 );
13
+ add_filter ('admin_enqueue_scripts ' , [$ this , 'admin_enqueue_scripts ' ]);
14
+ // Add settings link on the plugin page
15
+ add_filter (
16
+ 'plugin_action_links_ ' . plugin_basename (__FILE__ ), [$ this , 'plugin_action_links ' ]
17
+ );
18
+
19
+ // Add plugin to WordPress admin menu
20
+ add_action ('admin_menu ' , [$ this , 'admin_menu ' ]);
21
+
10
22
}
11
- return $ slug ;
12
- }, 10 , 6 );
13
23
14
- add_filter ('template_redirect ' , 'set_404 ' );
15
- add_filter ('redirect_canonical ' , 'set_404 ' , 0 );
16
- add_filter ('attachment_link ' , 'change_attachment_link ' , 10 , 2 );
24
+ function set_404 ()
25
+ {
26
+ if (is_attachment ()) {
27
+ global $ wp_query ;
28
+ $ wp_query ->set_404 ();
29
+ status_header (404 );
30
+ }
31
+ }
17
32
18
- function set_404 ()
19
- {
20
- if (is_attachment ()) {
21
- global $ wp_query ;
22
- $ wp_query ->set_404 ();
23
- status_header (404 );
33
+ function change_attachment_link ($ url , $ id )
34
+ {
35
+ $ attachment_url = wp_get_attachment_url ($ id );
36
+ if ($ attachment_url ) {
37
+ return $ attachment_url ;
38
+ }
39
+ return $ url ;
24
40
}
25
- }
26
41
27
- function change_attachment_link ($ url , $ id )
28
- {
29
- $ attachment_url = wp_get_attachment_url ($ id );
30
- if ($ attachment_url ) {
31
- return $ attachment_url ;
42
+ function unique_slug ($ slug , $ post_ID , $ post_status , $ post_type , $ post_parent , $ original_slug ) {
43
+ if ($ post_type === 'attachment ' ) {
44
+ return str_replace ('- ' , '' , wp_generate_uuid4 ());
45
+ }
46
+ return $ slug ;
47
+ }
48
+
49
+ function admin_enqueue_scripts ()
50
+ {
51
+ $ plugin_data = get_plugin_data (__FILE__ );
52
+ $ version = $ plugin_data ['Version ' ];
53
+ $ url = plugin_dir_url (__FILE__ );
54
+ $ path = plugin_dir_path (__FILE__ );
55
+
56
+ wp_enqueue_script (
57
+ 'csf-script ' ,
58
+ "{$ url }dist/script.js " ,
59
+ [],
60
+ WP_DEBUG ? md5_file ($ path . 'dist/script.js ' ) : $ version
61
+ );
62
+
63
+ wp_enqueue_style (
64
+ 'csf-style ' ,
65
+ "{$ url }dist/style.css " ,
66
+ [],
67
+ WP_DEBUG ? md5_file ($ path . 'dist/style.css ' ) : $ version
68
+ );
32
69
}
33
- return $ url ;
34
- }
70
+
71
+ public function plugin_action_links ($ links ) {
72
+ $ settings_link =
73
+ '<a href="options-general.php?page=disable-media-pages"> ' .
74
+ __ ('Settings ' , 'disable-media-pages ' ) .
75
+ '</a> ' ;
76
+ array_unshift ($ links , $ settings_link );
77
+ return $ links ;
78
+ }
79
+
80
+ public function admin_menu () {
81
+ add_submenu_page (
82
+ null ,
83
+ __ (
84
+ 'Disable Media Pages ' ,
85
+ 'disable-media-pages '
86
+ ),
87
+ __ (
88
+ 'Disable Media Pages ' ,
89
+ 'disable-media-pages '
90
+ ),
91
+ 'manage_options ' ,
92
+ 'disable-media-pages ' ,
93
+ [$ this , 'settings_page ' ]
94
+ );
95
+ }
96
+
97
+ public function settings_page () {
98
+ echo '<div id="disable-media-pages"><disable-media-pages></disable-media-pages></div> ' ;
99
+ }
100
+ }
101
+
102
+ $ disable_media_pages = new DisableMediaPages ();
0 commit comments