Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/php/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
use Code_Snippets\UnifiedSnippets\REST\Scan_REST_Controller;
use Code_Snippets\UnifiedSnippets\Scanners\Additional_CSS_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Functions_Php_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Header_Footer_Code_Manager_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Htaccess_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Insert_Headers_And_Footers_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Insert_PHP_Code_Snippet_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Mu_Plugins_Scanner;
use Code_Snippets\UnifiedSnippets\Scanners\Wp_Config_Scanner;

Expand Down Expand Up @@ -179,6 +182,9 @@ private function init_unified_snippets(): void {
$this->unified_snippets->register( new Htaccess_Scanner() );
$this->unified_snippets->register( new Wp_Config_Scanner() );
$this->unified_snippets->register( new Mu_Plugins_Scanner() );
$this->unified_snippets->register( new Insert_Headers_And_Footers_Scanner() );
$this->unified_snippets->register( new Header_Footer_Code_Manager_Scanner() );
$this->unified_snippets->register( new Insert_PHP_Code_Snippet_Scanner() );

new Scan_REST_Controller( $this->unified_snippets, $this->unified_snippets_store );
}
Expand Down
109 changes: 109 additions & 0 deletions src/php/UnifiedSnippets/Adapters/DB_Scanner_Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Code_Snippets\UnifiedSnippets\Adapters;

use Code_Snippets\REST_API\Import\Plugins\Plugin_Importer;
use Code_Snippets\UnifiedSnippets\Model\Discovered_Snippet;
use Code_Snippets\UnifiedSnippets\Scanner_Base;

