| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Alt Text Tools |
| 4 |
Description: Exports a CSV file of all images that are actually used in your content, along with their corresponding alt tags. |
| 5 |
Version: 0.2.0 |
| 6 |
Author: NerdPress |
| 7 |
Author URI: https://www.nerdpress.net |
| 8 |
License: GPLv2 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) |
| 12 |
die( 'YOU SHALL NOT PASS' ); |
| 13 |
|
| 14 |
if ( ! defined( 'NP_ATT_VERSION' ) ) |
| 15 |
define( 'NP_ATT_VERSION', '0.2.0' ); |
| 16 |
|
| 17 |
class NerdpressAltTextTools { |
| 18 |
/** |
| 19 |
* @var string. Plugins PRO Api Key |
| 20 |
* |
| 21 |
* This is reserved for the tiered features |
| 22 |
* |
| 23 |
* @since 0.0.1 |
| 24 |
*/ |
| 25 |
private $apikey; |
| 26 |
|
| 27 |
/** |
| 28 |
* Class' initializer |
| 29 |
* |
| 30 |
* @since 0.0.1 |
| 31 |
*/ |
| 32 |
public static function init() { |
| 33 |
$class = __CLASS__; |
| 34 |
new $class; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
* |
| 40 |
* @since 0.0.1 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
if ( ! current_user_can( 'manage_options' ) ) |
| 44 |
return; |
| 45 |
|
| 46 |
add_action( 'admin_menu', array( $this, 'settingsPage' ) ); |
| 47 |
add_action( 'wp_ajax_getCsv', array( $this, 'getCsv' ) ); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Inject JS script(s) into the settings page |
| 53 |
* |
| 54 |
* @since 0.0.1 |
| 55 |
*/ |
| 56 |
public function injectScripts() { |
| 57 |
wp_register_script( 'np_alt_tools_js', plugins_url( 'js/np_alt_tools.js', __FILE__ ), array( 'jquery' ), NP_ATT_VERSION ); |
| 58 |
wp_localize_script( 'np_alt_tools_js', 'np_alt_tools', array( |
| 59 |
'endpoint' => admin_url( 'admin-ajax.php' ), |
| 60 |
'nonce' => wp_create_nonce( 'np_alt_tools_secure_me' ) |
| 61 |
) ); |
| 62 |
wp_enqueue_script( 'np_alt_tools_js' ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Plugin settings page (where most of the action happens ;) |
| 67 |
* Use injectScripts to insert admin scripts only on the settings page |
| 68 |
* |
| 69 |
* @since 0.0.1 |
| 70 |
*/ |
| 71 |
public function settingsPage() { |
| 72 |
$hookSuffix = add_management_page( |
| 73 |
'Alt Text Tools', |
| 74 |
'Alt Text Tools', |
| 75 |
'manage_options', |
| 76 |
'nerdpress-alt-text-tools', |
| 77 |
array( $this, 'settingsHtml' ) |
| 78 |
); |
| 79 |
add_action( 'admin_print_scripts-' . $hookSuffix, array( $this, 'injectScripts' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Markup for the settings page |
| 84 |
* |
| 85 |
* @since 0.0.1 |
| 86 |
*/ |
| 87 |
public function settingsHtml() { |
| 88 |
?> |
| 89 |
<div class="wrap"> |
| 90 |
<h1>NerdPress Alt Text Tools</h1> |
| 91 |
<div class="button" id="npatt-csv-action" style="margin-top:30px">Download Alt Tag CSV</div> |
| 92 |
</div> |
| 93 |
<?php |
| 94 |
} |
| 95 |
|
| 96 |
private static function quoteIfComma( $fields_array ) { |
| 97 |
$result = []; |
| 98 |
foreach( $fields_array as $field => $value ) { |
| 99 |
if ( strpos( $value, ',' ) !== FALSE ) |
| 100 |
$result[] = '"' . $value . '"'; |
| 101 |
else |
| 102 |
$result[] = $value; |
| 103 |
} |
| 104 |
return $result; |
| 105 |
} |
| 106 |
|
| 107 |
private static function getImageNameFromUrl( $img_url ) { |
| 108 |
$path_parts = explode( '/', $img_url ); |
| 109 |
$len = count( $path_parts ); |
| 110 |
if ( empty( $path_parts[ $len - 1 ] ) ) |
| 111 |
$image_name = $path_paths[ $len - 2 ]; |
| 112 |
else |
| 113 |
$image_name = $path_parts[ $len - 1 ]; |
| 114 |
|
| 115 |
// Strip the size of the image |
| 116 |
return preg_replace( '/-[0-9]+x[0-9]+/', '', $image_name ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Assemble a csv file |
| 121 |
* To be called via AJAX |
| 122 |
* |
| 123 |
* @since 0.0.1 |
| 124 |
*/ |
| 125 |
public static function getCsv() { |
| 126 |
check_ajax_referer( 'np_alt_tools_secure_me', 'np_alt_tools_nonce' ); |
| 127 |
|
| 128 |
/*============================= |
| 129 |
* Get all the posts/pages etc. |
| 130 |
*=============================*/ |
| 131 |
$opts = array( |
| 132 |
'public' => TRUE, |
| 133 |
'_builtin' => FALSE |
| 134 |
); |
| 135 |
$postTypes = array_merge( |
| 136 |
array( 'post', 'page' ), |
| 137 |
get_post_types( $opts ) |
| 138 |
); |
| 139 |
|
| 140 |
$posts = []; |
| 141 |
foreach( $postTypes as $type ) { |
| 142 |
$posts = array_merge( $posts, get_posts( array( |
| 143 |
'posts_per_page' => -1, |
| 144 |
'post_type' => $type |
| 145 |
) ) ); |
| 146 |
} |
| 147 |
sort( $posts ); |
| 148 |
|
| 149 |
/*============================ |
| 150 |
* Find images, src, alt tags. |
| 151 |
*============================*/ |
| 152 |
$imgElemRegex = '/<img[^>]*>/'; |
| 153 |
$site_url = site_url(); |
| 154 |
$finds = []; |
| 155 |
foreach( $posts as $post ) { |
| 156 |
$post_link = get_permalink( $post ); |
| 157 |
$post_title = get_the_title( $post ); |
| 158 |
$post_edit_link = get_edit_post_link( $post ); |
| 159 |
$found = preg_match_all( |
| 160 |
$imgElemRegex, |
| 161 |
apply_filters( 'the_content', $post->post_content ), |
| 162 |
$matches, |
| 163 |
PREG_PATTERN_ORDER |
| 164 |
); |
| 165 |
|
| 166 |
if ( ! $found ) continue; |
| 167 |
|
| 168 |
foreach( $matches as $match ) { |
| 169 |
foreach( $match as $submatch ) { |
| 170 |
preg_match( '/src="([^"]*)"/', $submatch, $img_url ); |
| 171 |
if ( strpos( $img_url[ 1 ], $site_url ) !== FALSE ) |
| 172 |
$media_link = $site_url .'/wp-admin/upload.php?s=' . self::getImageNameFromUrl( $img_url[ 1 ] ); |
| 173 |
else |
| 174 |
$media_link = ''; |
| 175 |
|
| 176 |
preg_match( '/alt="([^"]*)"/', $submatch, $alt_tag ); |
| 177 |
if ( ! $alt_tag ) |
| 178 |
$alt_tag = [ '', 'MISSING' ]; |
| 179 |
else if ( $alt_tag[1] == '' ) |
| 180 |
$alt_tag = [ '', 'EMPTY' ]; |
| 181 |
|
| 182 |
$finds[] = array( |
| 183 |
$post->ID, |
| 184 |
$post->post_type, |
| 185 |
html_entity_decode( $post_title ), |
| 186 |
html_entity_decode( $post_link ), |
| 187 |
html_entity_decode( $img_url[1] ), |
| 188 |
html_entity_decode( $alt_tag[1] ), |
| 189 |
html_entity_decode( $post_edit_link ), |
| 190 |
html_entity_decode( $media_link ) |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/*================================ |
| 197 |
* Create CSV File and AJAX it back |
| 198 |
*=================================*/ |
| 199 |
$rows = array( implode( ',', [ |
| 200 |
'post id', |
| 201 |
'post_type', |
| 202 |
'page title', |
| 203 |
'page url', |
| 204 |
'image url', |
| 205 |
'image alt tag', |
| 206 |
'edit link', |
| 207 |
'media library link' |
| 208 |
] |
| 209 |
) ); |
| 210 |
|
| 211 |
foreach( $finds as $find ) { |
| 212 |
$rows[] = implode( ',', self::quoteIfComma( $find ) ); |
| 213 |
} |
| 214 |
|
| 215 |
$csvString = implode( '\r\n', $rows ); |
| 216 |
echo $csvString; |
| 217 |
die(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
add_action( 'plugins_loaded', array( 'NerdpressAltTextTools', 'init' ) ); |
| 222 |
|