Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce autoloader #358

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Autoloader for WordPress.
*/

namespace StringLocator;

use function spl_autoload_register;

spl_autoload_register(
/**
* Closure for the autoloader.
*
* @param class-string $class_name The fully-qualified class name.
* @return void
*/
static function ( $class_name ) {
/**
* __NAMESPACE__ could be an empty string.
*
* @phpstan-ignore-next-line
*/
$project_namespace = '' === __NAMESPACE__ ? '' : __NAMESPACE__ . '\\';
$length = strlen( $project_namespace );

// Class is not in our namespace.
if ( 0 !== strncmp( $project_namespace, $class_name, $length ) ) {
return;
}

// E.g. Model\Item.
$relative_class_name = substr( $class_name, $length );
// Class file names should be based on the class name with "class-" prepended
// and the underscores in the class name replaced with hyphens.
// E.g. model/class-item.php.
$name_parts = explode( '\\', strtolower( str_replace( '_', '-', $relative_class_name ) ) );
$last_part = array_pop( $name_parts );

$file = sprintf(
'%1$s/includes%2$s/class-%3$s.php',
__DIR__,
array() === $name_parts ? '' : '/' . implode( '/', $name_parts ),
$last_part
);

if ( ! is_file( $file ) ) {
return;
}

require $file;
}
);
16 changes: 0 additions & 16 deletions includes/Extension/SQL/sql.php

This file was deleted.

16 changes: 0 additions & 16 deletions includes/Extension/SearchReplace/search-replace.php

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace StringLocator\Extension\SearchReplace\Replace;

use StringLocator\Extension\SQL\Helpers;
use StringLocator\Extension\SQL\Search;
use StringLocator\String_Locator;
use function StringLocator\Extension\SQL\validate_sql_fields;

/**
* SQL class.
Expand Down Expand Up @@ -60,13 +60,13 @@ public function __construct( $primary_column, $primary_key, $primary_type, $tabl
* @return bool
*/
public function validate() {
if ( ! validate_sql_fields( $this->primary_column ) ) {
if ( ! Helpers::validate_sql_fields( $this->primary_column ) ) {
return false;
}
if ( ! validate_sql_fields( $this->table_name ) ) {
if ( ! Helpers::validate_sql_fields( $this->table_name ) ) {
return false;
}
if ( ! validate_sql_fields( $this->column_name ) ) {
if ( ! Helpers::validate_sql_fields( $this->column_name ) ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace StringLocator\Extension\SQL;

use StringLocator\Extension\SQL\Helpers;
use StringLocator\String_Locator;

/**
Expand Down Expand Up @@ -74,12 +75,12 @@ public function sql_edit_page( $include_path ) {
}

// Validate the table name.
if ( ! isset( $_GET['sql-table'] ) || ! validate_sql_fields( $_GET['sql-table'] ) ) {
if ( ! isset( $_GET['sql-table'] ) || ! Helpers::validate_sql_fields( $_GET['sql-table'] ) ) {
return $include_path;
}

// Validate the primary column
if ( ! isset( $_GET['sql-primary-column'] ) || ! validate_sql_fields( $_GET['sql-primary-column'] ) ) {
if ( ! isset( $_GET['sql-primary-column'] ) || ! Helpers::validate_sql_fields( $_GET['sql-primary-column'] ) ) {
return $include_path;
}

Expand Down
15 changes: 15 additions & 0 deletions includes/extension/sql/class-helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace StringLocator\Extension\SQL;

/**
* Helpers class.
*/
class Helpers {

public static function validate_sql_fields( $field ) {
return preg_match( '/^[0-9a-zA-Z_]+$/s', $field );
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace StringLocator\Extension\SQL;

use StringLocator\Base\Search as SearchBase;
use StringLocator\Extension\SQL\Helpers;
use StringLocator\String_Locator;

/**
Expand Down Expand Up @@ -214,7 +215,7 @@ public function run( $filenum ) {

$identifier_name = 'Tables_in_' . DB_NAME;

if ( ! validate_sql_fields( $identifier_name ) ) {
if ( ! Helpers::validate_sql_fields( $identifier_name ) ) {
wp_send_json_error(
array(
'continue' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,3 @@ public function run( $content ) {
return true;
}
}

new Serialized_Data();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 4 additions & 32 deletions string-locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

namespace StringLocator;

use StringLocator\Extensions\SQL\Tests\Serialized_Data;

if ( ! defined( 'ABSPATH' ) ) {
die();
}
Expand All @@ -36,41 +38,11 @@
define( 'STRING_LOCATOR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'STRING_LOCATOR_PLUGIN_FILE', __FILE__ );

/**
* Base classes that other classes may extend.
*/
require_once __DIR__ . '/includes/Base/class-search.php';
require_once __DIR__ . '/includes/Base/class-rest.php';

/**
* Search handlers
*/
require_once __DIR__ . '/includes/Extension/SQL/sql.php';
require_once __DIR__ . '/includes/Extension/SearchReplace/search-replace.php';

/**
* Plugin test runners
*/
require_once __DIR__ . '/includes/Tests/class-loopback.php';
require_once __DIR__ . '/includes/Tests/class-smart-scan.php';
require __DIR__ . '/autoload.php';

/**
* Plugin action classes.
*/
require_once __DIR__ . '/includes/class-save.php';
require_once __DIR__ . '/includes/class-search.php';
require_once __DIR__ . '/includes/class-directory-iterator.php';

/**
* Prepare REST endpoints.
*/
require_once __DIR__ . '/includes/REST/class-save.php';
require_once __DIR__ . '/includes/REST/class-clean.php';
require_once __DIR__ . '/includes/REST/class-search.php';
require_once __DIR__ . '/includes/REST/class-directory-structure.php';
new Serialized_Data();

/**
* Instantiate the plugin
*/
require_once __DIR__ . '/includes/class-string-locator.php';
new String_Locator();