| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Pluginception |
| 4 |
Plugin URI: http://ottopress.com/wordpress-plugins/pluginception |
| 5 |
Description: A plugin to create other plugins. Pluginception. |
| 6 |
Version: 1.3 |
| 7 |
Author: Otto |
| 8 |
Author URI: http://ottopress.com |
| 9 |
Text Domain: pluginception |
| 10 |
License: GPLv2 only |
| 11 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 12 |
|
| 13 |
Copyright 2011-2013 Samuel Wood (email : otto@ottodestruct.com) |
| 14 |
|
| 15 |
This program is free software; you can redistribute it and/or modify |
| 16 |
it under the terms of the GNU General Public License version 2, |
| 17 |
as published by the Free Software Foundation. |
| 18 |
|
| 19 |
You may NOT assume that you can use any other version of the GPL. |
| 20 |
|
| 21 |
This program is distributed in the hope that it will be useful, |
| 22 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
GNU General Public License for more details. |
| 25 |
|
| 26 |
The license for this software can likely be found here: |
| 27 |
http://www.gnu.org/licenses/gpl-2.0.html |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
// Load the textdomain |
| 32 |
add_action('init', 'pluginception_load_textdomain'); |
| 33 |
function pluginception_load_textdomain() { |
| 34 |
load_plugin_textdomain('pluginception', false, dirname(plugin_basename(__FILE__))); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
add_action('admin_menu', 'pluginception_admin_add_page'); |
| 39 |
function pluginception_admin_add_page() { |
| 40 |
add_plugins_page( |
| 41 |
esc_html__('Create a New Plugin','pluginception'), |
| 42 |
esc_html__('Create a New Plugin','pluginception'), |
| 43 |
'edit_plugins', |
| 44 |
'pluginception', |
| 45 |
'pluginception_options_page' |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
function pluginception_options_page() { |
| 50 |
$results = pluginception_create_plugin(); |
| 51 |
|
| 52 |
if ( $results === true ) return; |
| 53 |
?> |
| 54 |
<div class="wrap"> |
| 55 |
<?php screen_icon(); ?> |
| 56 |
<h2><?php esc_html_e('Create a New Plugin','pluginception'); ?></h2> |
| 57 |
<?php settings_errors(); ?> |
| 58 |
<form method="post" action=""> |
| 59 |
<?php wp_nonce_field('pluginception_nonce'); ?> |
| 60 |
<table class="form-table"> |
| 61 |
<?php |
| 62 |
$opts = array( |
| 63 |
'name' => esc_html__('Plugin Name', 'pluginception'), |
| 64 |
'slug' => esc_html__('Plugin Slug (optional)', 'pluginception'), |
| 65 |
'uri' => esc_html__('Plugin URI (optional)', 'pluginception'), |
| 66 |
'description' => esc_html__('Description (optional)', 'pluginception'), |
| 67 |
'version' => esc_html__('Version (optional)', 'pluginception'), |
| 68 |
'author' => esc_html__('Author (optional)', 'pluginception'), |
| 69 |
'author_uri' => esc_html__('Author URI (optional)', 'pluginception'), |
| 70 |
'license' => esc_html__('License (optional)', 'pluginception'), |
| 71 |
'license_uri' => esc_html__('License URI (optional)', 'pluginception'), |
| 72 |
); |
| 73 |
|
| 74 |
foreach ($opts as $slug=>$title) { |
| 75 |
$value = ''; |
| 76 |
if (!empty($results['pluginception_'.$slug])) $value = esc_attr($results['pluginception_'.$slug]); |
| 77 |
echo "<tr valign='top'><th scope='row'>{$title}</th><td><input class='regular-text' type='text' name='" . esc_attr("pluginception_{$slug}") . "' value='{$value}'></td></tr>\n"; |
| 78 |
} |
| 79 |
?> |
| 80 |
</table> |
| 81 |
<?php submit_button( esc_html__('Create a blank plugin and activate it!', 'pluginception') ); ?> |
| 82 |
</form> |
| 83 |
</div> |
| 84 |
<?php |
| 85 |
} |
| 86 |
|
| 87 |
function pluginception_create_plugin() { |
| 88 |
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) |
| 89 |
return false; |
| 90 |
|
| 91 |
check_admin_referer('pluginception_nonce'); |
| 92 |
|
| 93 |
// remove the magic quotes |
| 94 |
$_POST = stripslashes_deep( $_POST ); |
| 95 |
|
| 96 |
if (empty($_POST['pluginception_name'])) { |
| 97 |
add_settings_error( 'pluginception', 'required_name',esc_html__('Plugin Name is required', 'pluginception'), 'error' ); |
| 98 |
return $_POST; |
| 99 |
} |
| 100 |
|
| 101 |
if ( empty($_POST['pluginception_slug'] ) ) { |
| 102 |
$_POST['pluginception_slug'] = sanitize_title($_POST['pluginception_name']); |
| 103 |
} else { |
| 104 |
$_POST['pluginception_slug'] = sanitize_title($_POST['pluginception_slug']); |
| 105 |
} |
| 106 |
|
| 107 |
if ( file_exists(trailingslashit(WP_PLUGIN_DIR).$_POST['pluginception_slug'] ) ) { |
| 108 |
add_settings_error( 'pluginception', 'existing_plugin', esc_html__('That plugin appears to already exist. Use a different slug or name.', 'pluginception'), 'error' ); |
| 109 |
return $_POST; |
| 110 |
} |
| 111 |
|
| 112 |
$form_fields = array ('pluginception_name', 'pluginception_slug', 'pluginception_uri', 'pluginception_description', 'pluginception_version', |
| 113 |
'pluginception_author', 'pluginception_author_uri', 'pluginception_license', 'pluginception_license_uri'); |
| 114 |
$method = ''; // TODO TESTING |
| 115 |
|
| 116 |
// okay, let's see about getting credentials |
| 117 |
$url = wp_nonce_url('plugins.php?page=pluginception','pluginception_nonce'); |
| 118 |
if (false === ($creds = request_filesystem_credentials($url, $method, false, false, $form_fields) ) ) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
// now we have some credentials, try to get the wp_filesystem running |
| 123 |
if ( ! WP_Filesystem($creds) ) { |
| 124 |
// our credentials were no good, ask the user for them again |
| 125 |
request_filesystem_credentials($url, $method, true, false, $form_fields); |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
global $wp_filesystem; |
| 130 |
|
| 131 |
// create the plugin directory |
| 132 |
$plugdir = $wp_filesystem->wp_plugins_dir() . $_POST['pluginception_slug']; |
| 133 |
|
| 134 |
if ( ! $wp_filesystem->mkdir($plugdir) ) { |
| 135 |
add_settings_error( 'pluginception', 'create_directory', esc_html__('Unable to create the plugin directory.', 'pluginception'), 'error' ); |
| 136 |
return $_POST; |
| 137 |
} |
| 138 |
|
| 139 |
// create the plugin header |
| 140 |
|
| 141 |
$header = <<<END |
| 142 |
<?php |
| 143 |
/* |
| 144 |
Plugin Name: {$_POST['pluginception_name']} |
| 145 |
Plugin URI: {$_POST['pluginception_uri']} |
| 146 |
Description: {$_POST['pluginception_description']} |
| 147 |
Version: {$_POST['pluginception_version']} |
| 148 |
Author: {$_POST['pluginception_author']} |
| 149 |
Author URI: {$_POST['pluginception_author_uri']} |
| 150 |
License: {$_POST['pluginception_license']} |
| 151 |
License URI: {$_POST['pluginception_license_uri']} |
| 152 |
*/ |
| 153 |
|
| 154 |
END; |
| 155 |
|
| 156 |
$plugfile = trailingslashit($plugdir).$_POST['pluginception_slug'].'.php'; |
| 157 |
|
| 158 |
if ( ! $wp_filesystem->put_contents( $plugfile, $header, FS_CHMOD_FILE) ) { |
| 159 |
add_settings_error( 'pluginception', 'create_file', esc_html__('Unable to create the plugin file.', 'pluginception'), 'error' ); |
| 160 |
} |
| 161 |
|
| 162 |
$plugslug = $_POST['pluginception_slug'].'/'.$_POST['pluginception_slug'].'.php'; |
| 163 |
$plugeditor = admin_url('plugin-editor.php?file='.$_POST['pluginception_slug'].'%2F'.$_POST['pluginception_slug'].'.php'); |
| 164 |
|
| 165 |
if ( null !== activate_plugin( $plugslug, '', false, true ) ) { |
| 166 |
add_settings_error( 'pluginception', 'activate_plugin', esc_html__('Unable to activate the new plugin.', 'pluginception'), 'error' ); |
| 167 |
} |
| 168 |
|
| 169 |
// plugin created and activated, redirect to the plugin editor |
| 170 |
?> |
| 171 |
<script type="text/javascript"> |
| 172 |
<!-- |
| 173 |
window.location = "<?php echo esc_url_raw( $plugeditor ); ?>" |
| 174 |
//--> |
| 175 |
</script> |
| 176 |
<?php |
| 177 |
|
| 178 |
/* translators: inline link to plugin editor */ |
| 179 |
$message = sprintf(esc_html__('The new plugin has been created and activated. You can %1$sgo to the editor%2$s if your browser does not redirect you.', 'pluginception'), '<a href="'.$plugeditor.'">', '</a>'); |
| 180 |
|
| 181 |
add_settings_error('pluginception', 'plugin_active', $message, 'pluginception', 'updated'); |
| 182 |
|
| 183 |
return true; |
| 184 |
} |