Skip to content

Commit

Permalink
add bg process for migrating legacy data, and github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
twoelevenjay committed Oct 28, 2024
1 parent 1a41470 commit 6a4c676
Show file tree
Hide file tree
Showing 14 changed files with 478 additions and 83 deletions.
72 changes: 72 additions & 0 deletions .github/scripts/generate-readme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const fs = require('fs');
const path = require('path');

const readmePath = path.join(__dirname, '../../readme.md');
const changelogPath = path.join(__dirname, '../../CHANGELOG.md');
const outputPath = path.join(__dirname, '../temp/readme.txt');

// Function to detect and remove badge lines
function shouldRemoveLine(line) {
return line.startsWith('[!') || line.includes('![Build Status]') || line.includes('badges/');
}

function formatLine(line) {
if (line.startsWith('### ')) {
return `= ${line.replace('### ', '').trim()} =\n`;
}
if (line.startsWith('## ')) {
return `== ${line.replace('## ', '').trim()} ==\n`;
}
if (line.startsWith('# ')) {
return `=== ${line.replace('# ', '').trim()} ===\n`;
}
if (line.startsWith('**Q:')) {
return `= ${line.replace('**Q:', '').replace('**', '').trim()} =\n`;
}
if (line.startsWith('**A:')) {
return `${line.replace('**A:', '').replace('**', '').trim()}\n`;
}
// Replace **bold** and remove extra spaces
return line.replace(/\*\*/g, '').replace(/\s+/g, ' ') + '\n';
}

// Check if the output path (readme.txt) exists, and if not, create it
function ensureFileExists(filePath) {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true }); // Create directories if needed
}
if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, '', 'utf8'); // Create the file if it doesn't exist
}
}

// Read the files and filter the lines
function processFiles() {
const readmeMd = fs.readFileSync(readmePath, 'utf8').split('\n');
const changelogMd = fs.readFileSync(changelogPath, 'utf8').split('\n');
const output = [];

// Process readme.md file
readmeMd.forEach(line => {
if (!shouldRemoveLine(line)) {
output.push(formatLine(line));
}
});

// Process changelog.md and format accordingly
changelogMd.forEach(line => {
if (line.startsWith('### ')) {
output.push(`= ${line.replace('### ', '').trim()} =\n`);
} else {
output.push(line + '\n');
}
});

// Ensure the output file exists and write the content
ensureFileExists(outputPath);
fs.writeFileSync(outputPath, output.join(''), 'utf8');
console.log(`readme.txt generated at ${outputPath}`);
}

processFiles();
55 changes: 55 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Deploy to WordPress SVN

on:
push:
tags:
- 'v*.*.*'

jobs:
deploy:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout the repository
- name: Checkout Repository
uses: actions/checkout@v2

# Step 2: Set up Node.js (use the latest stable version)
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 'lts/*' # Use latest LTS version of Node.js

# Step 3: Install Node.js dependencies (if you have any, can be skipped if not needed)
# - name: Install dependencies
# run: npm install

# Step 4: Generate readme.txt using the Node.js script in .github/scripts/
- name: Generate readme.txt
run: node .github/scripts/generate-readme.js

# Step 5: Install Subversion
- name: Install Subversion
run: sudo apt-get install subversion

