Created
June 8, 2020 09:59
-
-
Save DrizzlyOwl/2bffc34ae3ecda175a2295277e4c21a2 to your computer and use it in GitHub Desktop.
Scan a WordPress theme directory for any PHP components that are not in use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# Load libs | |
import os | |
import re | |
import argparse | |
# Register CLI args | |
parser = argparse.ArgumentParser( | |
description='Parse all your WordPress templates to determine which PHP components are not in use', | |
epilog='Made by Mixd https://www.mixd.co.uk/' | |
) | |
parser.add_argument( | |
'--quiet', | |
help='suppress non-essential info', | |
default=False | |
) | |
args = parser.parse_args() | |
# Get theme directory | |
theme_path = os.path.dirname(os.path.realpath(__file__)) | |
# Get a list of all components | |
components = {} | |
components_path = theme_path + '/components/' | |
for component in os.listdir(components_path): | |
if component == ".DS_Store" or component == "_example": | |
continue | |
# Set counter to zero for this component | |
components[component] = 0 | |
if args.quiet == False: | |
print(str(len(components)) + " components found") | |
# Register searchable directories | |
dirs_to_search = [ | |
theme_path, | |
theme_path + '/lib/', | |
theme_path + '/lib/acf-blocks/', | |
theme_path + '/lib/shortcodes/' | |
] | |
dirs_to_remove = [] | |
# Safety check to ensure the folders exist | |
for path in dirs_to_search: | |
isdir = os.path.isdir(path) | |
if isdir is False: | |
if args.quiet == False: | |
print(path + " is not a valid directory, skipping") | |
dirs_to_remove.append(path) | |
for item in dirs_to_remove: | |
dirs_to_search.remove(item) | |
# Iterate through the registered directories | |
for dir in dirs_to_search: | |
if args.quiet == False: | |
print("Listing " + dir) | |
for file in os.listdir(dir): | |
# Skip certain files to avoid false positives | |
if file == "functions.php" or file == "component-loader.php": | |
continue | |
# Match only PHP files | |
if file.endswith(".php"): | |
if args.quiet == False: | |
print("Processing: " + file) | |
found = False | |
# Build a qualified path to the file | |
php_file = os.path.join(dir, file) | |
# Open the file for reading | |
template = open(php_file, "r") | |
# Parse the file as text | |
template_raw = template.read() | |
# Close the file handler | |
template.close() | |
# For each component, see if there is a reference to get_component in the file | |
for component in components: | |
# Search the line for the component name | |
pattern = r"get_component\((\s+)?('|\")" + component + "('|\")" | |
search = re.search(pattern, template_raw, re.MULTILINE) | |
if search: | |
# Remove the component from the list if its found | |
if args.quiet == False: | |
print("Found: " + component + " (" + str(components[component]) + " instances so far)") | |
found = True | |
# Increment counter | |
components[component] += 1 | |
if found == False: | |
if args.quiet == False: | |
print("No components found") | |
if args.quiet == False: | |
print("") | |
i = 1 | |
print("Components not in use:") | |
for component, count in components.items(): | |
if count == 0: | |
print(str(i) + ". " + component) | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment