Skip to content

Commit

Permalink
Remove Old Plugin Migrator!
Browse files Browse the repository at this point in the history
  • Loading branch information
techjewel committed May 5, 2024
1 parent 152386d commit 69ab9e9
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 458 deletions.
57 changes: 20 additions & 37 deletions app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public static function getStorageDir()
return WP_CONTENT_DIR . '/fluent-snippet-storage';
}

public static function getCachedDir()
{
$dir = self::getStorageDir() . '/cached';

// check if the directory exists
if (!is_file($dir)) {
wp_mkdir_p($dir);
// add an empty index.php file to that dir
file_put_contents($dir.'/index.php', '<?php // silence is golden');
}

return $dir;
}

public static function validateCode($language, $code)
{
if (!$code) {
Expand Down Expand Up @@ -86,7 +100,8 @@ public static function cacheSnippetIndex($fileName = '', $isForced = false, $ext
'run_at',
'priority',
'group',
'condition'
'condition',
'load_as_file'
];

$snippets = (new \FluentSnippets\App\Model\Snippet())->get();
Expand Down Expand Up @@ -188,42 +203,6 @@ private static function getConfigFromFile()
$cachedFile = self::getStorageDir() . '/index.php';

if (!is_file($cachedFile)) {


// @todo: remove this migration at mid january 2024

// maybe we have files in uploads directory
$oldLocationIndex = wp_upload_dir()['basedir'] . '/fluent-snippet-storage/index.php';
if (!is_file($oldLocationIndex)) {
return [];
}

if (!class_exists('\WP_Filesystem')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}

// Initialize the WordPress filesystem
WP_Filesystem();
global $wp_filesystem;

// Define the source and destination paths
$source = wp_upload_dir()['basedir'] . '/fluent-snippet-storage';
$destination = self::getStorageDir();

// Check if the source directory exists
if ($wp_filesystem->is_dir($source)) {
// Create destination directory if it doesn't exist
if (!$wp_filesystem->is_dir($destination)) {
$wp_filesystem->mkdir($destination);
}

// Move the folder
if (copy_dir($source, $destination)) {
// Optionally, delete the original folder after copying
$wp_filesystem->delete($source, true);
}
}

return [];
}

Expand Down Expand Up @@ -328,6 +307,10 @@ public static function sanitizeMetaValue($value)
return $value;
}

if (!$value) {
return $value;
}

if (str_contains($value, '*/')) {
$value = str_replace('*/', '', $value); // we will not allow */ in meta values
}
Expand Down
41 changes: 0 additions & 41 deletions app/Hooks/Handlers/AdminMenuHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class AdminMenuHandler
public function register()
{
add_action('admin_menu', array($this, 'addMenu'));

add_action('admin_notices', [$this, 'maybeShowMigrationNotice'], 1);
}

public function addMenu()
Expand Down Expand Up @@ -83,23 +81,15 @@ public function render()
$indexConfig = Helper::getIndexedConfig();

if (!$indexConfig) {
// It's a new installation let's check if it's a legacy user
(new UpgradeHandler())->maybeUpgradeFromOld();
$indexConfig = Helper::getIndexedConfig();
}

$this->maybeMuModeMigrate();

add_filter('admin_footer_text', function ($content) use ($indexConfig) {
$ext = '';
if (defined('FLUENT_SNIPPETS_RUNNING_MU')) {
$ext = '<b>Standalone (MU Mode) is active</b> ';
}

if (Arr::get($indexConfig, 'meta.legacy_status') == 'migrated') {
$ext .= 'FluentSnippets was known as Easy Code Manager.';
}

return 'Thank you for using <a rel="noopener" target="_blank" href="https://fluentsnippets.com">Fluent Snippets</a>.' . ' ' . $ext;
});

Expand Down Expand Up @@ -434,35 +424,4 @@ private function getMenuIcon()
<path d="M46.4413 87.5247L46.4413 87.5247C45.6523 88.3139 45.2091 89.3841 45.2091 90.5C45.2091 91.6159 45.6523 92.6861 46.4413 93.4753L46.4413 93.4754L61.2716 108.306C61.6592 108.706 62.1225 109.026 62.6346 109.246C63.148 109.466 63.7002 109.582 64.259 109.587C64.8178 109.592 65.3719 109.485 65.8891 109.274C66.4063 109.062 66.8762 108.75 67.2713 108.355C67.6665 107.96 67.9789 107.49 68.1905 106.972C68.4021 106.455 68.5086 105.901 68.5038 105.342C68.4989 104.784 68.3828 104.231 68.1623 103.718C67.9423 103.206 67.6228 102.743 67.2223 102.355L55.3674 90.5L67.2223 78.6451C67.6228 78.2575 67.9423 77.7942 68.1623 77.2821C68.3828 76.7687 68.4989 76.2165 68.5038 75.6577C68.5086 75.0989 68.4021 74.5447 68.1905 74.0276C67.9789 73.5104 67.6665 73.0405 67.2713 72.6454C66.8762 72.2502 66.4063 71.9378 65.8891 71.7262C65.3719 71.5146 64.8178 71.4081 64.259 71.4129C63.7002 71.4178 63.148 71.5339 62.6346 71.7544C62.1225 71.9744 61.6593 72.2939 61.2717 72.6943L46.4413 87.5247ZM126.559 93.4754L126.559 93.4753C127.348 92.6861 127.791 91.6159 127.791 90.5C127.791 89.3841 127.348 88.3139 126.559 87.5247L126.559 87.5247L111.731 72.6975C111.73 72.6964 111.729 72.6953 111.728 72.6943C111.341 72.2938 110.877 71.9744 110.365 71.7544C109.852 71.5339 109.3 71.4178 108.741 71.4129C108.182 71.4081 107.628 71.5146 107.111 71.7262C106.594 71.9378 106.124 72.2502 105.729 72.6454C105.334 73.0405 105.021 73.5104 104.809 74.0276C104.598 74.5447 104.491 75.0989 104.496 75.6577C104.501 76.2165 104.617 76.7687 104.838 77.2821C105.058 77.7941 105.377 78.2574 105.778 78.6449C105.779 78.646 105.78 78.6471 105.781 78.6481L117.633 90.5L105.781 102.352C105.78 102.353 105.779 102.354 105.778 102.355C105.377 102.743 105.058 103.206 104.838 103.718C104.617 104.231 104.501 104.784 104.496 105.342C104.491 105.901 104.598 106.455 104.809 106.972C105.021 107.49 105.334 107.96 105.729 108.355C106.124 108.75 106.594 109.062 107.111 109.274C107.628 109.485 108.182 109.592 108.741 109.587C109.3 109.582 109.852 109.466 110.365 109.246C110.877 109.026 111.341 108.706 111.728 108.306C111.729 108.305 111.73 108.304 111.731 108.302L126.559 93.4754ZM77.9258 120.506L77.9304 120.508C78.3052 120.611 78.692 120.665 79.0808 120.667H79.0837C79.9982 120.666 80.8877 120.367 81.6174 119.816C82.3471 119.265 82.8773 118.491 83.1279 117.611L97.9587 65.7038C98.1319 65.1684 98.196 64.6037 98.1472 64.0431C98.0981 63.4793 97.9359 62.9312 97.6701 62.4316C97.4043 61.932 97.0404 61.4911 96.6003 61.1353C96.1602 60.7796 95.6529 60.5162 95.1087 60.3611C94.5644 60.2059 93.9945 60.1621 93.4329 60.2322C92.8714 60.3024 92.3298 60.4851 91.8405 60.7694C91.3512 61.0538 90.9242 61.4339 90.5852 61.887C90.2481 62.3375 90.0048 62.851 89.8697 63.397L75.0389 115.305C74.7324 116.377 74.8644 117.528 75.4057 118.503C75.947 119.479 76.8535 120.199 77.9258 120.506Z" stroke="white"/>
</svg>');
}

