Giter Club home page Giter Club logo

wp-theme-migrator's Introduction

WP Theme Migrator

Coding Standards Testing Suite

A library to support agile, incremental theme migrations in WordPress.

Background

This library facilitates an incremental approach to migrating a WordPress site to a new theme.

The conventional strategy for re-theming a site is to build an entire theme, then activate the new theme on the production environment when it's complete. This library enables you to use a Strangler Fig pattern to move gradually from an old theme to a new theme, with both themes installed on the production environment.

The parameters of the migration strategy are passed via callbacks to the Migrator during initialization. The Migrator parses the current request early and passes the query vars to each callback so you can base the migration strategy on post type, taxonomy, publish date, language, post meta or any public query var that is added before the Migrator is run.

Furthermore, you can define your migration strategy on more than just what's available in the query. The Migrator is agnostic – it only needs to know whether the current request should be migrated to the new theme. So your strategy can draw on globals, constants, an API integration, the day of the week – or any value that's available when the Migrator is run – to determine migratability.

With thoughtfully structured callbacks, you can even A/B test redesigned pages during development. Individual content types can be built, tested, and released before the theme is complete, bringing a truly iterative cycle to your workflow.

Releases

This package follows semantic versioning conventions.

Roadmap

This package is in a pre-release status. Milestones to be completed before the first release include:

  • Adding feature testing.

Install

Requires Composer and PHP >= 8.0.

Use

Install a new valid theme. To be valid, it must exist in the /wp-content/themes/ directory, be compatible with the local WordPress and PHP versions, and include at least a style.css file. Do not activate the new theme.

Install this package via Composer.

composer require alleyinteractive/wp-theme-migrator

Ensure that the Composer autoloader is loaded into your project.

require_once __DIR__ . '/vendor/autoload.php';

Initialize the Migrator in your project. The Migrator performs its magic on the setup_theme hook so it must be initialized before that. Here, the Migrator object is created on the plugins_loaded hook:

/**
* Initialize WP Theme Migrator early.
*/
function init_migrator() {
	try {
		$migrator = new \Alley\WP\Theme_Migrator\Migrator();
		$migrator->init();
	} catch( Exception  $e ) {
		// Do something. The Migrator will throw an Exception when it's
		// initialized with an invalid theme or callback. Be sure to catch
		// the Exception to fatal errors.
	}
}
add_action( 'plugins_loaded', 'init_migrator' );

Pass the name of the new theme and a list of one or more callbacks to the Migrator through filters. A callback must return true if a given request should be handled with the new theme. If more than one callback is provided, the Migrator will call each one once until one returns true. Then, the Migrator will load the new theme and no more callbacks will be called on that request. If none of the provided callbacks return true, the old theme will be loaded.

/**
 * Pass the new theme to WP Theme Migrator.
 */
add_filter( 'wp_theme_migrator_theme', fn() => 'new-theme-slug' );

/**
* Add callbacks for WP Theme Migrator to determine migratability.
*
* @param callable[] $callbacks Array of callbacks.
* @param Migrator   $migrator Migrator instance.
*/
function filter_wp_theme_migrator_callbacks( $context, $migrator) {
	return [
		'a_callback', // This can be any callable.
		'another_callback',
	];
}
add_filter( 'wp_theme_migrator_callbacks', 'filter_wp_theme_migrator_callbacks', 10, 2 );

Define your migration strategy through your callbacks.

/**
* Callback to manage theme migration.
*
* @param array $query_vars Array of query vars for the current request.
* @return bool Whether to load the new theme.
*/
function a_callback( $query_vars ): bool {
	// Do something to decide if the current request is migratable.
}

Once you've migrated the entire site, activate your new theme, remove this package from your project, and uninstall your old theme.

From Source

To work on this project locally, first add the repository to your project's composer.json:

{
	"repositories": [
		{
			"type": "path",
			"url": "../path/to/wp-theme-migrator",
			"options": {
				"symlink": true
			}
		}
	]
}

Next, add the local development files to the require section of composer.json:

{
	"require": {
		"alleyinteractive/wp-theme-migrator": "@dev"
	}
}

Finally, update composer to use the local copy of the package:

