Skip to content

Commit

Permalink
Merge pull request #25 from sansecio/wp-base-url
Browse files Browse the repository at this point in the history
implement BaseURLs for WP/WooCommerce
  • Loading branch information
danslo authored Jan 9, 2025
2 parents f341e76 + 47999a8 commit cf6d85d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/find/find.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"os"

Expand All @@ -27,5 +28,12 @@ func main() {
}
fmt.Printf("- %s (ver: %s) at %s\n", store.Platform.Name(), ver, store.DocRoot)
fmt.Printf("DBC: %+v\n", store.Config.DB)

if urls, err := store.Platform.BaseURLs(context.Background(), store.DocRoot); err == nil {
fmt.Println("Base URLs:")
for _, url := range urls {
fmt.Printf("- %s\n", url)
}
}
}
}
47 changes: 47 additions & 0 deletions fixture/wordpress/wp-includes/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* WordPress Version
*
* Contains version information for the current WordPress release.
*
* @package WordPress
* @since 1.2.0
*/

/**
* The WordPress version string.
*
* Holds the current version number for WordPress core. Used to bust caches
* and to enable development mode for scripts when running from the /src directory.
*
* @global string $wp_version
*/
$wp_version = '6.7.1';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
*
* @global int $wp_db_version
*/
$wp_db_version = 58975;

/**
* Holds the TinyMCE version.
*
* @global string $tinymce_version
*/
$tinymce_version = '49110-20201110';

/**
* Holds the required PHP version.
*
* @global string $required_php_version
*/
$required_php_version = '7.2.24';

/**
* Holds the required MySQL version.
*
* @global string $required_mysql_version
*/
$required_mysql_version = '5.5.5';
39 changes: 39 additions & 0 deletions woocommerce.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package gocommerce

import (
"context"
"errors"
"os"
"path/filepath"
"regexp"
)

Expand Down Expand Up @@ -50,3 +53,39 @@ func (w *WooCommerce) ParseConfig(cfgPath string) (*StoreConfig, error) {
},
}, nil
}

func (w *WooCommerce) BaseURLs(ctx context.Context, docroot string) ([]string, error) {
cfg, err := w.ParseConfig(filepath.Join(docroot, w.ConfigPath()))
if err != nil {
return nil, err
}

db, err := ConnectDB(ctx, *cfg.DB)
if err != nil {
return nil, err
}

prefix, err := cfg.DB.SafePrefix()
if err != nil {
return nil, err
}

var url string
if err = db.QueryRow(`select option_value from ` + prefix + `options where option_name = 'home'`).Scan(&url); err != nil {
return nil, err
}
return []string{url}, nil
}

func (w *WooCommerce) Version(docroot string) (string, error) {
re := regexp.MustCompile(`\$wp_version\s*=\s*'([^']+)';`)
data, err := os.ReadFile(filepath.Join(docroot, "wp-includes", "version.php"))
if err != nil {
return "", err
}
match := re.FindStringSubmatch(string(data))
if len(match) < 2 {
return "", errors.New("no version found")
}
return match[1], nil
}
12 changes: 11 additions & 1 deletion woocommerce_config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package gocommerce

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWooCommerceVersion(t *testing.T) {
wc := WooCommerce{}
ver, err := wc.Version(filepath.Join(fixtureBase, "wordpress"))
assert.NoError(t, err)
assert.Equal(t, "6.7.1", ver)
}

func TestWooCommerceConfigToDSN(t *testing.T) {
tests := map[string]DBConfig{
"wp-config.php": {
Expand Down Expand Up @@ -58,7 +68,7 @@ func TestWooCommerceConfigToDSN(t *testing.T) {

wc := WooCommerce{}
for cnf, want := range tests {
path := fixtureBase + "/wordpress/configs/" + cnf
path := filepath.Join(fixtureBase, "wordpress", "configs", cnf)
got := dbConfigFromSource(t, path, &wc)

if got.DSN() != want.DSN() {
Expand Down

0 comments on commit cf6d85d

Please sign in to comment.