| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Plugin Name: BSK PDF Manager |
| 5 |
* Plugin URI: http://www.bannersky.com/bsk-pdf-manager/ |
| 6 |
* Description: Help you manage your PDF documents. PDF documents can be filter by category. Support short code to show special PDF documents or all PDF documents under category. Widget supported. |
| 7 |
* Version: 3.8.1 |
| 8 |
* Author: BannerSky.com |
| 9 |
* Author URI: http://www.bannersky.com/ |
| 10 |
* License: GPLv2 or later |
| 11 |
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 12 |
*/ |
| 13 |
if ( !defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
// Plugin Folder Path. |
| 18 |
if ( ! defined( 'BSK_PDFM_PLUGIN_DIR' ) ) { |
| 19 |
define( 'BSK_PDFM_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 20 |
} |
| 21 |
// Plugin Folder URL. |
| 22 |
if ( ! defined( 'BSK_PDFM_PLUGIN_URL' ) ) { |
| 23 |
define( 'BSK_PDFM_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
| 24 |
} |
| 25 |
|
| 26 |
class BSKPDFManager { |
| 27 |
|
| 28 |
private static $instance; |
| 29 |
public static $_cats_tbl_name = 'bsk_pdf_manager_cats'; |
| 30 |
public static $_pdfs_tbl_name = 'bsk_pdf_manager_pdfs'; |
| 31 |
public static $_rels_tbl_name = 'bsk_pdf_manager_relationships'; |
| 32 |
public static $_user_available_tbl_name = 'bsk_pdf_manager_user_available'; |
| 33 |
public static $_notifications_tbl_name = 'bsk_pdf_manager_notifications'; |
| 34 |
|
| 35 |
public static $_PLUGIN_VERSION_ = '3.8.1'; |
| 36 |
private static $_plugin_db_version = '3.1'; |
| 37 |
private static $_plugin_saved_db_version_option = '_bsk_pdf_manager_db_ver_'; |
| 38 |
private static $_plugin_db_rels_done_option = '_bsk_pdf_manager_rels_done_'; |
| 39 |
private static $_plugin_db_upgrading = '_bsk_pdf_manager_db_upgrading_'; |
| 40 |
|
| 41 |
private static $_plugin_db_doc_slug_done_option = '_bsk_pdf_manager_update_doc_slug_done_'; |
| 42 |
private static $_plugin_db_doc_slug_doing_option = '_bsk_pdf_manager_update_doc_slug_doing_'; |
| 43 |
|
| 44 |
private static $_plugin_db_file_size_done_option = '_bsk_pdf_manager_update_file_size_done_'; |
| 45 |
private static $_plugin_db_file_size_doing_option = '_bsk_pdf_manager_update_file_size_doing_'; |
| 46 |
|
| 47 |
public static $_upload_path = ''; |
| 48 |
public static $_upload_folder = 'bsk-pdf-manager/'; |
| 49 |
public static $_upload_path_4_ftp = ''; |
| 50 |
public static $_upload_folder_4_ftp = 'ftp/'; |
| 51 |
public static $_upload_url = ''; |
| 52 |
|
| 53 |
public static $_upload_root_path = ''; |
| 54 |
public static $_upload_root_appendix = ''; |
| 55 |
|
| 56 |
private static $_plugin_admin_notice_message = array(); |
| 57 |
|
| 58 |
public static $_plugin_settings_option = '_bsk_pdf_manager_pro_settings_'; |
| 59 |
public static $_plugin_temp_option_prefix = '_bsk_pdf_manager_pro_temp_'; |
| 60 |
|
| 61 |
public static $_default_pdf_icon_url = ''; |
| 62 |
public static $_ajax_loader_img_url = ''; |
| 63 |
public static $_delete_cat_icon_url = ''; |
| 64 |
|
| 65 |
public static $url_to_upgrade = 'https://bannersky.com/bsk-pdf-manager/'; |
| 66 |
|
| 67 |
public static $_category_max_depth = 3; |
| 68 |
|
| 69 |
//objects |
| 70 |
public $_bsk_pdfm_pro_OBJ_dashboard = NULL; |
| 71 |
public $_bsk_pdfm_pro_OBJ_shortcodes = NULL; |
| 72 |
public $_bsk_pdfm_pro_OBJ_permalink_accessCtrl = NULL; |
| 73 |
|
| 74 |
public static function instance() { |
| 75 |
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof BSKPDFManager ) ) { |
| 76 |
self::$instance = new BSKPDFManager; |
| 77 |
|
| 78 |
require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 79 |
|
| 80 |
//default upload folder, path, url |
| 81 |
$uploads = wp_upload_dir(); |
| 82 |
self::$_upload_path = $uploads['basedir'].'/'.self::$_upload_folder; |
| 83 |
self::$_upload_url = $uploads['baseurl'].'/'.self::$_upload_folder; |
| 84 |
self::$_upload_path_4_ftp = self::$_upload_path.self::$_upload_folder_4_ftp; |
| 85 |
|
| 86 |
$site_url_no_prefix = str_replace( 'http://www.', '', site_url() ); |
| 87 |
$site_url_no_prefix = str_replace( 'https://www.', '', $site_url_no_prefix ); |
| 88 |
$site_url_no_prefix = str_replace( 'http://', '', $site_url_no_prefix ); |
| 89 |
$site_url_no_prefix = str_replace( 'https://', '', $site_url_no_prefix ); |
| 90 |
$uploads_base_url_no_prefix = str_replace( 'http://www.', '', $uploads['baseurl'] ); |
| 91 |
$uploads_base_url_no_prefix = str_replace( 'https://www.', '', $uploads_base_url_no_prefix ); |
| 92 |
$uploads_base_url_no_prefix = str_replace( 'http://', '', $uploads_base_url_no_prefix ); |
| 93 |
$uploads_base_url_no_prefix = str_replace( 'https://', '', $uploads_base_url_no_prefix ); |
| 94 |
self::$_upload_root_appendix = str_replace($site_url_no_prefix, '', $uploads_base_url_no_prefix); //general, /wp-cotnent/uploads/ |
| 95 |
self::$_upload_root_path = str_replace(self::$_upload_root_appendix, '', $uploads['basedir']).'/'; //general, same as ABSPATH |
| 96 |
|
| 97 |
self::$_ajax_loader_img_url = BSK_PDFM_PLUGIN_URL.'images/ajax-loader.gif'; |
| 98 |
self::$_default_pdf_icon_url = BSK_PDFM_PLUGIN_URL.'images/default_PDF_icon.png'; |
| 99 |
self::$_delete_cat_icon_url = BSK_PDFM_PLUGIN_URL.'images/delete-2.png'; |
| 100 |
|
| 101 |
|
| 102 |
//read plugin setting to set custom upload folder |
| 103 |
$plugin_settings = get_option( self::$_plugin_settings_option, false ); |
| 104 |
if( !$plugin_settings || !is_array($plugin_settings) || count($plugin_settings) < 1 ){ |
| 105 |
$plugin_settings = array(); |
| 106 |
} |
| 107 |
|
| 108 |
add_action( 'admin_notices', array(self::$instance, 'bsk_pdf_manager_admin_notice') ); |
| 109 |
add_action( 'admin_enqueue_scripts', array(self::$instance, 'bsk_pdf_manager_enqueue_scripts_n_css') ); |
| 110 |
add_action( 'wp_enqueue_scripts', array(self::$instance, 'bsk_pdf_manager_enqueue_scripts_n_css') ); |
| 111 |
|
| 112 |
//include others class |
| 113 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/backend.php' ); |
| 114 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/display.php' ); |
| 115 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/filter-extension.php' ); |
| 116 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/filter-tags.php' ); |
| 117 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/data-source.php' ); |
| 118 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/common/count-desc-bar.php' ); |
| 119 |
|
| 120 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/dashboard/dashboard.php' ); |
| 121 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/shortcodes/shortcodes.php' ); |
| 122 |
|
| 123 |
|
| 124 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/widgets/widget.php' ); |
| 125 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/widgets/widget-category.php' ); |
| 126 |
|
| 127 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/permalink-access/permalink-access.php' ); |
| 128 |
|
| 129 |
self::$instance->_bsk_pdfm_pro_OBJ_dashboard = new BSKPDFM_Dashboard(); |
| 130 |
self::$instance->_bsk_pdfm_pro_OBJ_shortcodes = new BSKPDFM_Shortcodes(); |
| 131 |
self::$instance->_bsk_pdfm_pro_OBJ_permalink_accessCtrl = new BSKPDFM_Permalink_AccessCtrl(); |
| 132 |
|
| 133 |
//forms integration, pub upload form, just require |
| 134 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/forms/forms-integration.php' ); |
| 135 |
require_once( BSK_PDFM_PLUGIN_DIR.'classes/shortcodes/pub/pub-upload.php' ); |
| 136 |
|
| 137 |
//hooks |
| 138 |
register_activation_hook(__FILE__, array(self::$instance, 'bsk_pdf_manager_activate') ); |
| 139 |
register_deactivation_hook( __FILE__, array(self::$instance, 'bsk_pdf_manager_deactivate') ); |
| 140 |
register_uninstall_hook( __FILE__, 'BSKPDFManager::bsk_pdf_manager_uninstall' ); |
| 141 |
|
| 142 |
add_action( 'widgets_init', array(self::$instance, 'bsk_pdf_manager_pro_register_widgets')); |
| 143 |
|
| 144 |
add_action( 'init', array(self::$instance, 'bsk_pdf_manager_post_action') ); |
| 145 |
|
| 146 |
self::$instance->bsk_pdf_create_upload_folder_and_set_secure(); |
| 147 |
|
| 148 |
add_action( 'plugins_loaded', array(self::$instance, 'bsk_pdf_manager_update_database'), 10 ); |
| 149 |
add_action( 'plugins_loaded', array(self::$instance, 'bsk_pdf_manager_update_doc_slug_fun'), 16 ); |
| 150 |
add_action( 'plugins_loaded', array(self::$instance, 'bsk_pdf_manager_update_file_size_fun'), 18 ); |
| 151 |
add_action( 'init', array(self::$instance, 'bsk_pdf_manager_load_language'), 10 ); |
| 152 |
} |
| 153 |
|
| 154 |
return self::$instance; |
| 155 |
} |
| 156 |
|
| 157 |
private function __construct() { |
| 158 |
// |
| 159 |
} |
| 160 |
|
| 161 |
public function __clone() { |
| 162 |
// Cloning instances of the class is forbidden. |
| 163 |
_doing_it_wrong( __FUNCTION__, 'Cheatin’', '1.0' ); |
| 164 |
} |
| 165 |
|
| 166 |
public function __wakeup() { |
| 167 |
// Unserializing instances of the class is forbidden. |
| 168 |
_doing_it_wrong( __FUNCTION__, 'Cheatin’', '1.0' ); |
| 169 |
} |
| 170 |
|
| 171 |
function bsk_pdf_manager_activate( $network_wide ){ |
| 172 |
//create or update table |
| 173 |
self::$instance->bsk_pdf_manager_pro_create_table(); |
| 174 |
} |
| 175 |
|
| 176 |
function bsk_pdf_manager_deactivate(){ |
| 177 |
wp_clear_scheduled_hook( 'bsk_pdfm_free_schedule_check_promote_weekly' ); |
| 178 |
wp_clear_scheduled_hook( 'bsk_pdfm_free_schedule_check_promote_daily' ); |
| 179 |
} |
| 180 |
|
| 181 |
function bsk_pdf_manager_update_doc_slug_fun(){ |
| 182 |
//check db version first |
| 183 |
$db_version = get_option( self::$_plugin_saved_db_version_option ); |
| 184 |
if ( version_compare( $db_version, '2.6', '<' ) ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
$is_done = get_option( self::$_plugin_db_doc_slug_done_option, false ); |
| 188 |
if( $is_done ){ |
| 189 |
return; |
| 190 |
} |
| 191 |
$is_doing = get_option( self::$_plugin_db_doc_slug_doing_option, false ); |
| 192 |
if( $is_doing ){ |
| 193 |
return; |
| 194 |
} |
| 195 |
update_option( self::$_plugin_db_doc_slug_doing_option, true ); |
| 196 |
|
| 197 |
global $wpdb; |
| 198 |
$sql = 'SELECT `title`, `id` '. |
| 199 |
'FROM `'.esc_sql($wpdb->prefix . self::$_pdfs_tbl_name).'` '. |
| 200 |
'WHERE `slug` is NULL OR LENGTH(`slug`) < 1 '. |
| 201 |
'ORDER BY `id` ASC '. |
| 202 |
'LIMIT 0, 100'; |
| 203 |
$pdfs_to_process = $wpdb->get_results( $sql ); |
| 204 |
if( !$pdfs_to_process || !is_array( $pdfs_to_process ) || count( $pdfs_to_process ) < 1 ){ |
| 205 |
update_option( self::$_plugin_db_doc_slug_doing_option, false ); |
| 206 |
update_option( self::$_plugin_db_doc_slug_done_option, true ); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$all_supported_extensions = BSKPDFM_Common_Backend::get_supported_extension_with_mime_type(); |
| 211 |
foreach( $pdfs_to_process as $pdf_record ){ |
| 212 |
$ext_array = explode( '.', $pdf_record->title ); |
| 213 |
if( count($ext_array) >= 2 ){ |
| 214 |
//check if extention includes |
| 215 |
if( array_key_exists( $ext_array[count($ext_array) - 1], $all_supported_extensions ) ){ |
| 216 |
unset( $ext_array[count($ext_array) - 1] ); |
| 217 |
} |
| 218 |
} |
| 219 |
$title_without_ext = implode( '.', $ext_array ); |
| 220 |
$slug = BSKPDFM_Permalink_AccessCtrl::get_document_slug( $title_without_ext, $pdf_record->id ); |
| 221 |
$data_to_update = array( 'slug' => $slug ); |
| 222 |
$wpdb->update( $wpdb->prefix . self::$_pdfs_tbl_name, $data_to_update, array( 'id' => $pdf_record->id ) ); |
| 223 |
} |
| 224 |
|
| 225 |
update_option( self::$_plugin_db_doc_slug_doing_option, false ); |
| 226 |
update_option( self::$_plugin_db_doc_slug_done_option, false ); |
| 227 |
|
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
public static function bsk_pdf_manager_pro_remove_tables_n_options(){ |
| 232 |
global $wpdb; |
| 233 |
|
| 234 |
delete_option( '_bsk_pdf_manager_open_target' ); |
| 235 |
delete_option( '_bsk_pdf_manager_category_list_has_title' ); |
| 236 |
delete_option( '_bsk_pdf_manager_pdf_order_by_' ); |
| 237 |
delete_option( '_bsk_pdf_manager_pdf_order_' ); |
| 238 |
delete_option( '_bsk_pdf_manager_db_ver_'); |
| 239 |
delete_option( '_bsk_pdf_manager_rels_done_'); |
| 240 |
delete_option( '_bsk_pdf_manager_free_to_pro_done_'); |
| 241 |
|
| 242 |
$table_cats = $wpdb->prefix."bsk_pdf_manager_cats"; |
| 243 |
$table_pdfs = $wpdb->prefix."bsk_pdf_manager_pdfs"; |
| 244 |
$table_rels = $wpdb->prefix."bsk_pdf_manager_relationships"; |
| 245 |
$table_user_available = $wpdb->prefix."bsk_pdf_manager_user_available"; |
| 246 |
$table_notifications = $wpdb->prefix."notifications_tbl_name"; |
| 247 |
|
| 248 |
$wpdb->query("DROP TABLE IF EXISTS $table_cats"); |
| 249 |
$wpdb->query("DROP TABLE IF EXISTS $table_pdfs"); |
| 250 |
$wpdb->query("DROP TABLE IF EXISTS $table_rels"); |
| 251 |
$wpdb->query("DROP TABLE IF EXISTS $table_user_available"); |
| 252 |
$wpdb->query("DROP TABLE IF EXISTS $table_notifications"); |
| 253 |
|
| 254 |
$sql = 'DELETE FROM `'.$wpdb->options.'` WHERE `option_name` LIKE "_bsk_pdf_manager%"'; |
| 255 |
$wpdb->query( $sql ); |
| 256 |
$sql = 'DELETE FROM `'.$wpdb->options.'` WHERE `option_name` LIKE "_bsk_pdfm%"'; |
| 257 |
$wpdb->query( $sql ); |
| 258 |
} |
| 259 |
|
| 260 |
public static function bsk_pdf_manager_uninstall(){ |
| 261 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 262 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 263 |
} |
| 264 |
|
| 265 |
$has_active_pro_verison = false; |
| 266 |
$plugins = get_plugins(); |
| 267 |
foreach( $plugins as $plugin_key => $data ){ |
| 268 |
if( 'bsk-pdf-manager-pro/bsk-pdf-manager-pro.php' == $plugin_key && |
| 269 |
is_plugin_active( $plugin_key ) ){ |
| 270 |
$has_active_pro_verison = true; |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
if( $has_active_pro_verison == true ){ |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
//create or update table |
| 279 |
self::bsk_pdf_manager_pro_remove_tables_n_options(); |
| 280 |
} |
| 281 |
|
| 282 |
function bsk_pdf_manager_pro_register_widgets(){ |
| 283 |
register_widget( "BSKPDFManagerWidget" ); |
| 284 |
register_widget( "BSKPDFManagerWidget_Category" ); |
| 285 |
} |
| 286 |
|
| 287 |
function bsk_pdf_manager_enqueue_scripts_n_css(){ |
| 288 |
global $wp_version; |
| 289 |
|
| 290 |
wp_enqueue_script('jquery'); |
| 291 |
if( is_admin() ){ |
| 292 |
if( function_exists( 'wp_enqueue_media' ) && version_compare( $wp_version, '3.5', '>=' ) ) { |
| 293 |
wp_enqueue_media(); |
| 294 |
} |
| 295 |
wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 296 |
wp_enqueue_style( 'jquery-ui', |
| 297 |
BSK_PDFM_PLUGIN_URL.'css/jquery-ui.css', |
| 298 |
array(), |
| 299 |
filemtime(BSK_PDFM_PLUGIN_DIR.'css/jquery-ui.css') ); |
| 300 |
wp_enqueue_script( 'jstree', |
| 301 |
BSK_PDFM_PLUGIN_URL.'classes/dashboard/settings/jstree/dist/jstree.min.js', |
| 302 |
array('jquery'), |
| 303 |
filemtime(BSK_PDFM_PLUGIN_DIR.'classes/dashboard/settings/jstree/dist/jstree.min.js') ); |
| 304 |
wp_enqueue_script( 'dateformat', |
| 305 |
BSK_PDFM_PLUGIN_URL.'js/date.format.js', |
| 306 |
array('jquery'), |
| 307 |
filemtime(BSK_PDFM_PLUGIN_DIR.'js/date.format.js') ); |
| 308 |
wp_enqueue_script( 'bsk-pdfm-pro-admin', |
| 309 |
BSK_PDFM_PLUGIN_URL.'js/bsk_pdfm_pro_admin.js', |
| 310 |
array('jquery', 'jquery-ui-datepicker', 'jstree', 'dateformat'), |
| 311 |
filemtime(BSK_PDFM_PLUGIN_DIR.'js/bsk_pdfm_pro_admin.js') ); |
| 312 |
$supported_extension_and_mime_type = BSKPDFM_Common_Backend::get_supported_extension_with_mime_type(); |
| 313 |
wp_localize_script( |
| 314 |
'bsk-pdfm-pro-admin', |
| 315 |
'bsk_pdfm_admin', |
| 316 |
array( |
| 317 |
'extension_and_mime' => $supported_extension_and_mime_type, |
| 318 |
'ajax_loader_url' => self::$_ajax_loader_img_url, |
| 319 |
) |
| 320 |
); |
| 321 |
|
| 322 |
wp_enqueue_style( 'jstree', |
| 323 |
BSK_PDFM_PLUGIN_URL.'classes/dashboard/settings/jstree/dist/themes/default/style.min.css', |
| 324 |
array(), |
| 325 |
filemtime(BSK_PDFM_PLUGIN_DIR.'classes/dashboard/settings/jstree/dist/themes/default/style.min.css') ); |
| 326 |
wp_enqueue_style( 'bsk-pdf-manager-pro-admin', |
| 327 |
BSK_PDFM_PLUGIN_URL.'css/bsk-pdf-manager-pro-admin.css', |
| 328 |
array('jstree'), |
| 329 |
filemtime(BSK_PDFM_PLUGIN_DIR.'css/bsk-pdf-manager-pro-admin.css') ); |
| 330 |
|
| 331 |
if( isset( $_GET['page'] ) && sanitize_text_field( $_GET['page'] ) == BSKPDFM_Dashboard::$_bsk_pdfm_pro_pages['edit'] ){ |
| 332 |
wp_enqueue_style( |
| 333 |
'bsk-pdf-manager-pro-editor', |
| 334 |
BSK_PDFM_PLUGIN_URL.'css/bsk-pdf-manager-pro-editor.css', |
| 335 |
array( 'bsk-pdf-manager-pro-admin' ), |
| 336 |
filemtime( BSK_PDFM_PLUGIN_DIR.'css/bsk-pdf-manager-pro-editor.css' ) |
| 337 |
); |
| 338 |
} |
| 339 |
}else{ |
| 340 |
$default_styles_version = '2.0'; |
| 341 |
$plugin_settings = get_option( self::$_plugin_settings_option, '' ); |
| 342 |
if( $plugin_settings && is_array($plugin_settings) && count($plugin_settings) > 0 ){ |
| 343 |
if( isset($plugin_settings['default_styles_version']) ){ |
| 344 |
$default_styles_version = $plugin_settings['default_styles_version']; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
if( $default_styles_version != '2.0' ){ |
| 349 |
wp_enqueue_style( 'bsk-pdf-manager-pro-css', |
| 350 |
BSK_PDFM_PLUGIN_URL.'css/bsk-pdf-manager-pro-v_'.str_replace('.', '_', $default_styles_version).'.css', |
| 351 |
array(), |
| 352 |
filemtime(BSK_PDFM_PLUGIN_DIR.'css/bsk-pdf-manager-pro-v_'.str_replace('.', '_', $default_styles_version).'.css') ); |
| 353 |
}else{ |
| 354 |
wp_enqueue_style( 'bsk-pdf-manager-pro-css', |
| 355 |
BSK_PDFM_PLUGIN_URL.'css/bsk-pdf-manager-pro.css', |
| 356 |
array(), |
| 357 |
filemtime(BSK_PDFM_PLUGIN_DIR.'css/bsk-pdf-manager-pro.css') ); |
| 358 |
} |
| 359 |
|
| 360 |
wp_enqueue_script( 'bsk-pdf-manager-pro', |
| 361 |
BSK_PDFM_PLUGIN_URL.'js/bsk_pdf_manager_pro.js', |
| 362 |
array('jquery'), |
| 363 |
filemtime(BSK_PDFM_PLUGIN_DIR.'js/bsk_pdf_manager_pro.js') ); |
| 364 |
|
| 365 |
wp_localize_script( 'bsk-pdf-manager-pro', 'bsk_pdf_pro', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
function bsk_pdf_manager_admin_notice(){ |
| 370 |
$warning_message = array(); |
| 371 |
$error_message = array(); |
| 372 |
|
| 373 |
//admin message |
| 374 |
if (count(self::$_plugin_admin_notice_message) > 0){ |
| 375 |
foreach(self::$_plugin_admin_notice_message as $msg){ |
| 376 |
if($msg['type'] == 'ERROR'){ |
| 377 |
$error_message[] = $msg['message']; |
| 378 |
} |
| 379 |
if($msg['type'] == 'WARNING'){ |
| 380 |
$warning_message[] = $msg['message']; |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
//show error message |
| 386 |
if(count($warning_message) > 0){ |
| 387 |
echo '<div class="update-nag">'; |
| 388 |
foreach($warning_message as $msg_to_show){ |
| 389 |
echo '<p>'.esc_html($msg_to_show).'</p>'; |
| 390 |
} |
| 391 |
echo '</div>'; |
| 392 |
} |
| 393 |
|
| 394 |
//show error message |
| 395 |
if(count($error_message) > 0){ |
| 396 |
echo '<div class="error">'; |
| 397 |
foreach($error_message as $msg_to_show){ |
| 398 |
echo '<p>'.esc_html($msg_to_show).'</p>'; |
| 399 |
} |
| 400 |
echo '</div>'; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
function bsk_pdf_manager_pro_create_table(){ |
| 405 |
global $wpdb; |
| 406 |
|
| 407 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 408 |
$charset_collate = $wpdb->get_charset_collate(); |
| 409 |
|
| 410 |
$table_name = $wpdb->prefix . self::$_cats_tbl_name; |
| 411 |
$sql = "CREATE TABLE $table_name ( |
| 412 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 413 |
`type` varchar(8) DEFAULT 'CAT', |
| 414 |
`parent` int(11) DEFAULT 0, |
| 415 |
`title` varchar(512) NOT NULL, |
| 416 |
`description` text DEFAULT NULL, |
| 417 |
`password` varchar(32) DEFAULT NULL, |
| 418 |
`empty_message` varchar(512) DEFAULT NULL, |
| 419 |
`last_date` datetime DEFAULT NULL, |
| 420 |
UNIQUE KEY id (id) |
| 421 |
) $charset_collate;"; |
| 422 |
dbDelta( $sql ); |
| 423 |
|
| 424 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 425 |
$sql = "CREATE TABLE $table_name ( |
| 426 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 427 |
`cat_id` varchar(32) NOT NULL, |
| 428 |
`order_num` int(11) DEFAULT NULL, |
| 429 |
`thumbnail_id` int(11) DEFAULT NULL, |
| 430 |
`title` varchar(512) DEFAULT NULL, |
| 431 |
`slug` varchar(256) DEFAULT NULL, |
| 432 |
`file_name` varchar(512) NOT NULL, |
| 433 |
`description` text DEFAULT NULL, |
| 434 |
`by_media_uploader` int(11) DEFAULT 0, |
| 435 |
`media_ext` varchar(32) DEFAULT NULL, |
| 436 |
`last_date` datetime DEFAULT NULL, |
| 437 |
`weekday` varchar(8) DEFAULT NULL, |
| 438 |
`download_count` int(11) DEFAULT 0, |
| 439 |
`publish_date` datetime DEFAULT NULL, |
| 440 |
`expiry_date` datetime DEFAULT NULL, |
| 441 |
`trash` tinyint(1) NOT NULL DEFAULT 0, |
| 442 |
`pending` tinyint(1) NOT NULL DEFAULT 0, |
| 443 |
`draft` tinyint(1) NOT NULL DEFAULT 0, |
| 444 |
`author_id` int(11) NOT NULL DEFAULT 0, |
| 445 |
`size` int(11) NOT NULL DEFAULT 0, |
| 446 |
`redirect_permalink` tinyint(1) NOT NULL DEFAULT 0, |
| 447 |
`restricted` tinyint(1) NOT NULL DEFAULT 0, |
| 448 |
UNIQUE KEY id (id) |
| 449 |
) $charset_collate;"; |
| 450 |
dbDelta($sql); |
| 451 |
|
| 452 |
$table_name = $wpdb->prefix . self::$_rels_tbl_name; |
| 453 |
$sql = "CREATE TABLE $table_name ( |
| 454 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 455 |
`type` varchar(8) DEFAULT 'CAT', |
| 456 |
`pdf_id` int(11) NOT NULL, |
| 457 |
`cat_id` int(11) NOT NULL, |
| 458 |
UNIQUE KEY id (id) |
| 459 |
) $charset_collate;"; |
| 460 |
dbDelta($sql); |
| 461 |
|
| 462 |
$table_name = $wpdb->prefix . self::$_user_available_tbl_name; |
| 463 |
$sql = "CREATE TABLE $table_name ( |
| 464 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 465 |
`user_id` int(11) NOT NULL, |
| 466 |
`type` varchar(8) NOT NULL, |
| 467 |
`value` int(11) NOT NULL, |
| 468 |
UNIQUE KEY id (id) |
| 469 |
) $charset_collate;"; |
| 470 |
dbDelta($sql); |
| 471 |
|
| 472 |
$table_name = $wpdb->prefix . self::$_notifications_tbl_name; |
| 473 |
$sql = "CREATE TABLE $table_name ( |
| 474 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 475 |
`status` tinyint(1) NOT NULL DEFAULT 1, |
| 476 |
`name` varchar(256) NOT NULL, |
| 477 |
`trigger_by` varchar(32) NOT NULL, |
| 478 |
`auto_meta` text DEFAULT NULL, |
| 479 |
`send_to_type` varchar(32) DEFAULT NULL, |
| 480 |
`send_to_type_meta` text DEFAULT NULL, |
| 481 |
`from_name` varchar(256) DEFAULT NULL, |
| 482 |
`from_email` varchar(256) DEFAULT NULL, |
| 483 |
`subject` varchar(256) NOT NULL, |
| 484 |
`body` text DEFAULT NULL, |
| 485 |
UNIQUE KEY id (id) |
| 486 |
) $charset_collate;"; |
| 487 |
dbDelta($sql); |
| 488 |
|
| 489 |
update_option( self::$_plugin_saved_db_version_option, self::$_plugin_db_version ); |
| 490 |
//for new install, doesn't need to build relationships |
| 491 |
update_option( self::$_plugin_db_rels_done_option, 'YES' ); |
| 492 |
} |
| 493 |
|
| 494 |
function bsk_pdf_manager_update_database(){ |
| 495 |
|
| 496 |
$db_version = get_option( self::$_plugin_saved_db_version_option ); |
| 497 |
if ( version_compare( $db_version, self::$_plugin_db_version, '>=' ) ) { |
| 498 |
return; |
| 499 |
} |
| 500 |
|
| 501 |
$is_upgrading = get_option( self::$_plugin_db_upgrading, false ); |
| 502 |
if( $is_upgrading ){ |
| 503 |
//already have instance doing upgrading so exit this one |
| 504 |
return; |
| 505 |
} |
| 506 |
update_option( self::$_plugin_db_upgrading, true ); |
| 507 |
|
| 508 |
global $wpdb; |
| 509 |
|
| 510 |
|
| 511 |
//upgrade to 2.2 |
| 512 |
if ( version_compare( $db_version, '2.2', '<' ) ) { |
| 513 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 514 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `download_count` INT DEFAULT 0 AFTER `weekday`;'; |
| 515 |
$wpdb->query( $sql ); |
| 516 |
} |
| 517 |
|
| 518 |
//upgrade db version to 2.3 |
| 519 |
if ( version_compare( $db_version, '2.3', '<' ) ) { |
| 520 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 521 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `media_ext` VARCHAR(32) DEFAULT NULL AFTER `by_media_uploader`;'; |
| 522 |
$wpdb->query( $sql ); |
| 523 |
} |
| 524 |
|
| 525 |
//upgrade db version to 2.4 |
| 526 |
if ( version_compare( $db_version, '2.4', '<' ) ) { |
| 527 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 528 |
$sql = 'ALTER TABLE `'.$table_name.'` `description` `description` TEXT DEFAULT NULL'; |
| 529 |
$wpdb->query( $sql ); |
| 530 |
|
| 531 |
$table_name = $wpdb->prefix . self::$_cats_tbl_name; |
| 532 |
$sql = 'ALTER TABLE `'.$table_name.'` `description` `description` TEXT DEFAULT NULL'; |
| 533 |
$wpdb->query( $sql ); |
| 534 |
} |
| 535 |
|
| 536 |
//upgrade db version to 2.5 |
| 537 |
if ( version_compare( $db_version, '2.5', '<' ) ) { |
| 538 |
$table_name = $wpdb->prefix . self::$_cats_tbl_name; |
| 539 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `type` VARCHAR(8) NULL DEFAULT \'CAT\' AFTER `id`;'; |
| 540 |
$wpdb->query( $sql ); |
| 541 |
|
| 542 |
$table_name = $wpdb->prefix . self::$_rels_tbl_name; |
| 543 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `type` VARCHAR(8) NULL DEFAULT \'CAT\' AFTER `id`;'; |
| 544 |
$wpdb->query( $sql ); |
| 545 |
} |
| 546 |
|
| 547 |
//upgrade db version to 2.6 |
| 548 |
if ( version_compare( $db_version, '2.6', '<' ) ) { |
| 549 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 550 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `slug` VARCHAR(256) NULL AFTER `title`;'; |
| 551 |
$wpdb->query( $sql ); |
| 552 |
|
| 553 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `trash` TINYINT(1) NOT NULL DEFAULT \'0\' AFTER `expiry_date`;'; |
| 554 |
$wpdb->query( $sql ); |
| 555 |
} |
| 556 |
|
| 557 |
//upgrade db version to 2.7 |
| 558 |
if ( version_compare( $db_version, '2.7', '<' ) ) { |
| 559 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 560 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `pending` tinyint(1) NOT NULL DEFAULT 0 AFTER `trash`;'; |
| 561 |
$wpdb->query( $sql ); |
| 562 |
|
| 563 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `author_id` int(11) NOT NULL DEFAULT 0 AFTER `pending`;'; |
| 564 |
$wpdb->query( $sql ); |
| 565 |
} |
| 566 |
|
| 567 |
//upgrade db version to 2.8 |
| 568 |
if ( version_compare( $db_version, '2.8', '<' ) ) { |
| 569 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 570 |
$charset_collate = $wpdb->get_charset_collate(); |
| 571 |
$table_name = $wpdb->prefix . self::$_user_available_tbl_name; |
| 572 |
$sql = "CREATE TABLE $table_name ( |
| 573 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 574 |
`user_id` int(11) NOT NULL, |
| 575 |
`type` varchar(8) NOT NULL, |
| 576 |
`value` int(11) NOT NULL, |
| 577 |
UNIQUE KEY id (id) |
| 578 |
) $charset_collate;"; |
| 579 |
dbDelta($sql); |
| 580 |
} |
| 581 |
|
| 582 |
//upgrade db version to 2.9 |
| 583 |
if ( version_compare( $db_version, '2.9', '<' ) ) { |
| 584 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 585 |
|
| 586 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 587 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `size` int(11) NOT NULL DEFAULT 0 AFTER `author_id`;'; |
| 588 |
$wpdb->query( $sql ); |
| 589 |
|
| 590 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `redirect_permalink` tinyint(1) NOT NULL DEFAULT 0 AFTER `size`;'; |
| 591 |
$wpdb->query( $sql ); |
| 592 |
|
| 593 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 594 |
$charset_collate = $wpdb->get_charset_collate(); |
| 595 |
$table_name = $wpdb->prefix . self::$_notifications_tbl_name; |
| 596 |
$sql = "CREATE TABLE $table_name ( |
| 597 |
`id` int(11) NOT NULL AUTO_INCREMENT, |
| 598 |
`status` tinyint(1) NOT NULL DEFAULT 1, |
| 599 |
`name` varchar(256) NOT NULL, |
| 600 |
`trigger_by` varchar(32) NOT NULL, |
| 601 |
`auto_meta` text DEFAULT NULL, |
| 602 |
`send_to_type` varchar(32) DEFAULT NULL, |
| 603 |
`send_to_type_meta` text DEFAULT NULL, |
| 604 |
`from_name` varchar(256) DEFAULT NULL, |
| 605 |
`from_email` varchar(256) DEFAULT NULL, |
| 606 |
`subject` varchar(256) NOT NULL, |
| 607 |
`body` text DEFAULT NULL, |
| 608 |
UNIQUE KEY id (id) |
| 609 |
) $charset_collate;"; |
| 610 |
dbDelta($sql); |
| 611 |
} |
| 612 |
|
| 613 |
//upgrade db version to 3.0 |
| 614 |
if ( version_compare( $db_version, '3.0', '<' ) ) { |
| 615 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 616 |
|
| 617 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 618 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `draft` tinyint(1) NOT NULL DEFAULT 0 AFTER `pending`;'; |
| 619 |
$wpdb->query( $sql ); |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
//upgrade db version to 3.1 |
| 624 |
if ( version_compare( $db_version, '3.1', '<' ) ) { |
| 625 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 626 |
|
| 627 |
$table_name = $wpdb->prefix . self::$_pdfs_tbl_name; |
| 628 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD `restricted` tinyint(1) NOT NULL DEFAULT 0 AFTER `redirect_permalink`;'; |
| 629 |
$wpdb->query( $sql ); |
| 630 |
|
| 631 |
} |
| 632 |
|
| 633 |
//update db version to latest |
| 634 |
update_option( self::$_plugin_saved_db_version_option, self::$_plugin_db_version ); |
| 635 |
delete_option( self::$_plugin_db_upgrading ); |
| 636 |
} |
| 637 |
|
| 638 |
function bsk_pdf_manager_update_file_size_fun(){ |
| 639 |
//check db version first |
| 640 |
$db_version = get_option( self::$_plugin_saved_db_version_option ); |
| 641 |
if ( version_compare( $db_version, '2.9', '<' ) ) { |
| 642 |
return; |
| 643 |
} |
| 644 |
$is_done = get_option( self::$_plugin_db_file_size_done_option, false ); |
| 645 |
if( $is_done ){ |
| 646 |
return; |
| 647 |
} |
| 648 |
$is_doing = get_option( self::$_plugin_db_file_size_doing_option, false ); |
| 649 |
if( $is_doing ){ |
| 650 |
return; |
| 651 |
} |
| 652 |
update_option( self::$_plugin_db_file_size_doing_option, true ); |
| 653 |
|
| 654 |
global $wpdb; |
| 655 |
$sql = 'SELECT `id`, `by_media_uploader`, `file_name` '. |
| 656 |
'FROM `'.esc_sql($wpdb->prefix . self::$_pdfs_tbl_name).'` ' . |
| 657 |
'WHERE ( `by_media_uploader` > 0 OR LENGTH(`file_name`) > 3 ) ' . |
| 658 |
'AND `size` = 0 ' . |
| 659 |
'ORDER BY `id` ASC ' . |
| 660 |
'LIMIT 0, 100'; |
| 661 |
$pdfs_to_process = $wpdb->get_results( $sql ); |
| 662 |
if( !$pdfs_to_process || !is_array( $pdfs_to_process ) || count( $pdfs_to_process ) < 1 ){ |
| 663 |
update_option( self::$_plugin_db_file_size_doing_option, false ); |
| 664 |
update_option( self::$_plugin_db_file_size_done_option, true ); |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
foreach( $pdfs_to_process as $pdf_record ){ |
| 669 |
$file_size = -1; |
| 670 |
if ( $pdf_record->by_media_uploader > 1 ) { |
| 671 |
$file_path = get_attached_file( $pdf_record->by_media_uploader ); |
| 672 |
$file_size = filesize( $file_path ); |
| 673 |
} else { |
| 674 |
if( file_exists( BSKPDFManager::$_upload_root_path.$pdf_record->file_name ) ) { |
| 675 |
$file_size = filesize( BSKPDFManager::$_upload_root_path.$pdf_record->file_name ); |
| 676 |
} |
| 677 |
} |
| 678 |
$data_to_update = array( 'size' => $file_size ); |
| 679 |
$wpdb->update( $wpdb->prefix . self::$_pdfs_tbl_name, $data_to_update, array( 'id' => $pdf_record->id ) ); |
| 680 |
} |
| 681 |
|
| 682 |
update_option( self::$_plugin_db_file_size_doing_option, false ); |
| 683 |
|
| 684 |
return; |
| 685 |
} |
| 686 |
|
| 687 |
function bsk_pdf_manager_post_action(){ |
| 688 |
if (isset($_POST['bsk_pdf_manager_action'])) { |
| 689 |
$bsk_pdfm_action = sanitize_text_field($_POST['bsk_pdf_manager_action']); |
| 690 |
if ($bsk_pdfm_action && strlen($bsk_pdfm_action) > 1){ |
| 691 |
do_action( 'bsk_pdf_manager_' . $bsk_pdfm_action, $_POST ); |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
function bsk_pdf_create_upload_folder_and_set_secure(){ |
| 697 |
|
| 698 |
$cannot_create_message = __( 'Directory <strong>%s</strong> can not be created. Please create it first yourself.', 'bskpdfmanager' ); |
| 699 |
$not_writable_message = __( 'Directory <strong>%s</strong> is not writeable ! Check <a href="http://codex.wordpress.org/Changing_File_Permissions">http://codex.wordpress.org/Changing_File_Permissions</a> for how to set the permission.', 'bskpdfmanager' ); |
| 700 |
|
| 701 |
//create folder to upload |
| 702 |
if (!is_dir(self::$_upload_path)) { |
| 703 |
if ( !wp_mkdir_p(self::$_upload_path) ) { |
| 704 |
|
| 705 |
$message = sprintf($cannot_create_message, self::$_upload_path); |
| 706 |
self::$_plugin_admin_notice_message['upload_folder_missing'] = array( 'message' => $message, 'type' => 'ERROR' ); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
if (!is_writeable(self::$_upload_path)) { |
| 711 |
|
| 712 |
$message = sprintf( $not_writable_message, self::$_upload_path ); |
| 713 |
self::$_plugin_admin_notice_message['upload_folder_not_writeable'] = array( 'message' => $message, 'type' => 'ERROR'); |
| 714 |
} |
| 715 |
|
| 716 |
//copy file to upload foloder |
| 717 |
if (!file_exists(self::$_upload_path.'/index.php')) { |
| 718 |
copy( plugin_dir_path(__FILE__).'/assets/index.php', self::$_upload_path.'/index.php' ); |
| 719 |
} |
| 720 |
|
| 721 |
//create folder for ftp upload |
| 722 |
if (!is_dir(self::$_upload_path_4_ftp)) { |
| 723 |
if (!wp_mkdir_p(self::$_upload_path_4_ftp)) { |
| 724 |
|
| 725 |
$message = sprintf( $cannot_create_message, $_upload_folder_path ); |
| 726 |
self::$_plugin_admin_notice_message['upload_folder_missing_4_ftp'] = array( 'message' => $message, 'type' => 'ERROR' ); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
if (!is_writeable(self::$_upload_path_4_ftp)) { |
| 731 |
|
| 732 |
$message = sprintf( $not_writable_message, $_upload_folder_path ); |
| 733 |
self::$_plugin_admin_notice_message['upload_folder_not_writeable_4_ftp'] = array( 'message' => $message, 'type' => 'ERROR' ); |
| 734 |
} |
| 735 |
|
| 736 |
//copy file to upload foloder |
| 737 |
if (!file_exists(self::$_upload_path_4_ftp.'/index.php')) { |
| 738 |
copy( plugin_dir_path(__FILE__).'/assets/index.php', self::$_upload_path_4_ftp.'/index.php' ); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
function bsk_pdf_manager_load_language(){ |
| 743 |
|
| 744 |
$plugin_settings = get_option( self::$_plugin_settings_option, '' ); |
| 745 |
if( $plugin_settings && is_array( $plugin_settings ) && count( $plugin_settings ) > 0 ){ |
| 746 |
if( isset( $plugin_settings['language'] ) && $plugin_settings['language'] == 'ENGLISH' ){ |
| 747 |
return; |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
load_plugin_textdomain( 'bskpdfmanager', false, basename( BSK_PDFM_PLUGIN_DIR ) . '/languages/' ); |
| 752 |
} |
| 753 |
|
| 754 |
}//end of class |
| 755 |
|
| 756 |
BSKPDFManager::instance(); |
| 757 |
|