postie-helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /* $Id: postie-helper.php,v 1.2 2018/04/17 11:43:22 springm Exp springm $ */
  3. /* $Log: postie-helper.php,v $
  4. * Revision 1.2 2018/04/17 11:43:22 springm
  5. * Summary: Funktioniert
  6. * */
  7. /* ------------------- postie helper function for leftover categories --- */
  8. /* postie removes categories given in the mail subject if it finds them in the
  9. * wordpress categories.
  10. * all other categories will be made tags of the created post
  11. for this to work, postie-settings->message->remove matched categories has to be set to no
  12. */
  13. add_filter('postie_post_before', 'mws_auto_tag');
  14. function mws_auto_tag($post) {
  15. DebugEcho('============================== mws_auto_tag ' . print_r($post, true));
  16. // DebugEcho(' test for "common places" category');
  17. // if (in_category( 'Common Places', $post )) {
  18. // DebugEcho(' post has "common places" category');
  19. // }
  20. $post = evaluate_image_exif($post, $post['post_category']);
  21. $post['post_name'] = $post['post_title'];
  22. DebugEcho('========================== end mws_auto_tag');
  23. DebugEcho("========================== post_content before massage\n" . $post['post_content']);
  24. $post['post_content'] = preg_replace('!<div class="postie-attachments">!','', $post['post_content']);
  25. $post['post_content'] = preg_replace('!</div>!','', $post['post_content']);
  26. $post['post_content'] = preg_replace('!<p></p>!','', $post['post_content']);
  27. $post['post_content'] = preg_replace('!<br />!','', $post['post_content']);
  28. $post['post_content'] = preg_replace('!\n!','', $post['post_content']);
  29. $post['post_content'] = preg_replace('!&lt;!','<', $post['post_content']);
  30. $post['post_content'] = preg_replace('!&gt;!','>', $post['post_content']);
  31. DebugEcho("========================== post_content after massage\n" . $post['post_content']);
  32. $post = make_landscape_photos_large($post);
  33. return $post;
  34. }
  35. /* extract information from the exif metadata of the first image in the post.
  36. * if applicable create the title, categories and location information from this */
  37. function evaluate_image_exif( $post, $categories ) {
  38. DebugEcho('MWS =====> begin evaluate_image_exif');
  39. $common_places_found = 0;
  40. DebugEcho(' test for "common places" category');
  41. if (in_category( 'common places', $post )) {
  42. DebugEcho(' post has "common places" category');
  43. $common_places_found = 1;
  44. }
  45. $iimgs = get_attached_media('image', $post['ID']);
  46. $upload_dir = wp_upload_dir();
  47. foreach ( $iimgs as $iimg ) {
  48. $imagefile = $upload_dir['basedir'] . '/' .
  49. wp_get_attachment_metadata( get_post_thumbnail_id( $post['ID'] ) )['file'];
  50. break;
  51. }
  52. $meta = miech_wp_read_image_metadata( get_post_meta($post['ID']), $imagefile, $sourceImageType );
  53. if ( ! empty( $meta['title'] ) )
  54. $post['post_title'] = $meta['title'];
  55. DebugEcho('~~~~~~~~~~ ' . $post['post_title'] );
  56. preg_match_all('/\[(.*?)\]/', $post['post_title'], $leftovercats);
  57. print_r($leftovercats[1]);
  58. foreach ( array_merge( array( $meta['country'], $meta['state'], $meta['city'] ),
  59. explode(',', $meta['keywords']), $leftovercats[1] ) as $k) {
  60. /* DebugEcho(' category/tag: ' . $k); */
  61. $in_blacklist = 0;
  62. foreach ( array('wp', 'blog', 'status', 'genre', 'published', 'printed', 'lens', 'treatment' ) as $b ) {
  63. if ( strcasecmp( "$k", "$b" ) == 0 ) {
  64. $in_blacklist = 1;
  65. }
  66. }
  67. /* if ( $in_blacklist < 1 ) { */
  68. if ( $in_blacklist < 1 and strlen($k) > 0) {
  69. DebugEcho(" Examining '$k' " );
  70. if ( strcasecmp('common places', "$k") == 0) {
  71. DebugEcho(" '$k' GEFUNDEN!" );
  72. $common_places_found = 1;
  73. }
  74. $cat = get_term_by('name', (string) "$k", 'category');
  75. if ( isset($cat) ) {
  76. if ( $cat->term_id > 1 ) {
  77. DebugEcho(" Category '$k' exists with id " . $cat->term_id );
  78. array_push( $categories, $cat->term_id );
  79. } else {
  80. DebugEcho(" Category '$k' does not exist ==> '$k' becomes tag" );
  81. array_push($post['tags_input'], "$k");
  82. }
  83. }
  84. }
  85. }
  86. DebugEcho(" \$common_places_found ist $common_places_found: " . print_r($categories, true) );
  87. if ( $common_places_found == 0 ) {
  88. array_push( $categories, get_term_by('name', (string) "Vernacular", 'category')->term_id );
  89. } else {
  90. $c2 = \array_diff($categories, [1964]);
  91. $categories = $c2;
  92. DebugEcho(" 'vernacular' removed: " . print_r($categories, true) );
  93. }
  94. $post['post_category'] = $categories;
  95. if ( $meta['city'] )
  96. $post['post_content'] = mws_add_caption( $post['post_content'], $meta );
  97. /* remove leftover category strings from title */
  98. $post['post_title'] = preg_replace('/\[.*\]\s*/', '', $post['post_title']);
  99. DebugEcho( "MWS ===> end evaluate_image_exif " . $imagefile);
  100. return $post;
  101. }
  102. /* ------------------------------------------- exif related functions --- */
  103. function miech_wp_read_image_metadata( $meta, $file, $sourceImageType ) {
  104. DebugEcho('MWS ===> beginning miech_wp_read_image_metadata');
  105. $newmeta = array();
  106. // read iptc first, since it might contain data not available in exif such
  107. // as caption, description etc
  108. getimagesize( $file, $info );
  109. if ( ! empty( $info['APP13'] ) ) {
  110. $iptc = iptcparse( $info['APP13'] );
  111. $newmeta['title'] = trim( $iptc['2#105'][0] );
  112. $newmeta['country'] = trim( $iptc['2#101'][0] );
  113. $newmeta['state'] = trim( $iptc['2#095'][0] );
  114. $newmeta['city'] = trim( $iptc['2#090'][0] );
  115. $newmeta['location'] = trim( $iptc['2#092'][0] );
  116. $keywordcount = count($iptc["2#025"]);
  117. for ( $i = 0; $i < $keywordcount; $i++ ) {
  118. if ( strlen($newmeta['keywords']) > 0 )
  119. $newmeta['keywords'] .= ',' ;
  120. $newmeta['keywords'] .= $iptc["2#025"][$i];
  121. }
  122. }
  123. $exif = @exif_read_data( $file );
  124. /* DebugEcho('Exif' . print_r($exif)); */
  125. DebugEcho('GPSLatitude is ' . $exif['GPSLatitude']);
  126. if ( is_numeric($exif['GPSLatitude']) )
  127. DebugEcho('GPSLatitude is numeric ' );
  128. else
  129. DebugEcho('GPSLatitude is not numeric ' );
  130. if ( ! empty($exif['GPSLatitude']))
  131. $newmeta['latitude'] = geo_single_fracs2dec($exif['GPSLatitude'] );
  132. if ( ! empty($exif['GPSLongitude']))
  133. $newmeta['longitude'] = geo_single_fracs2dec($exif['GPSLongitude']) ;
  134. if ( ! empty($exif['GPSLatitudeRef']))
  135. if ( $exif['GPSLatitudeRef'] == 'S' )
  136. $newmeta['latitude'] = '-' . $newmeta['latitude'];
  137. if ( ! empty($exif['GPSLongitudeRef']))
  138. if ( $exif['GPSLongitudeRef'] == 'W' )
  139. $newmeta['longitude'] = '-' . $newmeta['longitude'];
  140. foreach ( array( 'city', 'location') as $key ) {
  141. if ( $newmeta[ $key ] && ! seems_utf8( $newmeta[ $key ] ) )
  142. $newmeta[ $key ] = utf8_encode( $newmeta[ $key ] );
  143. }
  144. $meta = array_merge($meta, $newmeta);
  145. DebugEcho('MWS ===> end miech_wp_read_image_metadata');
  146. return $meta;
  147. }
  148. function geo_frac2dec($str) {
  149. @list( $n, $d ) = explode( '/', $str );
  150. if ( !empty($d) )
  151. return $n / $d;
  152. return $str;
  153. }
  154. function geo_single_fracs2dec($fracs) {
  155. return geo_frac2dec($fracs[0]) +
  156. geo_frac2dec($fracs[1]) / 60 +
  157. geo_frac2dec($fracs[2]) / 3600;
  158. }
  159. function mws_add_caption( $s, $meta ) {
  160. DebugEcho('MWS ===> begin mws_add_caption');
  161. $alt = $meta['title'];
  162. $caption = "</div>\n<p class='mycaption'>";
  163. if ( $meta['location'] ) {
  164. $caption = $caption . $meta['location'] . ', ';
  165. $alt = $alt . ', ' . $meta['location'];
  166. }
  167. $caption = $caption . $meta['city'];
  168. $alt = $alt . ', ' . $meta['city'];
  169. if ( $meta['longitude'] ) {
  170. $caption = $caption . ' (<a href="http://www.openstreetmap.org/?mlat=' . $meta['latitude'];
  171. $caption = $caption . ';&mlon=' . $meta['longitude'] . '&zoom=14&q=mapquest" target="_blank">OSM</a>)';
  172. $alt = $alt . ', geotagged';
  173. }
  174. $caption = $caption . "</p>\n";
  175. $keywords = $meta['keywords'];
  176. DebugEcho('Keywords: ' . $keywords);
  177. $keywords = preg_replace('/(WP|Lens,|Status,|Blog,|Published,|Treatment,|)/s', '', $keywords);
  178. $keywords = preg_replace('/"/s', "'", $keywords);
  179. $keywords = preg_replace('/,/s', ', ', $keywords);
  180. $keywords = preg_replace('/, $/', '', $keywords);
  181. DebugEcho($keywords);
  182. $alt = $alt . ', ' . $keywords;
  183. DebugEcho($alt);
  184. $s = preg_replace('/<br \/>\s*<\/div>/ims', $caption, $s, 1);
  185. $s = preg_replace('/alt=""/ims', 'alt="' . $alt . '"', $s, 1);
  186. DebugEcho($s);
  187. DebugEcho('MWS ===> end mws_add_caption');
  188. return $s;
  189. }
  190. /* add the categories of the postie post to all attached images */
  191. add_filter('postie_post_after', 'mws_postie_add_cats_to_images'); /* MWS now */
  192. function mws_postie_add_cats_to_images( $post ) {
  193. $cats = $post['post_category'];
  194. $imgs = get_attached_media('image', $post['ID']);
  195. $flag = 0;
  196. foreach ($cats as $c) {
  197. /* DebugEcho( $c ); */
  198. $cat = get_category($c);
  199. /* DebugEcho( print_r($cat) ); */
  200. if ( $cat->cat_ID > 1 ) {
  201. foreach ( $imgs as $img ) {
  202. wp_set_post_categories( $img->ID, $cat->cat_ID, true );
  203. }
  204. }
  205. $flag = 1;
  206. }
  207. if ( $flag > 0 ) {
  208. wp_set_post_categories( $img->ID, get_cat_ID('no_user'), true );
  209. }
  210. return $post;
  211. }
  212. function make_landscape_photos_large( $post ) {
  213. DebugEcho("make_landscape_photos_large 1 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" . $post['post_content'] . "\n");
  214. preg_match('/ postie_dunit (\d+)pxx(\d+)px /', $post['post_content'], $size);
  215. DebugEcho( print_r($size) );
  216. if ( $size[1] > $size[2] ) {
  217. DebugEcho( "Landscape\n" );
  218. #<!-- postie_dunit 750pxx422px --><!-- wp:image {"id":36954,"sizeSlug":"large","linkDestination":"media"} --><figure class="wp-block-image size-large"><a href="https://markus-spring.info/wp-content/uploads/sites/2/2021/03/Ripples-6.jpg"><img src="https://markus-spring.info/wp-content/uploads/sites/2/2021/03/Ripples-6-1200x675.jpg" alt="Ripples.jpg" class="wp-image-36954"/></a><figcaption>Saalachau, Bad Reichenhall</figcaption></figure><!-- /wp:image -->
  219. #
  220. # <!-- wp:image {"align":"wide","id":36936,"sizeSlug":"large","linkDestination":"media"} --><figure class="wp-block-image alignwide size-large">
  221. $post['post_content'] = preg_replace('/{/', '{"align":"wide",', $post['post_content']);
  222. $post['post_content'] = preg_replace('/wp-block-image /','wp-block-image alignwide ', $post['post_content']);
  223. $post['post_content'] = preg_replace('/<!-- postie_dunit.*? -->\n*/', '', $post['post_content']);
  224. DebugEcho($post['post_content']);
  225. }
  226. return $post;
  227. }
  228. /* add_filter('postie_post_after', 'mws_postie_rotate_ios_images');
  229. function mws_postie_rotate_ios_images ( $post ) {
  230. $upload_dir = wp_upload_dir();
  231. foreach ( $iimgs as $iimg ) {
  232. $imagefile = $upload_dir['basedir'] . '/' .
  233. wp_get_attachment_metadata( get_post_thumbnail_id( $post['ID'] ) )['file'];
  234. DebugEcho("1. Image to rotate: " .$imagefile);
  235. correctImageOrientation($imagefile);
  236. }
  237. } */
  238. /* add_action('postie_file_added', 'mws_postie_fileadded', 10, 3); */ /* MWS now */
  239. /* function mws_postie_fileadded($postid, $attachmentid, $file_array) {
  240. DebugEcho('MWS ========> begin mws_postie_fileadded');
  241. $fileinfo = wp_check_filetype(get_attached_file($attachmentid));
  242. $upload_dir = wp_upload_dir()['basedir'];
  243. $meta = wp_get_attachment_metadata( $attachmentid );
  244. $mimetype = $fileinfo['type'];
  245. if (strpos($mimetype, 'image', 0) === 0) { //only if attachment is an image
  246. $file = $upload_dir . '/' . $meta['file'];
  247. set_title_from_exif_if_necessary($postid, $file, $meta);
  248. $subdir = dirname($meta['file']);
  249. $deg = correctImageOrientation($file);
  250. foreach ( array_keys($meta['sizes']) as $size ) {
  251. if ( $size != 'full' ) {
  252. $file = $upload_dir . '/' . $subdir . '/' . $meta['sizes'][$size]['file'];
  253. correctImageOrientation($file);
  254. }
  255. }
  256. }
  257. DebugEcho('MWS ========> end mws_postie_fileadded');
  258. }
  259. */
  260. ?>