# Step 6: Deploy to WordPress SVN /trunk/
- name: Deploy to WordPress SVN /trunk/
env:
SVN_USERNAME: ${{ secrets.WORDPRESS_USERNAME }}
SVN_PASSWORD: ${{ secrets.WORDPRESS_PASSWORD }}
run: |
svn co https://plugins.svn.wordpress.org/discontinued-products/ svn-dir
rsync -av --delete --exclude=".git" ./ svn-dir/trunk/
cp .github/temp/readme.txt svn-dir/trunk/readme.txt # Copy the newly generated readme.txt
cd svn-dir
svn add --force trunk/*
svn commit -m "Deploying version ${{ github.ref }}" --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive
# Step 7: Create a tag in WordPress SVN /tags/
- name: Create SVN Tag
env:
SVN_USERNAME: ${{ secrets.WORDPRESS_USERNAME }}
SVN_PASSWORD: ${{ secrets.WORDPRESS_PASSWORD }}
run: |
VERSION=${GITHUB_REF/refs\/tags\/v/}
svn cp https://plugins.svn.wordpress.org/discontinued-products/trunk https://plugins.svn.wordpress.org/discontinued-products/tags/$VERSION -m "Tagging version $VERSION" --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Changelog

### 2.0.0 10.27.2024
* Major update to the plugin.
* Fixed - Queries use the taxonomy table for better performance.
* Added - DB updater to migrate product meta data to the new taxonomies.

### 1.1.7 04.25.2020
* Fixed - price class .discontinued
* Removed - span.discontinued into the price
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"require": {
"woocommerce/woocommerce-sniffs": "^0.1.0"
"woocommerce/woocommerce-sniffs": "^0.1.3"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
24 changes: 14 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 75 additions & 1 deletion includes/class-discontinued-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Discontinued_Products Class.
*
* @package woocommerce
* @since 1.1.7
* @since 1.0.0
*/

if ( ! class_exists( 'Discontinued_Products' ) ) {
Expand All @@ -16,6 +16,13 @@
*/
class Discontinued_Products {

/**
* Background process to update data.
*
* @var DP_Data_Background_Process
*/
protected static $background_process;

/**
* Inititiate the Discontinued_Products Class.
*
Expand Down Expand Up @@ -54,8 +61,75 @@ public function woocommerce_loaded() {
include DP_PATH . 'includes/class-dp-settings.php';
include DP_PATH . 'includes/class-dp-shortcode-discontinued.php';
include DP_PATH . 'includes/class-dp-taxonomy.php';
include_once DP_PATH . 'includes/class-dp-data-background-process.php';
self::$background_process = new DP_Data_Background_Process();
add_action( 'admin_notices', array( $this, 'update_notice' ) );
$this->maybe_run_data_updater();
}

/**
* Take care of anything that needs woocommerce to be loaded.
* For instance, if you need access to the $woocommerce global
*/
public function maybe_run_data_updater() {
if ( isset( $_GET['do_update_dp'] ) && check_admin_referer( 'dp_update_nonce', '_wpnonce' ) ) {
// Run the data updater.
self::$background_process->start();
set_transient( 'do_update_dp', 'updating', 0 );
return;
}
if ( isset( $_GET['wc-hide-notice'] ) && 'dp_updated' === $_GET['wc-hide-notice'] ) {
delete_transient( 'do_update_dp' );
return;
}
if ( 'updating' === get_transient( 'do_update_dp' ) && ! self::has_legacy_meta() ) {
set_transient( 'do_update_dp', 'updated', 0 );
}
}

/**
* If we need to update the data, include a message with the data update button.
*/
public static function update_notice() {

$update_dp_transient = get_transient( 'do_update_dp' );
// Check if any products have legacy meta keys.
$has_legacy_meta = self::has_legacy_meta();

if ( $has_legacy_meta ) {

if ( 'updating' === $update_dp_transient ) {
include DP_PATH . 'views/html-notice-updating.php';
} else {
include DP_PATH . 'views/html-notice-update.php';
}
} elseif ( 'updated' === $update_dp_transient ) {
include DP_PATH . 'views/html-notice-updated.php';
}
}

/**
* CHeck if legacy meta keys exist.
*/
public static function has_legacy_meta() {
global $wpdb;

// Check if any products have legacy meta keys.
$has_legacy_meta = $wpdb->get_var(
"
SELECT 1
FROM {$wpdb->postmeta} pm
JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE p.post_type = 'product'
AND pm.meta_key IN ('_hide_from_search', '_hide_from_shop', '_is_discontinued')
LIMIT 1
"
);

return $has_legacy_meta;
}


/**
* Take care of anything that needs all plugins to be loaded
*/
Expand Down
Loading

0 comments on commit 6a4c676

Please sign in to comment.