public function maybeShowMigrationNotice()
{
$config = Helper::getIndexedConfig();
if ($config || !get_option('ecm_db_version') || !current_user_can('manage_options')) {
return;
}
?>
<div class="notice notice-error">
<h3>Easy Code Manager is now FluentSnippets</h3>
<p>We have upgraded Easy Code Manager with lots of new features and everything is free & opensource. Please
<b>migrate and review your snippets</b> and experience unmatched performance.</p>
<p><a class="button button-primary"
href="<?php echo esc_url(admin_url('admin.php?page=fluent-snippets#/')); ?>">Review Your Snippets</a>
</p>
</div>
<?php
}

private function maybeMuModeMigrate()
{
if (!defined('FLUENT_SNIPPETS_RUNNING_MU_VERSION')) {
return;
}

if (FLUENT_SNIPPETS_RUNNING_MU_VERSION == FLUENT_SNIPPETS_PLUGIN_VERSION) {
return;
}

Helper::enableStandAlone(true);
}
}
37 changes: 0 additions & 37 deletions app/Hooks/Handlers/UpgradeHandler.php

This file was deleted.

78 changes: 54 additions & 24 deletions app/Model/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Snippet
{

private $ars = [];
private $args = [];

public function __construct($args = [])
{
Expand Down Expand Up @@ -274,11 +274,16 @@ public function updateSnippet($fileName, $code, $metaData)
}

$docBlockString = $this->parseInputMeta($metaData, true);

$fullCode = $docBlockString . $code;

file_put_contents($file, $fullCode);

$type = Arr::get($metaData, 'type');

if ($type == 'css' || $type == 'js') {
$this->maybeCacheCssJs($fileName, $metaData, $code);
}

return $fileName;
}

