Skip to content

Commit

Permalink
Rename plugin name
Browse files Browse the repository at this point in the history
  • Loading branch information
kofimokome committed Jan 17, 2025
1 parent 8a594d7 commit e2f9ac8
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MIGRATIONS_DIR='/migrations'
MODELS_DIR='/models'

# If you want to use the WordPress Tools command line script, please set the path the the wordpress-tools directory
WORDPRESSTOOLS_DIR='/lib/wordpress_tools'
WPTOOLS_DIR='/lib/wordpress_tools'

# To avoid conflicts with other plugins, please set a unique namespace for your plugin. Your name space should be one word and should not contain any special characters
NAMESPACE=''
12 changes: 6 additions & 6 deletions KMBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ public function __construct( bool $is_update = false ) {
*/
public static function addColumn( string $table, string $field, string $type, string $default = '' ) {
global $wpdb;
$query = $wpdb->prepare( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '%s' AND column_name = '%s'", [

$results = $wpdb->get_results( $wpdb->prepare( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '%s' AND column_name = '%s'", [
$table,
$field
] );
$results = $wpdb->get_results( $query );
] ) );
if ( empty( $results ) ) {
$default_string = is_numeric( $default ) ? "DEFAULT $default" : "DEFAULT " . "'$default'";
$query = $wpdb->prepare( "ALTER TABLE %s ADD %s %s NOT NULL %s", [

$wpdb->query( $wpdb->prepare( "ALTER TABLE %s ADD %s %s NOT NULL %s", [
$table,
$field,
$type,
$default_string
] );
$wpdb->query( $query );
] ) );
}
}

Expand Down
11 changes: 6 additions & 5 deletions KMBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function first(): ?KMModel {
* example
* [Job::tableName().'.*',Currency::tableName().'.code',JobType::tableName().'.name AS job_type_name '],
*/
public function select( $fields = [] ): KMBuilder {
public function select( $fields = [] ): KMBuilder {
if ( is_array( $fields ) ) {
$this->selects = $fields;
} else {
Expand Down Expand Up @@ -447,13 +447,14 @@ public function save(): bool {
$table_name = $this->table_name;
if ( $this->model->id == 0 ) { // we are creating
if ( $this->model->hasTimeStamps() ) {
$fields['created_at'] = date( "Y-m-d H:i" );
$fields['updated_at'] = date( "Y-m-d H:i" );
$fields['created_at'] = gmdate( "Y-m-d H:i" );
$fields['updated_at'] = gmdate( "Y-m-d H:i" );
}
$result = $wpdb->insert( $table_name, $fields );
$fields['id'] = NULL;
$result = $wpdb->insert( $table_name, $fields );
} else { // we are updating
if ( $this->model->hasTimeStamps() ) {
$fields['updated_at'] = date( "Y-m-d H:i" );
$fields['updated_at'] = gmdate( "Y-m-d H:i" );
}
unset( $fields['id'] );
$result = $wpdb->update( $table_name, $fields, [ 'id' => $this->model->id ] );
Expand Down
4 changes: 2 additions & 2 deletions KMEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class KMEnv {
private $env = [];
private $envFile = '.env';
private $envFile = 'config.env';
private $plugin_path;
private $plugin_basename;

Expand All @@ -31,7 +31,7 @@ public function getEnv(): array {
$plugin_basename = $chars[0];

if ( $this->envFile == '' ) {
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_basename . '/.env';
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_basename . '/config.env';
} else {
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_basename . '/' . trim( $this->envFile );
}
Expand Down
28 changes: 13 additions & 15 deletions KMMigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public function update( array $migration, KMMigration $object ): void {
$object->up( $blueprint );
$columns = $blueprint->getColumns();
foreach ( $columns as $column ) {
$query = $wpdb->prepare( "ALTER TABLE `%1s` %1s", [ $object->getTableName(), $column->toString() ] );
if ( ! $wpdb->query( $query ) ) {
if ( ! $wpdb->query( $wpdb->prepare( "ALTER TABLE `%1s` %1s", [
$object->getTableName(),
$column->toString()
] ) ) ) {
throw new Exception( $wpdb->last_error );
}
}
Expand All @@ -108,11 +110,9 @@ public function runMigration( array $migration ) {
$migration_object->up( $blueprint );
$column_string = $blueprint->toString();

$query = $wpdb->prepare( "CREATE TABLE IF NOT EXISTS `%1s` ( $column_string )", [
if ( ! $wpdb->query( $wpdb->prepare( "CREATE TABLE IF NOT EXISTS `%1s` ( $column_string )", [
$migration_object->getTableName(),
] );

if ( ! $wpdb->query( $query ) ) {
] ) ) ) {
throw new Exception( $wpdb->last_error );
}
}
Expand Down Expand Up @@ -161,18 +161,16 @@ public function dropMigration( array $migration, bool $delete_file = true ) {
$migration_object->down( $blueprint );

if ( $blueprint->isDropTable() ) {
$query = $wpdb->prepare( "DROP TABLE IF EXISTS %1s", [ $migration_object->getTableName() ] );
if ( ! $wpdb->query( $query ) ) {
if ( ! $wpdb->query( $wpdb->prepare( "DROP TABLE IF EXISTS %1s", [ $migration_object->getTableName() ] ) ) ) {
throw new Exception( $wpdb->last_error );
}
} else {
$columns = $blueprint->getColumns();
foreach ( $columns as $column ) {
$query = $wpdb->prepare( "ALTER TABLE `%1s` %1s", [
if ( ! $wpdb->query( $wpdb->prepare( "ALTER TABLE `%1s` %1s", [
$migration_object->getTableName(),
$column->toString()
] );
if ( ! $wpdb->query( $query ) ) {
] ) ) ) {
throw new Exception( $wpdb->last_error );
}
}
Expand All @@ -183,7 +181,7 @@ public function dropMigration( array $migration, bool $delete_file = true ) {
}

if ( $delete_file ) {
unlink( $migration['path'] );
wp_delete_file( $migration['path'] );
}
}

Expand Down Expand Up @@ -261,10 +259,10 @@ private function createMigrationsTable() {
$blueprint->integer( 'batch' );
$blueprint->timestamps();
$additions = $blueprint->toString();
$query = $wpdb->prepare( "CREATE TABLE IF NOT EXISTS `%1s` ( $additions )", [

if ( ! $wpdb->query( $wpdb->prepare( "CREATE TABLE IF NOT EXISTS `%1s` ( $additions )", [
$table_name,
] );
if ( ! $wpdb->query( $query ) ) {
] ) ) ) {
throw new Exception( $wpdb->last_error );
}
}
Expand Down
2 changes: 1 addition & 1 deletion KMRouteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function route( string $name, array $params = [] ): string {

return site_url( $route );
} else {
return 'bro';
return '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion KMSubMenuPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct( $data ) {
* @since 1.0.0
*/
public function show_tabs() {
$current_tab = isset( $_GET['tab'] ) ? esc_html( $_GET['tab'] ) : null;
$current_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : null;
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
Expand Down
4 changes: 2 additions & 2 deletions KMValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private function isFile( $data ): bool {
* Rules: required, bool, int, numeric, pdf
* @author kofimokome
*/
public function validateData( $rules = [], $data = [] ): bool {
public function validateData( $rules = [], $data = [] ): bool|array {

if ( sizeof( $rules ) == 0 ) {
$rules = $this->rules;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function validateData( $rules = [], $data = [] ): bool {
$this->rules = [];
$this->data = [];

return true;
return $data;
}
}
}
6 changes: 3 additions & 3 deletions WordPressTools.php → WPTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
require_once 'lib/plural/Plural.php';


if ( ! class_exists( 'WordPressTools' ) ) {
class WordPressTools {
if ( ! class_exists('WPTools') ) {
class WPTools {
public $env;
public $route_manager;
public $migration_manager;
Expand All @@ -43,7 +43,7 @@ public function __construct( string $context ) {
/**
* @author kofimokome
*/
public static function getInstance( string $context ): WordPressTools {
public static function getInstance( string $context ): WPTools {
$plugin_basename = plugin_basename( $context );
$plugin = explode( '/', $plugin_basename )[0];

Expand Down
22 changes: 11 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WordPress Tools
# WP Tools

These are a set of helper classes that will make WordPress plugin development easier

Expand All @@ -13,31 +13,31 @@ to find out how to use these tools.
.env
3. If you would like to use the `wptools` command line interface:
1. Copy/move the `wptools` file to the root of your plugin
2. In your `.env` file, make sure to set the `WORDPRESSTOOLS_DIR` and `NAMESPACE` variables.
2. In your `.env` file, make sure to set the `WPTOOLS_DIR` and `NAMESPACE` variables.
3. cd to the root of your plugin and run `php wptools` for the list of commands.

```bash
$ php wptools
```

4. Import the `WordPressTools.php` file in your plugin main file and instantiate the class.
4. Import the `WPTools.php` file in your plugin main file and instantiate the class.

```php
$wordpress_tools = new WordPressTools( __FILE__ );
$wordpress_tools = new WPTools( __FILE__ );
```

## 2. How to get the WordPressTools instance
## 2. How to get the WPTools instance

To get the instance of the WordPressTools class from any file in your plugin, you can use the `get_instance()` method. <br/>
NOTE: An exception is thrown if you have not created an instance of the WordPressTools class.
To get the instance of the WPTools class from any file in your plugin, you can use the `get_instance()` method. <br/>
NOTE: An exception is thrown if you have not created an instance of the WPTools class.

```php
$wordpress_tools = WordPressTools::get_instance(__FILE__);
$wordpress_tools = WPTools::get_instance(__FILE__);
```

## 3. Managing Migrations

The WordPressTools provides an easy-to-use interface for managing database migrations. <br/>
The WPTools provides an easy-to-use interface for managing database migrations. <br/>
Before you can use the migrations, you need to make sure you have done the following:

1. Make sure you have created a folder to store the migrations in your plugin
Expand Down Expand Up @@ -91,7 +91,7 @@ class CreateMessagesTable extends KMMigration {
To run migrations, you need to add the code below to your plugin main file.

```php
$wordpress_tools = new WordPressTools( __FILE__ );
$wordpress_tools = new WPTools( __FILE__ );
$wordpress_tools->migration_manager->runMigrations();

```
Expand Down Expand Up @@ -143,7 +143,7 @@ class AddSlugToQuestionsTable extends KMMigration {
## 4. Models

You can use models to query the database, without writing a single SQL command. <br/>
The WordPressTools provides an easy-to-use interface for query the database. Before you can use the model, you need to
The WPTools provides an easy-to-use interface for query the database. Before you can use the model, you need to
make sure you have done the following:

1. Make sure you have created a folder to store the models in your plugin
Expand Down
14 changes: 9 additions & 5 deletions scripts/migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function make_migration( string $migration_name, $table_name = '', $is_update =
global $env;
global $plugin_root_dir;
if ( ! isset( $env['MIGRATIONS_DIR'] ) ) {
echo("\033[31mERROR!: The migrations directory is not set. Please set it in the .env file\033[39m \n");
echo( "\033[31mERROR!: The migrations directory is not set. Please set it in the .env file\033[39m \n" );
exit();
}
// check if the migrations folder exists
$migrations_dir = $plugin_root_dir . $env['MIGRATIONS_DIR'];

if ( ! is_dir( $migrations_dir ) ) {
echo("\033[31mERROR!: The migrations directory {$env['MIGRATIONS_DIR']} does not exist. Please create it first\033[39m \n");
echo( "\033[31mERROR!: The migrations directory {$env['MIGRATIONS_DIR']} does not exist. Please create it first\033[39m \n" );
exit();
}
$migrations_dir = rtrim( $migrations_dir, '/' );
Expand All @@ -29,7 +29,7 @@ function make_migration( string $migration_name, $table_name = '', $is_update =
$migration_name = preg_replace( '/[^A-Za-z0-9\-]/', '_', $migration_name );

// create a file in the migration folder with the syntax YYYY_MM_DD_HHMMSS_migration_name.php
$file_name = date( 'Y_m_d_His' ) . '_' . $migration_name . '.php';
$file_name = gmdate( 'Y_m_d_His' ) . '_' . $migration_name . '.php';
$file_path = $migrations_dir . '/' . $file_name;
$file = fopen( $file_path, 'w' );

Expand Down Expand Up @@ -82,7 +82,11 @@ public function down( KMBlueprint $blueprint ) {
';
}
fwrite( $file, $migration_template );
if ( fwrite( $file, $migration_template ) ) {
echo( "\033[32m\033[1mMigration $file_path created successfully! \033[0m \033[39m \n" );
} else {
echo( "\033[31mERROR!: Could not create the migration file. Please check your file permissions.\033[39m \n" );
};

fclose( $file );
echo( "\033[32m\033[1mMigration $file_path created successfully! \033[0m \033[39m \n" );
}
6 changes: 3 additions & 3 deletions wptools
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ if ( is_file( '.env' ) ) {
}
}

if ( ! isset( $env['WORDPRESSTOOLS_DIR'] ) ) {
echo( "\033[31mERROR!: Please set the WORDPRESSTOOLS_DIR in the .env file\n" );
if ( ! isset( $env['WPTOOLS_DIR'] ) ) {
echo( "\033[31mERROR!: Please set the WPTOOLS_DIR in the .env file\n" );
exit();
}
$wp_tools_dir = ltrim( $env['WORDPRESSTOOLS_DIR'], '/' );
$wp_tools_dir = ltrim( $env['WPTOOLS_DIR'], '/' );
if ( ! is_dir( $wp_tools_dir ) ) {
echo( "\033[31mERROR!: $wp_tools_dir is not a valid directory\n" );
exit();
Expand Down

0 comments on commit e2f9ac8

Please sign in to comment.