-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiki-link.php
More file actions
74 lines (64 loc) · 2.03 KB
/
Copy pathwiki-link.php
File metadata and controls
74 lines (64 loc) · 2.03 KB
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
<?php
/**
* WP-CLI script that searches for "data-wiki-link-title" attributes and adds
* the proper WordPress permalink after a Wikispaces conversion.
*/
/**
* Wrapper function for strpos() to return all positions of a certain string.
*
* @param string $haystack Same usage as strpos().
* @param string $needle Same usage as strpos().
* @param int $offset Same usage as strpos().
* @param array $retval Array containing all positions.
* @return array
*/
function strposall( $haystack = '', $needle = '', $offset = 0, $retval = array() ) {
if ( false !== strpos( $haystack, $needle, $offset ) ) {
$pos = strpos( $haystack, $needle, $offset );
$retval[] = $pos;
$pos = $pos + 1;
if ( false !== strpos( $haystack, $needle, $pos ) ) {
$pos = strpos( $haystack, $needle, $pos );
$retval[] = $pos;
$pos = $pos + 1;
$retval = strposall( $haystack, $needle, $pos, $retval );
}
}
return $retval;
}
$q = new WP_Query( array(
's' => 'data-wiki-link-title',
'post_type' => 'page',
'nopaging' => true,
'update_post_term_cache' => false,
'no_found_rows' => true,
'orderby' => 'none'
) );
if ( empty( $q->posts ) ) {
echo 'No conversion needed!';
return;
}
foreach ( $q->posts as $post ) {
$wiki_link_refs = strposall( $post->post_content, 'data-wiki-link-title="' );
if ( empty( $wiki_link_refs ) ) {
continue;
}
$search = $replace = array();
foreach( $wiki_link_refs as $ref ) {
$end_pos = strpos( $post->post_content, '"', $ref + 22 + 1 );
$title = substr( $post->post_content, $ref + 22, $end_pos - ( $ref + 22 ) );
$page = get_page_by_title( $title );
if ( ! empty( $page->post_title ) ) {
$search[] = 'data-wiki-link-title="' . $title . '"';
$replace[] = 'href="' . get_permalink( $page->ID ) . '"';
}
}
if ( ! empty( $search ) ) {
$content = str_replace( $search, $replace, $post->post_content );
wp_update_post( array(
'ID' => $post->ID,
'post_content' => $content
) );
}
}
echo 'All done!';