/
wp2grav_exporter.php
136 lines (116 loc) · 3.79 KB
/
wp2grav_exporter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
Plugin Name: Grav Export
Plugin URI: https://www.github.com/jgonyea/wp2grav_exporter
Description: This plugin converts WP content for use in a GravCMS instance.
Version: 0.1.0
Author: Jeremy Gonyea
Author URI: https://gonyea.io
License: MIT
License URI: https://mit-license.org/
*/
require 'vendor/autoload.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$plugin_dir = plugin_dir_path( __FILE__ );
// Load plugins.
require_once ( $plugin_dir . "plugins/wp2grav-all.php" );
require_once ( $plugin_dir . "plugins/wp2grav-posts.php" );
require_once ( $plugin_dir . "plugins/wp2grav-post_types.php" );
require_once ( $plugin_dir . "plugins/wp2grav-roles.php" );
require_once ( $plugin_dir . "plugins/wp2grav-users.php" );
// Register commands with wp-cli.
WP_CLI::add_command( 'wp2grav-all', 'wp2grav_export_all' );
WP_CLI::add_command( 'wp2grav-posts', 'wp2grav_export_posts' );
WP_CLI::add_command( 'wp2grav-post_types', 'wp2grav_export_post_types' );
WP_CLI::add_command( 'wp2grav-roles', 'wp2grav_export_roles' );
WP_CLI::add_command( 'wp2grav-users', 'wp2grav_export_users' );
}
/**
* Converts a Wordpress role name to a valid Grav group name.
*
* @param string $role_name
* Wordpress role to be converted for Grav group name.
*
* @return string
* Grav compatible group name.
*/
function convert_role_wp_to_grav($role_name) {
return preg_replace('/\ /', '_', $role_name);
}
/**
* Converts a Wordpress username to Grav.
*
* @param WP_USER $user
* Wordpress user to be converted.
*
* @return string
* Grav username.
*/
function convert_username_wp_to_grav($user) {
// Default Grav settings. Make sure to reflect changes in Grav's system.yaml.
$user_char_mim_limit = 4;
$user_char_max_limit = 16;
$username = $user->get('user_login');
mb_strtolower($username);
// Replace invalid characters with underscore.
$patterns = array(
'space' => '/\ /',
'period' => '/\./',
'apostrophe' => '/\'/',
);
$replacements = array(
'space' => '_',
'period' => '_',
'apostrophe' => '_',
);
$username = preg_replace($patterns, $replacements, $username);
// Pad short usernames.
if (strlen($username) < $user_char_mim_limit) {
$username = $username . $user->uid;
$username = str_pad($username, $user_char_mim_limit, "_");
}
// Trim long usernames.
if (strlen($username) > $user_char_max_limit) {
$uid_length = strlen($user->uid);
$username = substr($username, 0, ($user_char_max_limit - $uid_length));
$username .= $user->uid;
}
return $username;
}
/**
* Connects to local filesystem.
*/
function connect_fs($url, $method, $context, $fields = null)
{
global $wp_filesystem;
if(false === ($credentials = request_filesystem_credentials($url, $method, false, $context, $fields)))
{
return false;
}
//check if credentials are correct or not.
if(!WP_Filesystem($credentials))
{
request_filesystem_credentials($url, $method, true, $context);
return false;
}
return true;
}
function random_str( $length = 16 ) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
/**
* Finds all posts of a post_type.
* This is used to find all posts of a certain post type. The built-in function of get_post doesn't find drafts/ scheduled.
*/
function wp2grav_find_posts( $type = 'post' ) {
global $wpdb;
$query = "SELECT * FROM {$wpdb->prefix}posts where `post_type` = '" . $type . "'";
$posts = $wpdb->get_results( $query, OBJECT, );
return $posts;
}