PluginProbe
Media Sync / 1.1.6
Media Sync v1.1.6
1.5.3 1.5.2 1.5.1 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.1.4 0.1.5 0.1.6 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.2.0 1.2.2 1.2.3 All 44 releases
media-sync / media-sync.php

media-sync.php in Media Sync 1.1.6, at media-sync.php

258 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Media Sync
5 * Plugin URI: https://wordpress.org/plugins/media-sync/
6 * Description: Simple plugin to scan uploads directory and bring files to Media Library.
7 * Version: 1.1.6
8 * Author: Erol Živina
9 * Author URI: https://github.com/erolsk8
10 * License: GPLv2+
11 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
12 * Text Domain: media-sync
13 * Domain Path: /languages
14 */
15
16
17 // Exit if accessed directly
18 if ( ! defined( 'ABSPATH' ) ) exit;
19
20
21
22 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'media_sync_link_to_main_plugin_page' );
23
24 /**
25 * Add link below plugin name on 'Plugins' page
26 *
27 * @since 0.1.0
28 */
29 function media_sync_link_to_main_plugin_page( $links ) {
30 $title = __('Media Sync', 'media-sync');
31 $links[] = '<a href="'. esc_url( get_admin_url(null, 'upload.php?page=media-sync-page') ) .'">' . $title . '</a>';
32 return $links;
33 }
34
35
36
37 add_action( 'admin_menu', 'media_sync_add_menu_items' );
38
39 /**
40 * Add menu item for this plugin
41 *
42 * @since 0.1.0
43 */
44 function media_sync_add_menu_items() {
45
46 $title = __('Media Sync', 'media-sync');
47
48 // Add sub item to Media menu
49 if(MediaSync::media_sync_user_has_general_access()) {
50 add_media_page( $title, $title, 'read', 'media-sync-page', 'media_sync_main_page' );
51 }
52
53 // Show options menu only if user can manage options
54 if ( current_user_can( 'manage_options' ) ) {
55
56 // Add sub item to Settings menu
57 add_options_page( $title, $title, 'manage_options', 'media-sync-options', 'media_sync_options_page' );
58
59 // Prepare plugin settings
60 add_action( 'admin_init', 'media_sync_options_setup' );
61 }
62 }
63
64
65
66 include( plugin_dir_path(__FILE__) . 'includes/MediaSync.class.php');
67
68
69
70 add_action( 'admin_enqueue_scripts', 'media_sync_load_admin_scripts', 100 );
71
72 /**
73 * Load Admin CSS and JS files
74 *
75 * @since 0.1.0
76 * @return void
77 */
78 function media_sync_load_admin_scripts( $hook ) {
79
80 $js_dir = plugin_dir_url( __FILE__ ) . 'admin/js/';
81 $css_dir = plugin_dir_url( __FILE__ ) . 'admin/css/';
82
83 wp_register_script( 'media-sync-js-admin-script', $js_dir . 'script.js', array('jquery'), false, true );
84 wp_enqueue_script( 'media-sync-js-admin-script' );
85
86 wp_enqueue_script( 'media-sync-js-admin-ajax-script', $js_dir . 'ajax_script.js', array('jquery') );
87 wp_localize_script( 'media-sync-js-admin-ajax-script', 'ajax_data', array(
88 'ajax_url' => admin_url( 'admin-ajax.php' ),
89 'security' => wp_create_nonce( "media_sync_import_files" )
90 ));
91
92 wp_register_style( 'media-sync-css-admin-style', $css_dir . 'style.css');
93 wp_enqueue_style( 'media-sync-css-admin-style' );
94 }
95
96
97
98 add_action( 'wp_ajax_media_sync_import_files', 'media_sync_import_files' );
99
100 /**
101 * Ajax action to import selected file
102 *
103 * @since 0.1.0
104 * @return void
105 */
106 function media_sync_import_files() {
107 MediaSync::media_sync_import_files();
108 }
109
110
111
112 add_action( 'plugins_loaded', 'media_sync_load_plugin_textdomain' );
113
114 /**
115 * Loads plugin translated strings
116 *
117 * @since 0.1.4
118 * @return void
119 */
120 function media_sync_load_plugin_textdomain() {
121 load_plugin_textdomain( 'media-sync', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
122 }
123
124
125
126 add_action('restrict_manage_posts', 'media_sync_missing_media_library_filter');
127
128 /**
129 * Add filter in Media Library for finding missing files
130 *
131 * @since 1.0.0
132 * @return void
133 */
134 function media_sync_missing_media_library_filter()
135 {
136 require_once(ABSPATH . 'wp-admin/includes/screen.php');
137
138 // Skip if it's not Media Library
139 $scr = get_current_screen();
140 if (!is_admin() || !is_object($scr) || property_exists($scr, 'base') !== true || $scr->base !== 'upload') return;
141
142 // Get "missing files" filter from URL
143 $missing = filter_input(INPUT_GET, 'media_sync_missing_files', FILTER_SANITIZE_STRING);
144 ?>
145 <label for="filter-by-media-sync-missing-files"
146 class="screen-reader-text"><?= __('Filter by missing file', 'media-sync') ?></label>
147 <select name="media_sync_missing_files" id="filter-by-media-sync-missing-files">
148 <option
149 value=""<?= !$missing ? ' selected="selected"' : '' ?>><?= __('Filter by missing file', 'media-sync') ?></option>
150 <option
151 value="no"<?= $missing === 'no' ? ' selected="selected"' : '' ?>><?= __('With file', 'media-sync') ?></option>
152 <option
153 value="yes"<?= $missing === 'yes' ? ' selected="selected"' : '' ?>><?= __('Without file', 'media-sync') ?></option>
154 </select>
155 <?php
156 }
157
158
159
160 add_action('pre_get_posts','media_sync_missing_media_library_apply_filter');
161
162 /**
163 * Apply filter for Media Library items with or without missing files
164 *
165 * @since 1.0.0
166 * @param $query
167 * @return void
168 */
169 function media_sync_missing_media_library_apply_filter($query)
170 {
171 require_once(ABSPATH . 'wp-admin/includes/screen.php');
172
173 // Skip if it's not Media Library
174 $scr = get_current_screen();
175 if (!is_admin() || !is_object($scr) || property_exists($scr, 'base') !== true || $scr->base !== 'upload') return;
176
177 // Get "missing files" filter from URL
178 $missing = filter_input(INPUT_GET, 'media_sync_missing_files', FILTER_SANITIZE_STRING);
179
180 // Skip if "missing files" filter is not applied
181 if (!$missing) {
182 return;
183 }
184
185 // Prevent infinite loop
186 remove_action('pre_get_posts', __FUNCTION__);
187
188 // Get all media library items (to be able to check which ones do not have actual files)
189 $media_library_items = get_posts(
190 array_merge(
191 $query->query,
192 array(
193 'fields' => 'ids',
194 'posts_per_page' => -1
195 )
196 )
197 );
198
199 // Get missing files
200 $missing_files = array();
201 foreach ($media_library_items as $id) {
202 // Get absolute path for this file
203 $absolute_path = get_home_path() . str_replace(get_site_url(), "", wp_get_attachment_url($id));
204
205 // Strip double slashes (//) in absolute path
206 $absolute_path = preg_replace('/([^:])(\/{2,})/', '$1/', $absolute_path);
207
208 // If file doesn't actually exists (if it's missing)
209 if (!file_exists($absolute_path)) {
210 $missing_files[] = $id;
211 }
212 }
213
214 // Apply appropriate filter
215 if ($missing === 'yes') {
216 $query->set('post__in', !empty($missing_files) ? $missing_files : [0]);
217 }
218
219 if ($missing === 'no') {
220 $query->set('post__not_in', $missing_files);
221 }
222 }
223
224
225
226 /**
227 * Main function for "Media Sync" page
228 *
229 * @since 0.1.0
230 * @return void
231 */
232 function media_sync_main_page() {
233 MediaSync::media_sync_main_page();
234 }
235
236
237
238 /**
239 * Render settings page
240 *
241 * @since 1.1.0
242 * @return void
243 */
244 function media_sync_options_page() {
245 MediaSync::media_sync_options_page();
246 }
247
248
249
250 /**
251 * Prepare plugin settings
252 *
253 * @since 1.1.0
254 * @return void
255 */
256 function media_sync_options_setup() {
257 MediaSync::media_sync_options_setup();
258 }