Skip to content

Instantly share code, notes, and snippets.

@jtsternberg
Forked from ramiabraham/gethooks
Last active July 8, 2016 02:02
Show Gist options
  • Save jtsternberg/56c2513c4c74d3acd850d00be85fb94d to your computer and use it in GitHub Desktop.
Save jtsternberg/56c2513c4c74d3acd850d00be85fb94d to your computer and use it in GitHub Desktop.
Copy all actions and filters in a plugin and save to a file.
#!/bin/bash
#
# Prints all hooks in a dir to a .log file.
#
# Permissions issues:
# run: chmod +x gethooks
#
# gist: https://gist.github.com/ramiabraham/e8356e00130351ddcbe2c62125e6c52a
#
# Easy usage:
#
# wget https://gist.githubusercontent.com/ramiabraham/e8356e00130351ddcbe2c62125e6c52a/raw/b56da966068a170c121442e4a19037e483853edd/gethooks && chmod +x gethooks
#
# To answer your question:
# Yes! This will indeed print matches for any occurence of `apply_filters` or `do_action`
# within .php files, including substring matches,
# and any occurrences that may be within doc blocks.
#
# Why?
# Mostly because I really don't want to write that awk regex.
# In addition, adding this would potentially introduce some weird bugs,
# due to the varying ways in which inline comments are added/decorated.
#
#
#
# the output file to create
__log="hooks.log"
# The dir to scan. Set it to the relative dir you need to scan.
#
# For example, if you were using AffiliateWP, and this script is located in the site root:
__setdir="wp-content/plugins/affiliate-wp/includes/"
# __setdir="your/cool/plugin"
# time
__now=$(date +"%Y-%m-%d at %T")
#print new line
newline() {
printf "\n"
}
# create log file
if [[ ! -f $__log ]]; then
touch "hooks.log"
echo "File hooks.log created."
fi
get_actions() {
for i in $(find "$__setdir" -type f -name "*.php"); do
# Uncomment to get line numbers and filenames;
# comment out the other awk command if doing so.
# awk '/do_action(/{print NR":"$0}' $i >> $__log
awk '/do_action/' $i >> $__log
done
}
get_filters() {
for i in $(find "$__setdir" -type f -name "*.php"); do
# Uncomment to get line numbers and filenames;
# comment out the other awk command if doing so.
# awk '/apply_filters(/{print NR":"$0}' $i >> $__log
awk '/apply_filters/' $i >> $__log
done
}
# run process
__run() {
echo "Getting all hooks and copying to $__log." &&
echo "Hooks for $__setdir, generated on $__now." >> $__log &&
echo "Actions:" >> $__log &&
echo $'\n' >> $__log
echo "------------------" >> $__log
echo $'\n' >> $__log
get_actions &&
echo $'\n' >> $__log
echo "Filters:" >> $__log &&
echo $'\n' >> $__log
echo "------------------" >> $__log
echo $'\n' >> $__log
get_filters &&
echo $'\n' >> $__log
echo "Done!" && exit
}
__run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment