From 71bd3e8a833afd912bca1522fa08693887b84aa5 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 3 Oct 2016 20:21:46 +0100 Subject: [PATCH] Code cleanup --- .codeclimate.yml | 2 +- src/Providers/WordpressServiceProvider.php | 58 +++++++++++++++++++++++++++++++++++++++++++--------------- src/Support/Wordpress.php | 1 + 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index ffb316a..0f9ba3b 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -10,7 +10,7 @@ engines: enabled: true config: file_extensions: "php" - rulesets: "codesize,controversial,design,naming,phpmd.xml" + rulesets: "codesize,controversial,design,phpmd.xml" ratings: paths: - "**.php" diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 845d103..8a82cc4 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.php @@ -24,7 +24,8 @@ class WordpressServiceProvider extends ServiceProvider public function register() { // get the path wordpress is installed in - define('WP_PATH', + define( + 'WP_PATH', json_decode( file_get_contents($this->app->basePath() . DIRECTORY_SEPARATOR . 'composer.json'), true @@ -42,12 +43,36 @@ class WordpressServiceProvider extends ServiceProvider * Set up the configuration values that wp-config.php * does. Use all the values out of .env instead. * + * @SuppressWarnings(PHPMD.CamelCaseVariableName) + * @SuppressWarnings(PHPMD.Superglobals) * @return void */ protected function setConfig() { + // Wordpress requires $table_prefix rather than another constant. $table_prefix = 'wp_'; + define('WP_DEBUG', $this->app->make('config')->get('app.debug')); + define('WP_DEBUG_DISPLAY', WP_DEBUG); + + $this->setDatabaseConstants($table_prefix); + $this->setAuthenticationConstants(); + $this->setLocationConstants(); + + if ($this->app->runningInConsole()) { + $_SERVER['SERVER_PROTOCOL'] = 'https'; + } + + require ABSPATH . 'wp-settings.php'; + } + + /** + * Set all the database constants used by Wordpress. + * + * @param string $tablePrefix + */ + private function setDatabaseConstants($tablePrefix) + { $db = DB::getConfig(null); define('DB_NAME', $db['database']); @@ -56,8 +81,14 @@ class WordpressServiceProvider extends ServiceProvider define('DB_HOST', $db['host']); define('DB_CHARSET', $db['charset']); define('DB_COLLATE', $db['collation']); - define('DB_PREFIX', $table_prefix); + define('DB_PREFIX', $tablePrefix); + } + /** + * Set all the authentication constants used by Wordpress. + */ + private function setAuthenticationConstants() + { define('AUTH_KEY', $this->app->make('config')->get('wordpress.auth_key')); define('SECURE_AUTH_KEY', $this->app->make('config')->get('wordpress.secure_auth_key')); define('LOGGED_IN_KEY', $this->app->make('config')->get('wordpress.logged_in_key')); @@ -66,12 +97,15 @@ class WordpressServiceProvider extends ServiceProvider define('SECURE_AUTH_SALT', $this->app->make('config')->get('wordpress.secure_auth_salt')); define('LOGGED_IN_SALT', $this->app->make('config')->get('wordpress.logged_in_salt')); define('NONCE_SALT', $this->app->make('config')->get('wordpress.nonce_salt')); + } - define('WP_DEBUG', $this->app->make('config')->get('app.debug')); - define('SAVEQUERIES', WP_DEBUG); - define('WP_DEBUG_DISPLAY', WP_DEBUG); - define('SCRIPT_DEBUG', WP_DEBUG); - + /** + * Set constants to let Wordpress know where it is in relation to the rest + * of the site, and move the wp_content directory to something a little more "saner" + * which sort of hides the fact that we are running Wordpress behind the scenes. + */ + private function setLocationConstants() + { if (!defined('ABSPATH')) { define('ABSPATH', $this->app->basePath() . DIRECTORY_SEPARATOR . WP_PATH); } @@ -81,12 +115,6 @@ class WordpressServiceProvider extends ServiceProvider define('WP_CONTENT_DIR', $this->app->basePath() . DIRECTORY_SEPARATOR . 'public/content'); define('WP_CONTENT_URL', $this->app->make(UrlGenerator::class)->to('content')); - - if ($this->app->runningInConsole()) { - $_SERVER['SERVER_PROTOCOL'] = 'https'; - } - - require ABSPATH . 'wp-settings.php'; } /** @@ -98,8 +126,8 @@ class WordpressServiceProvider extends ServiceProvider protected function triggerHooks() { // register the user's templates - Action::hook('theme_page_templates', function ($page_templates) { - return array_merge($page_templates, config('templates')); + Action::hook('theme_page_templates', function ($pageTemplates) { + return array_merge($pageTemplates, config('templates')); }); $this->registerPostTypes(); diff --git a/src/Support/Wordpress.php b/src/Support/Wordpress.php index db14603..776b40c 100644 --- a/src/Support/Wordpress.php +++ b/src/Support/Wordpress.php @@ -12,6 +12,7 @@ class Wordpress /** * Get the current Wordpress query. * + * @SuppressWarnings(PHPMD.CamelCaseVariableName) * @return \WP_Query */ public static function query() -- libgit2 1.7.2