-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyleguide-block.php
More file actions
139 lines (130 loc) · 4.61 KB
/
styleguide-block.php
File metadata and controls
139 lines (130 loc) · 4.61 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
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
137
138
139
<?php
// add_filter( 'wp_robots', 'wp_robots_no_robots' );
add_filter(
'rank_math/frontend/robots',
function ( $robots ) {
$robots['follow'] = 'nofollow';
$robots['index'] = 'noindex';
return $robots;
}
);
$defaults = array(
'block_name' => '',
'the_title' => '',
'the_description' => '',
'examples' => array(),
'files' => array(),
'block_directory_name' => '',
'before_content' => '',
);
$args = wp_parse_args( $args, $defaults );
$posts_containing_block = array();
if ( ! empty( $args['block_name'] ) ) {
if ( ! str_starts_with( $args['block_name'], 'acf/' ) ) {
$args['block_name'] = 'acf/' . $args['block_name'];
}
$posts_containing_block_query_args = array(
'post_type' => 'any',
'post_status' => 'publish',
'posts_per_page' => 999,
'orderby' => 'relevance',
's' => $args['block_name'],
// For performance
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
$posts_containing_block_query = get_posts( $posts_containing_block_query_args );
foreach ( $posts_containing_block_query as $found_post ) {
$post_type_obj = get_post_type_object( $found_post->post_type );
$posts_containing_block[] = array(
'title' => apply_filters( 'the_title', $found_post->post_title ),
'url' => get_permalink( $found_post ),
'post_type_label' => $post_type_obj->labels->singular_name,
'post_id' => '<a href="' . esc_url( get_edit_post_link( $found_post ) ) . '">' . $found_post->ID . '</a>',
);
}
// $fields = RH_Blocks::get_acf_fields_for_block( $args['block_name'] );
}
// Process source files
$source_files = array();
if ( ! is_array( $args['files'] ) ) {
$args['files'] = array( $args['files'] );
}
// Check if source files aren't provided but a block directory is
if ( empty( $args['files'] ) && ! empty( $args['block_directory_name'] ) ) {
$dir_path = get_template_directory() . '/blocks/' . $args['block_directory_name'] . '/';
$directory = new RecursiveDirectoryIterator( $dir_path );
$filter = new RecursiveCallbackFilterIterator(
$directory,
function ( $current ) {
// Skip hidden files and directories.
if ( $current->getFilename()[0] === '.' ) {
return false;
}
if ( $current->getExtension() === '' || $current->getExtension() === 'php' ) {
// return true;
}
return true;
}
);
$iterator = new RecursiveIteratorIterator( $filter );
foreach ( $iterator as $file ) {
$the_file = $file->getPathname();
// Make the file path relative to the theme directory
$the_file = str_replace( get_template_directory(), '', $the_file );
$args['files'][] = $the_file;
}
}
// Add the styleguide file to the end of the files list
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS );
if ( ! empty( $backtrace ) ) {
foreach ( $backtrace as $item ) {
if ( str_contains( $item['file'], '/styleguide/' ) ) {
$theme_relative_path = str_replace( get_template_directory(), '', $item['file'] );
$args['files'][] = $theme_relative_path;
break;
}
}
}
if ( ! empty( $args['files'] ) ) {
$github_branch = 'main';
if ( wp_get_environment_type() === 'staging' ) {
$github_branch = 'staging';
}
$theme_path = str_replace( untrailingslashit( ABSPATH ), '', get_template_directory() );
foreach ( $args['files'] as $file_path ) {
$file_path = ltrim( $file_path, '/' );
$new_file = (object) array(
'relative_path' => $file_path,
'root_path' => trailingslashit( $theme_path ) . $file_path,
'github_url' => 'https://github.com/kingkool68/wordpress-rh-starter-theme/blob/' . $github_branch . '/' . $file_path,
'extension' => pathinfo( $file_path, PATHINFO_EXTENSION ),
);
$source_files[] = $new_file;
}
}
$context = array(
'the_breadcrumbs' => RH_Breadcrumbs::render(
array(
'items' => array(
array(
'text' => 'Styleguide',
'url' => get_site_url() . '/styleguide/',
),
array(
'text' => $args['the_title'],
),
),
)
),
'the_title' => apply_filters( 'the_title', $args['the_title'] ),
'the_description' => apply_filters( 'the_content', $args['the_description'] ),
'examples' => $args['examples'],
'posts_containing_block' => $posts_containing_block,
'is_user_logged_in' => is_user_logged_in(),
'wp_login_url' => wp_login_url( $redirect = RH_Helpers::get_current_url() ),
'source_files' => $source_files,
'before_content' => $args['before_content'],
);
Sprig::out( 'styleguide-block.twig', $context );