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