| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WPide |
| 4 |
Plugin URI: https://github.com/WPsites/WPide |
| 5 |
Description: WordPress code editor with auto completion of both WordPress and PHP functions with reference, syntax highlighting, line numbers, tabbed editing, automatic backup. |
| 6 |
Version: 2.0.5 |
| 7 |
Author: Simon Dunton |
| 8 |
Author URI: http://www.wpsites.co.uk |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class WPide2 |
| 14 |
|
| 15 |
{ |
| 16 |
|
| 17 |
public $site_url, $plugin_url; |
| 18 |
|
| 19 |
function __construct() { |
| 20 |
|
| 21 |
//add WPide to the menu |
| 22 |
add_action( 'admin_menu', array( &$this, 'add_my_menu_page' ) ); |
| 23 |
|
| 24 |
//hook for processing incoming image saves |
| 25 |
if ( isset($_GET['wpide_save_image']) ){ |
| 26 |
|
| 27 |
//force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' |
| 28 |
$this->override_fs_method('direct'); |
| 29 |
|
| 30 |
add_action('admin_init', array($this, 'wpide_save_image')); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
//only include this plugin if on theme editor, plugin editor or an ajax call |
| 35 |
if ( $_SERVER['PHP_SELF'] === '/wp-admin/admin-ajax.php' || |
| 36 |
$_GET['page'] === 'wpide' ){ |
| 37 |
|
| 38 |
|
| 39 |
//force local file method for testing - you could force other methods 'direct', 'ssh', 'ftpext' or 'ftpsockets' |
| 40 |
$this->override_fs_method('direct'); |
| 41 |
|
| 42 |
// Uncomment any of these calls to add the functionality that you need. |
| 43 |
//add_action('admin_head', 'WPide2::add_admin_head'); |
| 44 |
add_action('admin_init', 'WPide2::add_admin_js'); |
| 45 |
add_action('admin_init', 'WPide2::add_admin_styles'); |
| 46 |
|
| 47 |
//setup jqueryFiletree list callback |
| 48 |
add_action('wp_ajax_jqueryFileTree', 'WPide2::jqueryFileTree_get_list'); |
| 49 |
//setup ajax function to get file contents for editing |
| 50 |
add_action('wp_ajax_wpide_get_file', 'WPide2::wpide_get_file' ); |
| 51 |
//setup ajax function to save file contents and do automatic backup if needed |
| 52 |
add_action('wp_ajax_wpide_save_file', 'WPide2::wpide_save_file' ); |
| 53 |
//setup ajax function to create new item (folder, file etc) |
| 54 |
add_action('wp_ajax_wpide_create_new', 'WPide2::wpide_create_new' ); |
| 55 |
|
| 56 |
//setup ajax function to create new item (folder, file etc) |
| 57 |
add_action('wp_ajax_wpide_image_edit_key', 'WPide2::wpide_image_edit_key' ); |
| 58 |
|
| 59 |
//setup ajax function for startup to get some debug info, checking permissions etc |
| 60 |
add_action('wp_ajax_wpide_startup_check', 'WPide2::wpide_startup_check' ); |
| 61 |
|
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
$WPide->site_url = get_bloginfo('url'); |
| 70 |
|
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
public function override_fs_method($method = 'direct'){ |
| 76 |
|
| 77 |
|
| 78 |
if ( defined('FS_METHOD') ){ |
| 79 |
|
| 80 |
define('WPIDE_FS_METHOD_FORCED', FS_METHOD); //make a note of the forced method |
| 81 |
|
| 82 |
}else{ |
| 83 |
|
| 84 |
define('FS_METHOD', $method); //force direct |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
public static function add_admin_head() |
| 91 |
{ |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function add_admin_js(){ |
| 101 |
|
| 102 |
$plugin_path = plugin_dir_url( __FILE__ ); |
| 103 |
//include file tree |
| 104 |
wp_enqueue_script('jquery-file-tree', plugins_url("jqueryFileTree.js", __FILE__ ) ); |
| 105 |
//include ace |
| 106 |
wp_enqueue_script('ace', plugins_url("ace-0.2.0/src/ace.js", __FILE__ ) ); |
| 107 |
//include ace modes for css, javascript & php |
| 108 |
wp_enqueue_script('ace-mode-css', $plugin_path . 'ace-0.2.0/src/mode-css.js'); |
| 109 |
wp_enqueue_script('ace-mode-javascript', $plugin_path . 'ace-0.2.0/src/mode-javascript.js'); |
| 110 |
wp_enqueue_script('ace-mode-php', $plugin_path . 'ace-0.2.0/src/mode-php.js'); |
| 111 |
//include ace theme |
| 112 |
wp_enqueue_script('ace-theme', plugins_url("ace-0.2.0/src/theme-dawn.js", __FILE__ ) );//monokai is nice |
| 113 |
// wordpress-completion tags |
| 114 |
wp_enqueue_script('wpide-wordpress-completion', plugins_url("js/autocomplete.wordpress.js", __FILE__ ) ); |
| 115 |
// php-completion tags |
| 116 |
wp_enqueue_script('wpide-php-completion', plugins_url("js/autocomplete.php.js", __FILE__ ) ); |
| 117 |
// load editor |
| 118 |
wp_enqueue_script('wpide-load-editor', plugins_url("js/load-editor.js", __FILE__ ) ); |
| 119 |
// load autocomplete dropdown |
| 120 |
wp_enqueue_script('wpide-dd', plugins_url("js/jquery.dd.js", __FILE__ ) ); |
| 121 |
|
| 122 |
// load jquery ui |
| 123 |
wp_enqueue_script('jquery-ui', plugins_url("js/jquery-ui-1.8.20.custom.min.js", __FILE__ ), array('jquery'), '1.8.20'); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
public static function add_admin_styles(){ |
| 131 |
|
| 132 |
//main wpide styles |
| 133 |
wp_register_style( 'wpide_style', plugins_url('wpide.css', __FILE__) ); |
| 134 |
wp_enqueue_style( 'wpide_style' ); |
| 135 |
//filetree styles |
| 136 |
wp_register_style( 'wpide_filetree_style', plugins_url('jqueryFileTree.css', __FILE__) ); |
| 137 |
wp_enqueue_style( 'wpide_filetree_style' ); |
| 138 |
//autocomplete dropdown styles |
| 139 |
wp_register_style( 'wpide_dd_style', plugins_url('dd.css', __FILE__) ); |
| 140 |
wp_enqueue_style( 'wpide_dd_style' ); |
| 141 |
|
| 142 |
//jquery ui styles |
| 143 |
wp_register_style( 'wpide_jqueryui_style', plugins_url('css/flick/jquery-ui-1.8.20.custom.css', __FILE__) ); |
| 144 |
wp_enqueue_style( 'wpide_jqueryui_style' ); |
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function jqueryFileTree_get_list() { |
| 152 |
//check the user has the permissions |
| 153 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 154 |
if ( !current_user_can('edit_themes') ) |
| 155 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 156 |
|
| 157 |
//setup wp_filesystem api |
| 158 |
global $wp_filesystem; |
| 159 |
if ( ! WP_Filesystem($creds) ) |
| 160 |
return false; |
| 161 |
|
| 162 |
$_POST['dir'] = urldecode($_POST['dir']); |
| 163 |
$root = WP_CONTENT_DIR; |
| 164 |
|
| 165 |
if( $wp_filesystem->exists($root . $_POST['dir']) ) { |
| 166 |
//$files = scandir($root . $_POST['dir']); |
| 167 |
//print_r($files); |
| 168 |
$files = $wp_filesystem->dirlist($root . $_POST['dir']); |
| 169 |
//print_r($files); |
| 170 |
|
| 171 |
echo "<ul class=\"jqueryFileTree\" style=\"display: none;\">"; |
| 172 |
if( count($files) > 0 ) { |
| 173 |
|
| 174 |
// All dirs |
| 175 |
foreach( $files as $file => $file_info ) { |
| 176 |
if( $file != '.' && $file != '..' && $file_info['type']=='d' ) { |
| 177 |
echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "/\">" . htmlentities($file) . "</a></li>"; |
| 178 |
} |
| 179 |
} |
| 180 |
// All files |
| 181 |
foreach( $files as $file => $file_info ) { |
| 182 |
if( $file != '.' && $file != '..' && $file_info['type']!='d') { |
| 183 |
$ext = preg_replace('/^.*\./', '', $file); |
| 184 |
echo "<li class=\"file ext_$ext\"><a href=\"#\" rel=\"" . htmlentities($_POST['dir'] . $file) . "\">" . htmlentities($file) . "</a></li>"; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
//output toolbar for creating new file, folder etc |
| 189 |
echo "<li class=\"create_new\"><a class='new_directory' title='Create a new directory here.' href=\"#\" rel=\"{type: 'directory', path: '" . htmlentities($_POST['dir']) . "'}\"></a> <a class='new_file' title='Create a new file here.' href=\"#\" rel=\"{type: 'file', path: '" . htmlentities($_POST['dir']) . "'}\"></a><br style='clear:both;' /></li>"; |
| 190 |
echo "</ul>"; |
| 191 |
} |
| 192 |
|
| 193 |
die(); // this is required to return a proper result |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public static function wpide_get_file() { |
| 198 |
//check the user has the permissions |
| 199 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 200 |
if ( !current_user_can('edit_themes') ) |
| 201 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 202 |
|
| 203 |
//setup wp_filesystem api |
| 204 |
global $wp_filesystem; |
| 205 |
if ( ! WP_Filesystem($creds) ) |
| 206 |
return false; |
| 207 |
|
| 208 |
|
| 209 |
$root = WP_CONTENT_DIR; |
| 210 |
$file_name = $root . stripslashes($_POST['filename']); |
| 211 |
echo $wp_filesystem->get_contents($file_name); |
| 212 |
die(); // this is required to return a proper result |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static function wpide_image_edit_key() { |
| 218 |
|
| 219 |
//check the user has the permissions |
| 220 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 221 |
if ( !current_user_can('edit_themes') ) |
| 222 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 223 |
|
| 224 |
//create a nonce based on the image path |
| 225 |
echo wp_create_nonce( 'wpide_image_edit' . $_POST['file'] ); |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
public static function wpide_create_new() { |
| 230 |
//check the user has the permissions |
| 231 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 232 |
if ( !current_user_can('edit_themes') ) |
| 233 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 234 |
|
| 235 |
//setup wp_filesystem api |
| 236 |
global $wp_filesystem; |
| 237 |
if ( ! WP_Filesystem($creds) ) |
| 238 |
return false; |
| 239 |
|
| 240 |
$root = WP_CONTENT_DIR; |
| 241 |
|
| 242 |
//check all required vars are passed |
| 243 |
if (strlen($_POST['path'])>0 && strlen($_POST['type'])>0 && strlen($_POST['file'])>0){ |
| 244 |
|
| 245 |
|
| 246 |
$filename = sanitize_file_name( $_POST['file'] ); |
| 247 |
$path = $_POST['path']; |
| 248 |
|
| 249 |
if ($_POST['type'] == "directory"){ |
| 250 |
|
| 251 |
$write_result = $wp_filesystem->mkdir($root . $path . $filename, FS_CHMOD_DIR); |
| 252 |
|
| 253 |
if ($write_result){ |
| 254 |
die("1"); //created |
| 255 |
}else{ |
| 256 |
echo "Problem creating directory" . $root . $path . $filename; |
| 257 |
} |
| 258 |
|
| 259 |
}else if ($_POST['type'] == "file"){ |
| 260 |
|
| 261 |
$write_result = $wp_filesystem->put_contents( |
| 262 |
$root . $path . $filename, |
| 263 |
' ', |
| 264 |
FS_CHMOD_FILE // predefined mode settings for WP files |
| 265 |
); |
| 266 |
|
| 267 |
if ($write_result){ |
| 268 |
die("1"); //created |
| 269 |
}else{ |
| 270 |
echo "Problem creating file " . $root . $path . $filename; |
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
//print_r($_POST); |
| 277 |
|
| 278 |
|
| 279 |
} |
| 280 |
echo "0"; |
| 281 |
die(); // this is required to return a proper result |
| 282 |
} |
| 283 |
|
| 284 |
public static function wpide_save_file() { |
| 285 |
//check the user has the permissions |
| 286 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 287 |
if ( !current_user_can('edit_themes') ) |
| 288 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 289 |
|
| 290 |
//setup wp_filesystem api |
| 291 |
global $wp_filesystem; |
| 292 |
if ( ! WP_Filesystem($creds) ) |
| 293 |
echo "Cannot initialise the WP file system API"; |
| 294 |
|
| 295 |
//save a copy of the file and create a backup just in case |
| 296 |
$root = WP_CONTENT_DIR; |
| 297 |
$file_name = $root . stripslashes($_POST['filename']); |
| 298 |
|
| 299 |
//set backup filename |
| 300 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 301 |
//create backup directory if not there |
| 302 |
$new_file_info = pathinfo($backup_path); |
| 303 |
if (!$wp_filesystem->is_dir($new_file_info['dirname'])) wp_mkdir_p( $new_file_info['dirname'] ); //should use the filesytem api here but there isn't a comparable command right now |
| 304 |
|
| 305 |
//do backup |
| 306 |
$wp_filesystem->copy( $file_name, $backup_path ); |
| 307 |
|
| 308 |
//save file |
| 309 |
if( $wp_filesystem->put_contents( $file_name, stripslashes($_POST['content'])) ) { |
| 310 |
$result = "success"; |
| 311 |
} |
| 312 |
|
| 313 |
die($result); // this is required to return a proper result |
| 314 |
} |
| 315 |
|
| 316 |
public static function wpide_save_image() { |
| 317 |
|
| 318 |
$filennonce = split("::", $_POST["opt"]); //file::nonce |
| 319 |
|
| 320 |
//check the user has a valid nonce |
| 321 |
//we are checking two variations of the nonce, one as-is and another that we have removed a trailing zero from |
| 322 |
//this is to get around some sort of bug where a nonce generated on another page has a trailing zero and a nonce generated/checked here doesn't have the zero |
| 323 |
if (! wp_verify_nonce( $filennonce[1], 'wpide_image_edit' . $filennonce[0]) && |
| 324 |
! wp_verify_nonce( rtrim($filennonce[1], "0") , 'wpide_image_edit' . $filennonce[0])) { |
| 325 |
die('Security check'); //die because both checks failed |
| 326 |
} |
| 327 |
//check the user has the permissions |
| 328 |
if ( !current_user_can('edit_themes') ) |
| 329 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 330 |
|
| 331 |
|
| 332 |
$_POST['content'] = base64_decode($_POST["data"]); //image content |
| 333 |
$_POST['filename'] = $filennonce[0]; //filename |
| 334 |
|
| 335 |
//setup wp_filesystem api |
| 336 |
global $wp_filesystem; |
| 337 |
|
| 338 |
if ( ! WP_Filesystem($creds) ) |
| 339 |
echo "Cannot initialise the WP file system API"; |
| 340 |
|
| 341 |
//save a copy of the file and create a backup just in case |
| 342 |
$root = WP_CONTENT_DIR; |
| 343 |
$file_name = $root . stripslashes($_POST['filename']); |
| 344 |
|
| 345 |
//set backup filename |
| 346 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 347 |
//create backup directory if not there |
| 348 |
$new_file_info = pathinfo($backup_path); |
| 349 |
if (!$wp_filesystem->is_dir($new_file_info['dirname'])) wp_mkdir_p( $new_file_info['dirname'] ); //should use the filesytem api here but there isn't a comparable command right now |
| 350 |
|
| 351 |
//do backup |
| 352 |
$wp_filesystem->move( $file_name, $backup_path ); |
| 353 |
|
| 354 |
|
| 355 |
//save file |
| 356 |
if( $wp_filesystem->put_contents( $file_name, $_POST['content']) ) { |
| 357 |
$result = "success"; |
| 358 |
} |
| 359 |
|
| 360 |
if ($result == "success"){ |
| 361 |
wp_die('<p>'.__('<strong>Image saved.</strong> <br />You may <a href="JavaScript:window.close();">close this window / tab</a>.').'</p>'); |
| 362 |
}else{ |
| 363 |
wp_die('<p>'.__('<strong>Problem saving image.</strong> <br /><a href="JavaScript:window.close();">Close this window / tab</a> and try editing the image again.').'</p>'); |
| 364 |
} |
| 365 |
//print_r($_POST); |
| 366 |
|
| 367 |
|
| 368 |
//return; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
public static function wpide_startup_check() { |
| 373 |
global $wp_filesystem, $wp_version; |
| 374 |
|
| 375 |
echo "\n\n\n\nWPIDE STARUP CHECKS \n"; |
| 376 |
echo "___________________ \n\n"; |
| 377 |
|
| 378 |
//WordPress version |
| 379 |
if ($wp_version > 3){ |
| 380 |
echo "WordPress version = " . $wp_version . "\n\n"; |
| 381 |
}else{ |
| 382 |
echo "WordPress version = " . $wp_version . " (which is too old to run WPide) \n\n"; |
| 383 |
} |
| 384 |
|
| 385 |
//check the user has the permissions |
| 386 |
check_admin_referer('plugin-name-action_wpidenonce'); |
| 387 |
if ( !current_user_can('edit_themes') ) |
| 388 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 389 |
|
| 390 |
if ( defined( 'WPIDE_FS_METHOD_FORCED' ) ){ |
| 391 |
echo "WordPress filesystem API has been forced to use the " . WPIDE_FS_METHOD_FORCED . " method by another plugin/WordPress. \n\n"; |
| 392 |
} |
| 393 |
|
| 394 |
//setup wp_filesystem api |
| 395 |
$wpide_filesystem_before = $wp_filesystem; |
| 396 |
if ( ! WP_Filesystem($creds) ) { |
| 397 |
|
| 398 |
echo "There has been a problem initialising the filesystem API \n\n"; |
| 399 |
echo "Filesystem API before this plugin ran: \n\n" . print_r($wpide_filesystem_before, true); |
| 400 |
echo "Filesystem API now: \n\n" . print_r($wp_filesystem, true); |
| 401 |
|
| 402 |
} |
| 403 |
unset($wpide_filesystem_before); |
| 404 |
|
| 405 |
|
| 406 |
$root = WP_CONTENT_DIR; |
| 407 |
|
| 408 |
|
| 409 |
//Running webservers user and group |
| 410 |
echo "Web server user/group = " . getenv('APACHE_RUN_USER') . ":" . getenv('APACHE_RUN_GROUP') . "\n"; |
| 411 |
//wp-content user and group |
| 412 |
echo "wp-content owner/group = " . $wp_filesystem->owner( $root ) . ":" . $wp_filesystem->group( $root ) . "\n\n"; |
| 413 |
|
| 414 |
|
| 415 |
//check we can list wp-content files |
| 416 |
if( $wp_filesystem->exists( $root ) ){ |
| 417 |
|
| 418 |
$files = $wp_filesystem->dirlist( $root ); |
| 419 |
if ( count($files) > 0){ |
| 420 |
echo "wp-content folder exists and contains ". count($files) ." files \n"; |
| 421 |
}else{ |
| 422 |
echo "wp-content folder exists but we cannot read it's contents \n"; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
// $wp_filesystem->owner() $wp_filesystem->group() $wp_filesystem->is_writable() $wp_filesystem->is_readable() |
| 427 |
echo "\nUsing the ".$wp_filesystem->method." method of the WP filesystem API\n"; |
| 428 |
|
| 429 |
//wp-content editable? |
| 430 |
echo "The wp-content folder ". ( $wp_filesystem->is_readable( $root )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 431 |
|
| 432 |
|
| 433 |
//plugins folder editable |
| 434 |
echo "The wp-content/plugins folder ". ( $wp_filesystem->is_readable( $root."/plugins" )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root."/plugins" )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 435 |
|
| 436 |
|
| 437 |
//themes folder editable |
| 438 |
echo "The wp-content/themes folder ". ( $wp_filesystem->is_readable( $root."/themes" )==1 ? "IS":"IS NOT" ) ." readable and ". ( $wp_filesystem->is_writable( $root."/themes" )==1 ? "IS":"IS NOT" ) ." writable by this method \n"; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
echo "___________________ \n\n\n\n"; |
| 443 |
|
| 444 |
echo " If the file tree to the right is empty there is a possibility that your server permissions are not compatible with this plugin. \n The startup information above may shed some light on things. \n Paste that information into the support forum for further assistance."; |
| 445 |
|
| 446 |
|
| 447 |
die(); |
| 448 |
|
| 449 |
//set backup filename |
| 450 |
$backup_path = ABSPATH .'wp-content/plugins/' . basename(dirname(__FILE__)) .'/backups/' . str_replace( str_replace('\\', "/", ABSPATH), '', $file_name) .'.'.date("YmdH"); |
| 451 |
//create backup directory if not there |
| 452 |
$new_file_info = pathinfo($backup_path); |
| 453 |
if (!$wp_filesystem->is_dir($new_file_info['dirname'])) wp_mkdir_p( $new_file_info['dirname'] ); //should use the filesytem api here but there isn't a comparable command right now |
| 454 |
|
| 455 |
//do backup |
| 456 |
$wp_filesystem->move( $file_name, $backup_path ); |
| 457 |
|
| 458 |
|
| 459 |
//save file |
| 460 |
if( $wp_filesystem->put_contents( $file_name, $_POST['content']) ) { |
| 461 |
$result = "success"; |
| 462 |
} |
| 463 |
|
| 464 |
if ($result == "success"){ |
| 465 |
wp_die('<p>'.__('<strong>Image saved.</strong> <br />You may <a href="JavaScript:window.close();">close this window / tab</a>.').'</p>'); |
| 466 |
}else{ |
| 467 |
wp_die('<p>'.__('<strong>Problem saving image.</strong> <br /><a href="JavaScript:window.close();">Close this window / tab</a> and try editing the image again.').'</p>'); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
//return; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public function add_my_menu_page() { |
| 478 |
//add_menu_page("wpide", "wpide","edit_themes", "wpidesettings", array( &$this, 'my_menu_page') ); |
| 479 |
add_menu_page('WPide', 'WPide', 'edit_themes', "wpide", array( &$this, 'my_menu_page' )); |
| 480 |
} |
| 481 |
|
| 482 |
public function my_menu_page() { |
| 483 |
if ( !current_user_can('edit_themes') ) |
| 484 |
wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site. SORRY').'</p>'); |
| 485 |
|
| 486 |
$app_url = get_bloginfo('url'); //need to make this https if we are currently looking on the site using https (even though https for admin might not be forced it can still cause issues) |
| 487 |
if (is_ssl()) $app_url = str_replace("http:", "https:", $app_url); |
| 488 |
|
| 489 |
?> |
| 490 |
<script> |
| 491 |
|
| 492 |
var wpide_app_path = "<?php echo plugin_dir_url( __FILE__ ); ?>"; |
| 493 |
|
| 494 |
function the_filetree() { |
| 495 |
jQuery('#wpide_file_browser').fileTree({ script: ajaxurl }, function(parent, file) { |
| 496 |
|
| 497 |
if ( jQuery(parent).hasClass("create_new") ){ //create new file/folder |
| 498 |
//to create a new item we need to know the name of it so show input |
| 499 |
|
| 500 |
var item = eval('('+file+')'); |
| 501 |
|
| 502 |
//hide all inputs just incase one is selected |
| 503 |
jQuery(".new_item_inputs").hide(); |
| 504 |
//show the input form for this |
| 505 |
jQuery("div.new_" + item.type).show(); |
| 506 |
jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").focus(); |
| 507 |
jQuery("div.new_" + item.type + " input[name='new_" + item.type + "']").attr("rel", file); |
| 508 |
|
| 509 |
|
| 510 |
}else if ( jQuery(".wpide_tab[rel='"+file+"']").length > 0) { //focus existing tab |
| 511 |
jQuery(".wpide_tab[sessionrel='"+ jQuery(".wpide_tab[rel='"+file+"']").attr("sessionrel") +"']").click();//focus the already open tab |
| 512 |
}else{ //open file |
| 513 |
|
| 514 |
var image_patern =new RegExp("(\.jpg|\.gif|\.png|\.bmp)"); |
| 515 |
if ( image_patern.test(file) ){ |
| 516 |
//it's an image so open it for editing |
| 517 |
|
| 518 |
//using modal+iframe |
| 519 |
if ("lets not" == "use the modal for now"){ |
| 520 |
|
| 521 |
var NewDialog = jQuery('<div id="MenuDialog">\ |
| 522 |
<iframe src="http://www.sumopaint.com/app/?key=ebcdaezjeojbfgih&target=<?php echo get_bloginfo('url') . "?action=wpide_image_save";?>&url=<?php echo get_bloginfo('url') . "/wp-content";?>' + file + '&title=Edit image&service=Save back to WPide" width="100%" height="600px"> </iframe>\ |
| 523 |
</div>'); |
| 524 |
NewDialog.dialog({ |
| 525 |
modal: true, |
| 526 |
title: "title", |
| 527 |
show: 'clip', |
| 528 |
hide: 'clip', |
| 529 |
width:'800', |
| 530 |
height:'600' |
| 531 |
}); |
| 532 |
|
| 533 |
}else{ //open in new tab/window |
| 534 |
|
| 535 |
var data = { action: 'wpide_image_edit_key', file: file, _wpnonce: jQuery('#_wpnonce').val(), _wp_http_referer: jQuery('#_wp_http_referer').val() }; |
| 536 |
var image_data = ''; |
| 537 |
jQuery.ajaxSetup({async:false}); //we need to wait until we get the response before opening the window |
| 538 |
jQuery.post(ajaxurl, data, function(response) { |
| 539 |
|
| 540 |
//with the response (which is a nonce), build the json data to pass to the image editor. The edit key (nonce) is only valid to edit this image |
| 541 |
image_data = file+'::'+response; |
| 542 |
|
| 543 |
}); |
| 544 |
|
| 545 |
|
| 546 |
window.open('http://www.sumopaint.com/app/?key=ebcdaezjeojbfgih&url=<?php echo $app_url. "/wp-content";?>' + file + '&opt=' + image_data + '&title=Edit image&service=Save back to WPide&target=<?php echo urlencode( $app_url . "/wp-admin/admin.php?wpide_save_image=yes" ) ;?>'); |
| 547 |
|
| 548 |
} |
| 549 |
|
| 550 |
}else{ |
| 551 |
jQuery(parent).addClass('wait'); |
| 552 |
|
| 553 |
wpide_set_file_contents(file, function(){ |
| 554 |
|
| 555 |
//once file loaded remove the wait class/indicator |
| 556 |
jQuery(parent).removeClass('wait'); |
| 557 |
|
| 558 |
}); |
| 559 |
|
| 560 |
jQuery('#filename').val(file); |
| 561 |
} |
| 562 |
|
| 563 |
} |
| 564 |
|
| 565 |
}); |
| 566 |
} |
| 567 |
|
| 568 |
jQuery(document).ready(function($) { |
| 569 |
// Handler for .ready() called. |
| 570 |
the_filetree() ; |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
}); |
| 578 |
</script> |
| 579 |
|
| 580 |
<?php |
| 581 |
$url = wp_nonce_url('admin.php?page=wpide','plugin-name-action_wpidenonce'); |
| 582 |
if ( ! WP_Filesystem($creds) ) { |
| 583 |
request_filesystem_credentials($url, '', true, false, null); |
| 584 |
return; |
| 585 |
} |
| 586 |
?> |
| 587 |
|
| 588 |
<div id="poststuff" class="metabox-holder has-right-sidebar"> |
| 589 |
|
| 590 |
<div id="side-info-column" class="inner-sidebar"> |
| 591 |
|
| 592 |
<div id="wpide_info"><div id="wpide_info_content"></div> </div> |
| 593 |
|
| 594 |
<div id="submitdiv" class="postbox "> |
| 595 |
<h3 class="hndle"><span>Files</span></h3> |
| 596 |
<div class="inside"> |
| 597 |
<div class="submitbox" id="submitpost"> |
| 598 |
<div id="minor-publishing"> |
| 599 |
</div> |
| 600 |
<div id="major-publishing-actions"> |
| 601 |
<div id="wpide_file_browser"></div> |
| 602 |
<br style="clear:both;" /> |
| 603 |
<div class="new_file new_item_inputs"> |
| 604 |
<label for="new_folder">File name</label><input class="has_data" name="new_file" type="text" rel="" value="" placeholder="Filename.ext" /> |
| 605 |
<a href="#" id="wpide_create_new_file" class="button-primary">CREATE</a> |
| 606 |
</div> |
| 607 |
<div class="new_directory new_item_inputs"> |
| 608 |
<label for="new_directory">Directory name</label><input class="has_data" name="new_directory" type="text" rel="" value="" placeholder="Filename.ext" /> |
| 609 |
<a href="#" id="wpide_create_new_directory" class="button-primary">CREATE</a> |
| 610 |
</div> |
| 611 |
<div class="clear"></div> |
| 612 |
</div> |
| 613 |
</div> |
| 614 |
</div> |
| 615 |
</div> |
| 616 |
|
| 617 |
|
| 618 |
</div> |
| 619 |
|
| 620 |
<div id="post-body"> |
| 621 |
<div id="wpide_toolbar" class="quicktags-toolbar"> |
| 622 |
<div id="wpide_toolbar_tabs"> </div> |
| 623 |
<div id="dialog_window_minimized_container"></div> |
| 624 |
</div> |
| 625 |
|
| 626 |
<div id="wpide_toolbar_buttons"> |
| 627 |
<div id="wpide_message" class="error highlight"></div> |
| 628 |
<a href="#"></a> <a href="#"></a> </div> |
| 629 |
|
| 630 |
|
| 631 |
<div id='fancyeditordiv'></div> |
| 632 |
|
| 633 |
<form id="wpide_save_container" action="" method="get"> |
| 634 |
<a href="#" id="wpide_save" class="button-primary">SAVE |
| 635 |
FILE</a> |
| 636 |
<input type="hidden" id="filename" name="filename" value="" /> |
| 637 |
<?php |
| 638 |
if ( function_exists('wp_nonce_field') ) |
| 639 |
wp_nonce_field('plugin-name-action_wpidenonce'); |
| 640 |
?> |
| 641 |
</form> |
| 642 |
</div> |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
</div> |
| 647 |
|
| 648 |
<?php |
| 649 |
} |
| 650 |
|
| 651 |
} |
| 652 |
add_action("init", create_function('', 'new WPide2();')); |
| 653 |
?> |
| 654 |
|