Expand Down Expand Up @@ -318,6 +323,8 @@ public function createSnippet($code, $metaData)

file_put_contents($file, $fullCode);

$this->maybeCacheCssJs($fileName, $metaData, $code);

return $fileName;
}

Expand Down Expand Up @@ -360,14 +367,15 @@ public function parseBlock($fileContent, $codeOnly = false)
$docBlock = explode('*', $docBlock);
// Explode by : and get the key and value
$docBlockArray = [
'name' => '',
'status' => '',
'tags' => '',
'description' => '',
'type' => '',
'run_at' => '',
'group' => '',
'condition' => ''
'name' => '',
'status' => '',
'tags' => '',
'description' => '',
'type' => '',
'run_at' => '',
'group' => '',
'condition' => '',
'load_as_file' => ''
];

foreach ($docBlock as $key => $value) {
Expand Down Expand Up @@ -413,20 +421,21 @@ public function parseBlock($fileContent, $codeOnly = false)
private function parseInputMeta($metaData, $convertString = false)
{
$metaDefaults = [
'description' => '',
'tags' => '',
'group' => '',
'name' => 'Snippet Created @ ' . current_time('mysql'),
'type' => 'PHP',
'status' => 'draft',
'created_by' => get_current_user_id(),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'is_valid' => 1,
'updated_by' => get_current_user_id(),
'priority' => 10,
'run_at' => '',
'condition' => [
'description' => '',
'tags' => '',
'group' => '',
'name' => 'Snippet Created @ ' . current_time('mysql'),
'type' => 'PHP',
'status' => 'draft',
'created_by' => get_current_user_id(),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'is_valid' => 1,
'updated_by' => get_current_user_id(),
'priority' => 10,
'run_at' => '',
'load_as_file' => '',
'condition' => [
'status' => 'no',
'run_if' => 'assertive',
'items' => [[]]
Expand Down Expand Up @@ -457,4 +466,25 @@ private function parseInputMeta($metaData, $convertString = false)

return $docBlockString;
}

private function maybeCacheCssJs($fileName, $metaData = [], $code = '')
{
// type
$type = Arr::get($metaData, 'type');
if ($type == 'css' || $type == 'js') {
// get file name without extension
$cacheFileName = str_replace('.php', '.' . $type, $fileName);
$fullFileName = Helper::getCachedDir() . '/' . $cacheFileName;
if (Arr::get($metaData, 'load_as_file') == 'yes') {
file_put_contents($fullFileName, $code);
return $cacheFileName;
}

if (file_exists($fullFileName)) {
@unlink($fullFileName);
}
}

return false;
}
}
Loading

0 comments on commit 69ab9e9

Please sign in to comment.