composer update alleyinteractive/wp-theme-migrator --prefer-source

Changelog

This project keeps a changelog.

Development Process

See instructions above on installing from source. Pull requests are welcome from the community and will be considered for inclusion. Releases follow semantic versioning and are shipped on an as-needed basis.

Contributing

See our contributor guidelines for instructions on how to contribute to this open source project.

Project Structure

This is a Composer package that is published to Packagist. Classes are autoloadable using alleyinteractive/composer-wordpress-autoloader. They live in the src directory and follow standard WordPress naming conventions for classes.

Third-Party Dependencies

Dependencies are managed by Composer, and include:

  • alleyinteractive/composer-wordpress-autoloader: Used for autoloading classes that follow the standard WordPress conventions for filenames.
  • alleyinteractive/alley-coding-standards: Used for running phpcs linting.
  • mantle-framework/testkit: Used for running unit tests.
  • symfony/filesystem: Used for copying files into the WordPress test installation for testing.

Maintainers

Alley logo

Contributors

Thanks to all of the contributors to this project.

License

This project is licensed under the GNU Public License (GPL) version 2 or later.

wp-theme-migrator's People

Contributors

dependabot[bot] avatar emilyatmobtown avatar github-actions[bot] avatar renatonascalves avatar szepeviktor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

szepeviktor

wp-theme-migrator's Issues

There are 2 PHPCS configuration files

Description of the bug

--- phpcs.xml   2023-11-01 14:44:17.704303730 +0000
+++ .phpcs.xml  2023-11-01 14:44:17.704303730 +0000
@@ -9,10 +9,12 @@
   Pass some flags to PHPCS:
     p flag: Show progress of the run.
     s flag: Show sniff codes in all reports.
-    n flag: Do not print warnings.
   -->
   <arg value="ps" />

+  <!-- Enable colors in report -->
+   <arg name="colors" />
+
   <!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
   <arg name="cache" value=".phpcs/cache.json" />

@@ -22,38 +24,31 @@
   <!-- Check up to 20 files simultaneously. -->
   <arg name="parallel" value="20" />

-  <!-- Check for cross-version support for PHP 8.0 and higher. -->
-  <config name="testVersion" value="8.0-"/>
-
   <!-- Set severity to 1 to see everything that isn't effectively turned off. -->
   <arg name="severity" value="1" />

   <!-- Exclude a few directories and autogenerated files. -->
-  <exclude-pattern>tests/</exclude-pattern>
-  <exclude-pattern>build/</exclude-pattern>
   <exclude-pattern>vendor/</exclude-pattern>

-  <!-- The version set here matches the minimum version tested in buddy.yml. -->
+  <!-- The version set here matches the minimum version tested in the CI. -->
   <config name="minimum_supported_wp_version" value="6.1" />

-  <!-- Define the prefixes that can be used by the plugin -->
-  <rule ref="WordPress.NamingConventions.PrefixAllGlobals">
+  <!-- Set the text domain for i18n. -->
+  <rule ref="WordPress.WP.I18n">
     <properties>
-      <property name="prefixes" type="array">
-        <element value="alleyinteractive" />
-        <element value="WP_Theme_Migrator" />
-      </property>
+      <property name="text_domain" type="array" value="wp-theme-migrator"/>
     </properties>
   </rule>

-  <rule ref="WordPress.WP.I18n">
+  <!-- Set the prefixes for functions etc. -->
+  <rule ref="WordPress.NamingConventions.PrefixAllGlobals">
     <properties>
-      <!--
-      Verify that the text_domain is set to the desired text-domain.
-      Multiple valid text domains can be provided as a comma-delimited list.
-       -->
-      <property name="text_domain" type="array" value="wp-theme-migrator" />
+      <property name="prefixes" type="array" value="wp_theme_migrator,Alley\WP\Theme_Migrator"/>
     </properties>
   </rule>

+  <!-- Exclude test classes from file naming rules. -->
+  <rule ref="WordPress.Files.FileName.InvalidClassFileName">
+    <exclude-pattern>tests/</exclude-pattern>
+  </rule>
 </ruleset>

Steps To Reproduce

ls .phpcs.xml phpcs.xml

Additional Information

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.