diff --git a/components/Migrate-PHP/Migrate-PHP.php b/components/Migrate-PHP/Migrate-PHP.php
new file mode 100644
index 0000000000..7ddc88f6a6
--- /dev/null
+++ b/components/Migrate-PHP/Migrate-PHP.php
@@ -0,0 +1,304 @@
+More information about Pod Page template hierarchy | More information about Pod Template hierarchy
+ *
+ * Category: Migration
+ *
+ * Version: 1.0
+ *
+ * Plugin: pods-migrate-php/pods-migrate-php.php
+ *
+ * @package Pods\Components
+ * @subpackage Migrate-PHP
+ */
+
+use Pods\Whatsit;
+use Pods\Whatsit\Page;
+use Pods\Whatsit\Template;
+
+if ( class_exists( 'Pods_Migrate_PHP' ) ) {
+ return;
+}
+
+/**
+ * Class Pods_Migrate_PHP
+ */
+class Pods_Migrate_PHP extends PodsComponent {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function init() {
+ // Nothing to do here.
+ }
+
+ /**
+ * Enqueue styles
+ *
+ * @since 2.0.0
+ */
+ public function admin_assets() {
+ wp_enqueue_style( 'pods-wizard' );
+ }
+
+ /**
+ * Show the Admin
+ *
+ * @param $options
+ * @param $component
+ */
+ public function admin( $options, $component ) {
+ $method = 'migrate';
+
+ $pod_templates = [];
+ $pod_pages = [];
+
+ $api = pods_api();
+
+ if ( class_exists( 'Pods_Templates' ) ) {
+ $pod_templates = array_filter(
+ $api->load_templates(),
+ static function( $object ) {
+ if ( ! $object instanceof Whatsit ) {
+ return false;
+ }
+
+ return ! empty( $object->get_id() ) && false !== strpos( $object->get_description(), '' );
+ }
+ );
+ }
+
+ if ( class_exists( 'Pods_Pages' ) ) {
+ $pod_pages = array_filter(
+ $api->load_pages(),
+ static function( $object ) {
+ if ( ! $object instanceof Whatsit ) {
+ return false;
+ }
+
+ return (
+ ! empty( $object->get_id() )
+ && (
+ false !== strpos( $object->get_description(), '' )
+ || false !== strpos( (string) $object->get_arg( 'precode' ), '' )
+ )
+ );
+ }
+ );
+ }
+
+ // ajax_migrate
+ pods_view( __DIR__ . '/ui/wizard.php', compact( array_keys( get_defined_vars() ) ) );
+ }
+
+ /**
+ * Handle the Migration AJAX
+ *
+ * @param $params
+ */
+ public function ajax_migrate( $params ) {
+ WP_Filesystem();
+
+ $pod_templates = [];
+ $pod_pages = [];
+
+ if ( isset( $params->templates ) && ! empty( $params->templates ) ) {
+ foreach ( $params->templates as $object_id => $checked ) {
+ if ( true === (boolean) $checked ) {
+ $pod_templates[] = $object_id;
+ }
+ }
+ }
+
+ if ( isset( $params->pages ) && ! empty( $params->pages ) ) {
+ foreach ( $params->pages as $object_id => $checked ) {
+ if ( true === (boolean) $checked ) {
+ $pod_pages[] = $object_id;
+ }
+ }
+ }
+
+ $pod_templates_file_paths = [];
+ $pod_pages_file_paths = [];
+
+ foreach ( $pod_templates as $object_id ) {
+ $pod_templates_file_paths[] = $this->migrate_template( $object_id );
+ }
+
+ foreach ( $pod_pages as $object_id ) {
+ $pod_pages_file_paths[] = $this->migrate_page( $object_id );
+ }
+
+ $content = '
' . "\n";
+
+ $content .= '
' . esc_html__( 'Migration Complete! The following paths were saved:', 'pods' ) . '
' . "\n";
+
+ if ( ! empty( $pod_templates_file_paths ) ) {
+ $content .= '
' . esc_html__( 'Pod Templates saved', 'pods' ) . '
' . "\n";
+ $content .= '
' . "\n";
+
+ foreach ( $pod_templates_file_paths as $file_path ) {
+ $content .= '- ' . esc_html( $file_path ) . '
' . "\n";
+ }
+
+ $content .= '
' . "\n";
+ }
+
+ if ( ! empty( $pod_pages_file_paths ) ) {
+ $content .= '
' . esc_html__( 'Pod Pages saved', 'pods' ) . '
' . "\n";
+ $content .= '
' . "\n";
+
+ foreach ( $pod_pages_file_paths as $file_path ) {
+ $content .= '- ' . esc_html( $file_path ) . '
' . "\n";
+ }
+
+ $content .= '
' . "\n";
+ }
+
+ return $content;
+ }
+
+ private function setup_file_path( $file_path ) {
+ /**
+ * @var $wp_filesystem WP_Filesystem_Base
+ */
+ global $wp_filesystem;
+
+ if ( ! $wp_filesystem->is_dir( dirname( $file_path ) ) ) {
+ $pods_path = trailingslashit( get_stylesheet_directory() ) . 'pods';
+
+ if ( ! $wp_filesystem->is_dir( $pods_path ) && ! $wp_filesystem->mkdir( $pods_path, FS_CHMOD_DIR ) ) {
+ // translators: %s is the directory path.
+ pods_error( sprintf( esc_html__( 'Unable to create the directory: %s', 'pods' ), $pods_path ) );
+ }
+
+ $grandparent_path = dirname( dirname( $file_path ) );
+
+ if ( $pods_path !== $grandparent_path && ! $wp_filesystem->is_dir( $grandparent_path ) && ! $wp_filesystem->mkdir( $grandparent_path, FS_CHMOD_DIR ) ) {
+ // translators: %s is the directory path.
+ pods_error( sprintf( esc_html__( 'Unable to create the directory: %s', 'pods' ), $grandparent_path ) );
+ }
+
+ if ( ! $wp_filesystem->mkdir( dirname( $file_path ), FS_CHMOD_DIR ) ) {
+ // translators: %s is the directory path.
+ pods_error( sprintf( esc_html__( 'Unable to create the directory: %s', 'pods' ), $file_path ) );
+ }
+ } elseif ( ! $wp_filesystem->is_writable( dirname( $file_path ) ) ) {
+ // translators: %s is the directory path.
+ pods_error( sprintf( esc_html__( 'Unable to write to the directory: %s', 'pods' ), $file_path ) );
+ }
+ }
+
+ private function migrate_template( $object_id ) {
+ /**
+ * @var $wp_filesystem WP_Filesystem_Base
+ */
+ global $wp_filesystem;
+
+ $api = pods_api();
+
+ /** @var Template $object */
+ $object = $api->load_template( [ 'id' => $object_id ] );
+
+ if ( ! $object ) {
+ // translators: %s is the object ID.
+ pods_error( sprintf( esc_html__( 'Unable to find the Pod Template by ID: %s', 'pods' ), $object_id ) );
+ }
+
+ $files = Pods_Templates::get_templates_for_pod_template( $object );
+
+ if ( count( $files ) < 2 ) {
+ // translators: %s is the file paths found.
+ pods_error( sprintf( esc_html__( 'Unable to detect the file path: %s', 'pods' ), json_encode( $files, JSON_PRETTY_PRINT ) ) );
+ }
+
+ $file_path = trailingslashit( get_stylesheet_directory() ) . array_shift( $files );
+
+ $this->setup_file_path( $file_path );
+
+ $contents = <<
get_label()}
+ */
+?>
+
+{$object->get_description()}
+PHPTEMPLATE;
+
+ if ( ! $wp_filesystem->put_contents( $file_path, $contents, FS_CHMOD_FILE ) ) {
+ // translators: %s is the file path.
+ pods_error( sprintf( esc_html__( 'Unable to write to the file: %s', 'pods' ), $file_path ) );
+ }
+
+ return str_replace( ABSPATH, '', $file_path );
+ }
+
+ private function migrate_page( $object_id ) {
+ /**
+ * @var $wp_filesystem WP_Filesystem_Base
+ */
+ global $wp_filesystem;
+
+ $api = pods_api();
+
+ /** @var Page $object */
+ $object = $api->load_page( [ 'id' => $object_id ] );
+
+ if ( ! $object ) {
+ // translators: %s is the object ID.
+ pods_error( sprintf( esc_html__( 'Unable to find the Pod Page by ID: %s', 'pods' ), $object_id ) );
+ }
+
+ $files = Pods_Pages::get_templates_for_pod_page( $object );
+
+ if ( count( $files ) < 2 ) {
+ // translators: %s is the file paths found.
+ pods_error( sprintf( esc_html__( 'Unable to detect the file path: %s', 'pods' ), json_encode( $files, JSON_PRETTY_PRINT ) ) );
+ }
+
+ $file_path = trailingslashit( get_stylesheet_directory() ) . array_shift( $files );
+
+ $this->setup_file_path( $file_path );
+
+ $precode = $object->get_arg( 'precode' );
+
+ if ( false !== strpos( $precode, '' ) && false === strpos( $precode, '?>' ) ) {
+ $precode .= "\n?>";
+ }
+
+ $contents = <<get_label()}
+ */
+?>
+
+
+{$precode}
+
+
+{$object->get_description()}
+PHPTEMPLATE;
+
+ if ( ! $wp_filesystem->put_contents( $file_path, $contents, FS_CHMOD_FILE ) ) {
+ // translators: %s is the file path.
+ pods_error( sprintf( esc_html__( 'Unable to write to the file: %s', 'pods' ), $file_path ) );
+ }
+
+ return str_replace( ABSPATH, '', $file_path );
+ }
+
+}
diff --git a/components/Migrate-PHP/ui/wizard.php b/components/Migrate-PHP/ui/wizard.php
new file mode 100644
index 0000000000..eca18cc9f0
--- /dev/null
+++ b/components/Migrate-PHP/ui/wizard.php
@@ -0,0 +1,245 @@
+
+
diff --git a/components/Pages.php b/components/Pages.php
index 842e1ccf02..41e10f77c6 100644
--- a/components/Pages.php
+++ b/components/Pages.php
@@ -1046,6 +1046,42 @@ public static function exists( $uri = null ) {
return false;
}
+ /**
+ * Convert a Page object to a Pod Page data array.
+ *
+ * @since TBD
+ *
+ * @param Page $object The Pod Page object.
+ *
+ * @return array The Pod Page data array.
+ */
+ public static function object_to_page( Page $object ): array {
+ $id = $object->get_id();
+
+ return [
+ 'id' => $object->get_name(),
+ 'uri' => $object->get_arg( 'uri' ),
+ 'code' => $object->get_description(),
+ 'phpcode' => $object->get_description(),
+ // phpcode is deprecated
+ 'precode' => get_post_meta( $id, 'precode', true ),
+ 'page_template' => get_post_meta( $id, 'page_template', true ),
+ 'title' => get_post_meta( $id, 'page_title', true ),
+ 'options' => [
+ 'admin_only' => (boolean) get_post_meta( $id, 'admin_only', true ),
+ 'restrict_role' => (boolean) get_post_meta( $id, 'restrict_role', true ),
+ 'restrict_capability' => (boolean) get_post_meta( $id, 'restrict_capability', true ),
+ 'roles_allowed' => get_post_meta( $id, 'roles_allowed', true ),
+ 'capability_allowed' => get_post_meta( $id, 'capability_allowed', true ),
+ 'restrict_redirect' => (boolean) get_post_meta( $id, 'restrict_redirect', true ),
+ 'restrict_redirect_login' => (boolean) get_post_meta( $id, 'restrict_redirect_login', true ),
+ 'restrict_redirect_url' => get_post_meta( $id, 'restrict_redirect_url', true ),
+ 'pod' => get_post_meta( $id, 'pod', true ),
+ 'pod_slug' => get_post_meta( $id, 'pod_slug', true ),
+ ],
+ ];
+ }
+
/**
* Check if a Pod Page exists
*/
@@ -1449,7 +1485,7 @@ public function template_include( $original_template ) {
if ( $template !== $located_template ) {
$template = $located_template;
} else {
- $default_templates = $this->get_default_templates();
+ $default_templates = self::get_templates_for_pod_page( self::$exists );
$template = locate_template( $default_templates );
@@ -1573,7 +1609,7 @@ public function template_redirect() {
if ( $template !== $located_template ) {
$template = $located_template;
} else {
- $default_templates = $this->get_default_templates();
+ $default_templates = self::get_templates_for_pod_page( self::$exists );
$template = locate_template( $default_templates, true );
@@ -1600,15 +1636,23 @@ public function template_redirect() {
}
/**
- * Get the list of default templates.
+ * Get templates for pod page.
+ *
+ * @since TBD
*
- * @return array The list of default templates.
+ * @param array|Page $pod_page The pod page data.
+ *
+ * @return array The list of templates for the pod page.
*/
- public function get_default_templates(): array {
+ public static function get_templates_for_pod_page( $pod_page ): array {
+ if ( $pod_page instanceof Page ) {
+ $pod_page = self::object_to_page( $pod_page );
+ }
+
$default_templates = [];
- if ( empty( self::$exists ) ) {
- $uri = explode( '?', self::$exists['uri'] );
+ if ( ! empty( $pod_page ) ) {
+ $uri = explode( '?', $pod_page['uri'] );
$uri = explode( '#', $uri[0] );
$uri = $uri[0];
@@ -1635,7 +1679,7 @@ public function get_default_templates(): array {
* @param array $default_templates The list of default templates.
* @param array $pod_page The current Pod Page data.
*/
- return (array) apply_filters( 'pods_page_default_templates', $default_templates, self::$exists );
+ return (array) apply_filters( 'pods_page_default_templates', $default_templates, $pod_page );
}
}
diff --git a/components/Templates/Templates.php b/components/Templates/Templates.php
index 456c500b0e..ed71f48ec4 100644
--- a/components/Templates/Templates.php
+++ b/components/Templates/Templates.php
@@ -656,24 +656,7 @@ public static function template( $template_name, $code = null, $obj = null, $dep
}
}
} elseif ( $template_name == trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template_name ), ' /-' ) ) {
- $default_templates = array(
- 'pods/templates/' . $template_name . '.php',
- 'pods/' . $template_name . '.php',
- 'pods-' . $template_name . '.php',
- $template_name . '.php',
- );
-
- /**
- * Allow filtering the list of default theme templates to check for a template.
- *
- * @since unknown
- *
- * @param string[] $default_templates The list of default theme templates to check for a template.
- * @param string $template_name The template name.
- * @param array $template The template information.
- * @param Pods $obj The Pods object.
- */
- $default_templates = apply_filters( 'pods_template_default_templates', $default_templates, $template_name, $template, $obj );
+ $default_templates = self::get_templates_for_pod_template( $template, $obj );
if ( empty( $obj->id ) ) {
while ( $obj->fetch() ) {
@@ -715,6 +698,39 @@ public static function template( $template_name, $code = null, $obj = null, $dep
return $out;
}
+ /**
+ * Get templates for pod page.
+ *
+ * @since TBD
+ *
+ * @param array|Template $template The pod template data.
+ * @param Pods|null $obj The Pods object.
+ *
+ * @return array The list of templates for the pod template.
+ */
+ public static function get_templates_for_pod_template( $template, $obj = null ): array {
+ $template_name = trim( preg_replace( '/[^a-zA-Z0-9_\-\/]/', '', $template->get_name() ), ' /-' );
+
+ $default_templates = array(
+ 'pods/templates/' . $template_name . '.php',
+ 'pods/' . $template_name . '.php',
+ 'pods-' . $template_name . '.php',
+ $template_name . '.php',
+ );
+
+ /**
+ * Allow filtering the list of default theme templates to check for a template.
+ *
+ * @since unknown
+ *
+ * @param string[] $default_templates The list of default theme templates to check for a template.
+ * @param string $template_name The template name.
+ * @param array|Template $template The template information.
+ * @param Pods $obj The Pods object.
+ */
+ return (array) apply_filters( 'pods_template_default_templates', $default_templates, $template_name, $template, $obj );
+ }
+
/**
* Parse a template string
*
diff --git a/src/Pods/Whatsit/Legacy_Object.php b/src/Pods/Whatsit/Legacy_Object.php
index 93e7b297bc..bd03ee89d9 100644
--- a/src/Pods/Whatsit/Legacy_Object.php
+++ b/src/Pods/Whatsit/Legacy_Object.php
@@ -20,6 +20,7 @@ public function get_clean_args() {
$old_mapping = [
'name' => 'label',
'slug' => 'name',
+ 'uri' => 'name',
'code' => 'description',
];
@@ -49,6 +50,7 @@ public function get_arg( $arg, $default = null, $strict = false, $raw = false )
$old_mapping = [
'name' => 'label',
'slug' => 'name',
+ 'uri' => 'name',
'code' => 'description',
];