/**
* Adapts an existing {@see Plugin_Importer} into a Unified Snippets scanner.
*
* @package Code_Snippets
*/
abstract class DB_Scanner_Adapter extends Scanner_Base {

protected Plugin_Importer $importer;

public function __construct( ?Plugin_Importer $importer = null ) {
$this->importer = $importer ?? $this->create_importer();
}

abstract protected function create_importer(): Plugin_Importer;

abstract protected function get_table_name(): string;

/**
* Map a single raw row from the importer into Discovered_Snippet field overrides.
*
* Return null to skip the row (unsupported type, missing code, etc.).
*
* @param array<string, mixed> $row Raw row cast to associative array.
*
* @return array<string, mixed>|null
*/
abstract protected function map_row( array $row ): ?array;

public function is_available(): bool {
return (bool) call_user_func( [ get_class( $this->importer ), 'is_active' ] );
}

public function scan(): array {
if ( ! $this->is_available() ) {
return [];
}

$snippets = [];

foreach ( $this->importer->get_data() as $row ) {
$row_array = is_array( $row ) ? $row : (array) $row;
$fields = $this->map_row( $row_array );

if ( null === $fields ) {
continue;
}

$snippets[] = $this->build_snippet( $this->with_defaults( $fields ) );
}

return $snippets;
}

private function with_defaults( array $fields ): array {
return array_merge(
[
'source_type' => 'plugin',
'source_name' => $this->get_label(),
'line_start' => 0,
'line_end' => 0,
],
$fields
);
}

/**
* Build a synthetic URI identifying a row in the source plugin's table.
*
* @param int|string $id Row identifier.
*
* @return string e.g. 'db://hfcm_scripts/42'.
*/
protected function build_source_path( $id ): string {
return 'db://' . $this->get_table_name() . '/' . $id;
}

/**
* Derive a {@see Discovered_Snippet} `type` from the source plugin's code-type value.
*
* @param string $code_type Source-plugin code type, e.g. 'html', 'universal'.
*
* @return string
*/
protected function derive_type( string $code_type ): string {
switch ( strtolower( $code_type ) ) {
case 'css':
return 'css';
case 'js':
case 'javascript':
return 'js';
case 'html':
case 'universal':
return 'html';
case 'php':
case '':
return 'php';
default:
return 'mixed';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Code_Snippets\UnifiedSnippets\Scanners;

use Code_Snippets\REST_API\Import\Plugins\Header_Footer_Code_Manager_Plugin_Importer;
use Code_Snippets\REST_API\Import\Plugins\Plugin_Importer;
use Code_Snippets\UnifiedSnippets\Adapters\DB_Scanner_Adapter;

/**
* Scanner for Header Footer Code Manager (HFCM).
*
* Reuses {@see Header_Footer_Code_Manager_Plugin_Importer} to read rows from the
* `{prefix}hfcm_scripts` table and emits one {@see \Code_Snippets\UnifiedSnippets\Model\Discovered_Snippet}
* per row.
*
* @package Code_Snippets
*/
class Header_Footer_Code_Manager_Scanner extends DB_Scanner_Adapter {

public function get_id(): string {
return 'hfcm';
}

public function get_label(): string {
return __( 'Header Footer Code Manager', 'code-snippets' );
}

protected function create_importer(): Plugin_Importer {
return new Header_Footer_Code_Manager_Plugin_Importer();
}

protected function get_table_name(): string {
return 'hfcm_scripts';
}

protected function map_row( array $row ): ?array {
$code = (string) ( $row['snippet'] ?? '' );

if ( '' === trim( $code ) ) {
return null;
}

$id = (int) ( $row['script_id'] ?? 0 );
$title = (string) ( $row['name'] ?? '' );

return [
'name' => '' !== $title ? $title : sprintf( 'HFCM #%d', $id ),
'code' => $code,
'type' => $this->derive_type( (string) ( $row['snippet_type'] ?? '' ) ),
'source_path' => $this->build_source_path( $id ),
'is_active' => 'active' === ( $row['status'] ?? '' ),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Code_Snippets\UnifiedSnippets\Scanners;

use Code_Snippets\REST_API\Import\Plugins\Insert_Headers_And_Footers_Plugin_Importer;
use Code_Snippets\REST_API\Import\Plugins\Plugin_Importer;
use Code_Snippets\UnifiedSnippets\Adapters\DB_Scanner_Adapter;

/**
* Scanner for WPCode (formerly Insert Headers and Footers).
*
* Reuses {@see Insert_Headers_And_Footers_Plugin_Importer} to read snippets stored
* as `wpcode` custom posts and emits one {@see \Code_Snippets\UnifiedSnippets\Model\Discovered_Snippet}
* per supported row.
*
* @package Code_Snippets
*/
class Insert_Headers_And_Footers_Scanner extends DB_Scanner_Adapter {

private const SUPPORTED_CODE_TYPES = [ 'php', 'css', 'js', 'html', 'universal' ];

public function get_id(): string {
return 'wpcode';
}

public function get_label(): string {
return __( 'WPCode (Insert Headers and Footers)', 'code-snippets' );
}

protected function create_importer(): Plugin_Importer {
return new Insert_Headers_And_Footers_Plugin_Importer();
}

protected function get_table_name(): string {
return 'wpcode_snippets';
}

protected function map_row( array $row ): ?array {
$code_type = (string) ( $row['code_type'] ?? '' );

if ( ! in_array( $code_type, self::SUPPORTED_CODE_TYPES, true ) ) {
return null;
}

$code = (string) ( $row['code'] ?? '' );

if ( '' === trim( $code ) ) {
return null;
}

$id = $row['table_data']['id'] ?? ( $row['id'] ?? 0 );
$title = (string) ( $row['table_data']['title'] ?? ( $row['title'] ?? '' ) );

return [
'name' => '' !== $title ? $title : sprintf( 'WPCode #%d', (int) $id ),
'code' => $code,
'type' => $this->derive_type( $code_type ),
'source_path' => $this->build_source_path( (int) $id ),
'is_active' => true,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Code_Snippets\UnifiedSnippets\Scanners;

use Code_Snippets\REST_API\Import\Plugins\Insert_PHP_Code_Snippet_Plugin_Importer;
use Code_Snippets\REST_API\Import\Plugins\Plugin_Importer;
use Code_Snippets\UnifiedSnippets\Adapters\DB_Scanner_Adapter;

/**
* Scanner for 'Insert PHP Code Snippet'.
*
* Reuses {@see Insert_PHP_Code_Snippet_Plugin_Importer} to read rows from the
* `{prefix}xyz_ips_short_code` table and emits one
* {@see \Code_Snippets\UnifiedSnippets\Model\Discovered_Snippet} per row.
*
* @package Code_Snippets
*/
class Insert_PHP_Code_Snippet_Scanner extends DB_Scanner_Adapter {

public function get_id(): string {
return 'insert-php-code-snippet';
}

public function get_label(): string {
return __( 'Insert PHP Code Snippet', 'code-snippets' );
}

public function get_risk_level(): string {
return 'high';
}

protected function create_importer(): Plugin_Importer {
return new Insert_PHP_Code_Snippet_Plugin_Importer();
}

protected function get_table_name(): string {
return 'xyz_ips_short_code';
}

protected function map_row( array $row ): ?array {
$code = (string) ( $row['content'] ?? '' );

if ( '' === trim( $code ) ) {
return null;
}

$id = (int) ( $row['id'] ?? 0 );
$title = (string) ( $row['title'] ?? '' );

return [
'name' => '' !== $title ? $title : sprintf( 'Insert PHP #%d', $id ),
'code' => $code,
'type' => 'php',
'source_path' => $this->build_source_path( $id ),
'is_active' => 1 === (int) ( $row['status'] ?? 0 ),
];
}
}
16 changes: 16 additions & 0 deletions tests/phpunit/fakes/Fake_Hfcm_Importer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Code_Snippets\Tests;

use Code_Snippets\REST_API\Import\Plugins\Header_Footer_Code_Manager_Plugin_Importer;

class Fake_Hfcm_Importer extends Header_Footer_Code_Manager_Plugin_Importer {

public static function is_active(): bool {
return true;
}

public function get_data( array $ids_to_import = [] ): array {
return $this->rows;
}
}
18 changes: 18 additions & 0 deletions tests/phpunit/fakes/Fake_Ihaf_Importer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Code_Snippets\Tests;

use Code_Snippets\REST_API\Import\Plugins\Insert_Headers_And_Footers_Plugin_Importer;

class Fake_Ihaf_Importer extends Insert_Headers_And_Footers_Plugin_Importer {

public array $rows = [];

public static function is_active(): bool {
return true;
}

public function get_data( array $ids_to_import = [] ): array {
return $this->rows;
}
}
18 changes: 18 additions & 0 deletions tests/phpunit/fakes/Fake_Ipcs_Importer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Code_Snippets\Tests;

use Code_Snippets\REST_API\Import\Plugins\Insert_PHP_Code_Snippet_Plugin_Importer;

class Fake_Ipcs_Importer extends Insert_PHP_Code_Snippet_Plugin_Importer {

public array $rows = [];

public static function is_active(): bool {
return true;
}

public function get_data( array $ids_to_import = [] ): array {
return $this->rows;
}
}
Loading
Loading