PageRenderTime 73ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/bladencountyrecords/functions_syc.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 398 lines | 270 code | 75 blank | 53 comment | 56 complexity | 60b66eade604d13a9a03b615038a4177 MD5 | raw file
  1. <?php
  2. // custom constant (opposite of STYLESHEETPATH)
  3. define('_TEMPLATEURL', get_bloginfo('stylesheet_directory') );
  4. function syc_sample_metaboxes( $meta_boxes ) {
  5. /* $meta_boxes[] = array(
  6. 'id' => 'test_metabox',
  7. 'title' => 'Test Metabox',
  8. 'pages' => array('page'), // post type
  9. 'context' => 'side',
  10. 'priority' => 'default',
  11. 'show_names' => true, // Show field names on the left
  12. 'fields' => array(
  13. array(
  14. 'name' => 'Test Text',
  15. 'desc' => 'field description (optional)',
  16. 'id' => 'test_text',
  17. 'type' => 'text'
  18. ),
  19. ),
  20. ); */
  21. return $meta_boxes;
  22. }
  23. add_filter( 'cmb_meta_boxes', 'syc_sample_metaboxes' );
  24. // Initialize the metabox class
  25. add_action( 'init', 'syc_initialize_cmb_meta_boxes', 9999 );
  26. function syc_initialize_cmb_meta_boxes() {
  27. if ( !class_exists( 'cmb_Meta_Box' ) ) {
  28. require_once( locate_template('inc/metaboxes/init.php' ) );
  29. }
  30. }
  31. /**
  32. * error logs a variable in a parseable format while maintaining whitespace
  33. * in html
  34. * @param $var
  35. */
  36. function clog($var){
  37. error_log(var_export($var, TRUE));
  38. }
  39. // thumbnails
  40. add_theme_support( 'post-thumbnails' );
  41. // Admin CSS layer
  42. if ( !function_exists('base_admin_css') ) {
  43. function base_admin_css()
  44. {
  45. wp_enqueue_style('base-admin-css', _TEMPLATEURL .'/admin.css', false, '1.0', 'all');
  46. }
  47. add_action('admin_print_styles', 'base_admin_css');
  48. }
  49. // Body class for admin
  50. if ( !function_exists('base_admin_body_class') ) {
  51. function base_admin_body_class( $classes )
  52. {
  53. // Current action
  54. if ( is_admin() && isset($_GET['action']) ) {
  55. $classes .= 'action-'.$_GET['action'];
  56. }
  57. // Current post ID
  58. if ( is_admin() && isset($_GET['post']) ) {
  59. $classes .= ' ';
  60. $classes .= 'post-'.$_GET['post'];
  61. }
  62. // New post type & listing page
  63. if ( isset($_GET['post_type']) ) $post_type = $_GET['post_type'];
  64. if ( isset($post_type) ) {
  65. $classes .= ' ';
  66. $classes .= 'post-type-'.$post_type;
  67. }
  68. // Editting a post type
  69. $post_query = $_GET['post'];
  70. if ( isset($post_query) ) {
  71. $current_post_edit = get_post($post_query);
  72. $current_post_type = $current_post_edit->post_type;
  73. if ( !empty($current_post_type) ) {
  74. $classes .= ' ';
  75. $classes .= 'post-type-'.$current_post_type;
  76. }
  77. }
  78. // Return the $classes array
  79. return $classes;
  80. }
  81. add_filter('admin_body_class', 'base_admin_body_class');
  82. }
  83. /** adds tinymce editors to any fields with '.use-tinymce' class in admin **/
  84. //important: note the priority of 99, the js needs to be placed after tinymce loads
  85. add_action('admin_print_footer_scripts','my_admin_print_footer_scripts',99);
  86. function my_admin_print_footer_scripts()
  87. {
  88. ?><script type="text/javascript">/* <![CDATA[ */
  89. jQuery(function($)
  90. {
  91. var i=1;
  92. $('.use-tinymce textarea').each(function(e)
  93. {
  94. var id = $(this).attr('id');
  95. if (!id)
  96. {
  97. id = 'customEditor-' + i++;
  98. $(this).attr('id',id);
  99. }
  100. tinyMCE.execCommand('mceAddControl', false, id);
  101. });
  102. });
  103. var activeTinyEditor = '';
  104. jQuery( document ).ready( function(){
  105. jQuery( '.custom_upload_buttons a' ).live( 'click', function(e){
  106. var id = jQuery( e.target )
  107. .closest( 'td' )
  108. .find( '.use-tinymce textarea' )
  109. .attr( 'id' );
  110. activeTinyEditor = id;
  111. });
  112. if ( parent != self )
  113. {
  114. if ( typeof parent.tinyMCE != 'undefined' && parent.tinyMCE.activeEditor ) {
  115. parent.tinyMCE.get( parent.activeTinyEditor ).focus();
  116. parent.tinyMCE.activeEditor.windowManager.bookmark = parent.tinyMCE.activeEditor.selection.getBookmark('simple');
  117. }
  118. }
  119. });
  120. /* ]]> */</script><?php
  121. }
  122. add_filter('mce_buttons','prc_mce_buttons');
  123. function prc_mce_buttons($mce_buttons) {
  124. $mce_buttons[] = 'code';
  125. return $mce_buttons;
  126. }
  127. function meta($the_field, $single=true){
  128. global $post;
  129. $the_meta = get_post_meta($post->ID, $the_field, $single);
  130. return $the_meta;
  131. }
  132. /** add first & last classes to menu items **/
  133. function syc_add_first_and_last($output) {
  134. $output = preg_replace('/class="menu-item/', 'class="first menu-item', $output, 1);
  135. $output = substr_replace($output, 'class="last menu-item', strripos($output, 'class="menu-item'),
  136. strlen('class="menu-item'));
  137. return $output;
  138. }
  139. add_filter('wp_nav_menu', 'syc_add_first_and_last');
  140. function syc_add_markup_categories($output) {
  141. $output= preg_replace('/cat-item/', ' first cat-item', $output, 1);
  142. $output=substr_replace($output, " last cat-item", strripos($output, "cat-item"), strlen("cat-item"));
  143. return $output;
  144. }
  145. add_filter('wp_list_categories', 'syc_add_markup_categories');
  146. function devinsays_translation_mangler($translation, $text, $domain) {
  147. global $post;
  148. if ($post->post_type == 'retreat' || $post->post_type == 'workshop' ) {
  149. $translations = &get_translations_for_domain( $domain);
  150. if ( $text == 'Scheduled for: <b>%1$s</b>') {
  151. return $translations->translate( 'Retreat Date: <b>%1$s</b>' );
  152. }
  153. if ( $text == 'Published on: <b>%1$s</b>') {
  154. return $translations->translate( 'Retreat Date: <b>%1$s</b>' );
  155. }
  156. if ( $text == 'Publish <b>immediately</b>') {
  157. return $translations->translate( 'Retreat Date: <b>%1$s</b>' );
  158. }
  159. }
  160. return $translation;
  161. }
  162. // Show Scheduled Posts
  163. function devinsays_show_scheduled_posts($posts) {
  164. global $wp_query, $wpdb;
  165. if(is_single() && $wp_query->post_count == 0) {
  166. $posts = $wpdb->get_results($wp_query->request);
  167. }
  168. return $posts;
  169. }
  170. add_filter('the_posts', 'devinsays_show_scheduled_posts');
  171. add_filter('gettext', 'devinsays_translation_mangler', 10, 4);
  172. add_action('save_post', 'metabox_save');
  173. // parses woo_metaboxes and adds <p> tags via wpautop to tinymce fields
  174. function metabox_save($post_id) {
  175. global $woo_metaboxes;
  176. $woo_metaboxes = woo_metaboxes_add(get_option('woo_custom_template'));
  177. // check autosave
  178. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  179. return $post_id;
  180. }
  181. // check permissions
  182. if ('page' == $_POST['post_type']) {
  183. if (!current_user_can('edit_page', $post_id)) {
  184. return $post_id;
  185. }
  186. } elseif (!current_user_can('edit_post', $post_id)) {
  187. return $post_id;
  188. }
  189. foreach ($woo_metaboxes as $metabox) {
  190. $key = $metabox['name'];
  191. $old = get_post_meta($post_id, $key, true);
  192. $new = $_POST[$key];
  193. if( in_array($metabox['type'], array('tinymce', 'textarea'))){
  194. $new = wpautop($new);
  195. }
  196. if ($new && $new != $old) {
  197. update_post_meta($post_id, $key, $new);
  198. } elseif ('' == $new && $old) {
  199. delete_post_meta($post_id, $key, $old);
  200. }
  201. }
  202. }
  203. // Add all custom post types to the "Right Now" box on the Dashboard
  204. add_action( 'right_now_content_table_end' , 'syc_right_now_content_table_end' );
  205. function syc_right_now_content_table_end() {
  206. $args = array(
  207. 'show_ui' => true,
  208. '_builtin' => false
  209. );
  210. $output = 'object';
  211. $operator = 'and';
  212. $post_types = get_post_types( $args , $output , $operator );
  213. foreach( $post_types as $post_type ) {
  214. $num_posts = wp_count_posts( $post_type->name );
  215. $num = number_format_i18n( $num_posts->publish );
  216. $text = _n( $post_type->labels->singular_name, $post_type->labels->name , intval( $num_posts->publish ) );
  217. if ( current_user_can( 'edit_posts' ) ) {
  218. $num = "<a href='edit.php?post_type=$post_type->name'>$num</a>";
  219. $text = "<a href='edit.php?post_type=$post_type->name'>$text</a>";
  220. }
  221. echo '<tr><td class="first b b-' . $post_type->name . '">' . $num . '</td>';
  222. echo '<td class="t ' . $post_type->name . '">' . $text . '</td></tr>';
  223. }
  224. }
  225. // add slugs to body classes
  226. add_filter( 'body_class', 'syc_add_body_class_slug' );
  227. function syc_add_body_class_slug( $classes )
  228. {
  229. global $post;
  230. if ( isset( $post ) ) {
  231. $classes[] = $post->post_type . '-' . $post->post_name;
  232. }
  233. return $classes;
  234. }
  235. function syc_get_video_info($url){
  236. error_log('getting video info:' . $url);
  237. if(preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches) != 0){
  238. $video_id = $matches[0];
  239. error_log(print_r($matches, true));
  240. error_log("got yotube video id: $video_id");
  241. // YouTube - get the corresponding thumbnail images
  242. if($video_id != ''){
  243. $video_thumb = "http://img.youtube.com/vi/".$video_id."/0.jpg";
  244. $video_embed = '<iframe width="560" height="315" src="http://www.youtube.com/embed/'.$video_id.'" frameborder="0" allowfullscreen></iframe>';
  245. $result = array('video_thumb'=>$video_thumb, 'video_embed'=>$video_embed);
  246. return $result;
  247. }
  248. }elseif(preg_match('#vimeo\.com\/(\d+)#', $url, $matches) != 0){
  249. $video_id = $matches[1];
  250. error_log("got vimeo id: $video_id");
  251. if($video_id != ''){
  252. $hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video_id.php"));
  253. error_log($hash[0]['thumbnail_medium']);
  254. $video_thumb = $hash[0]['thumbnail_medium'];
  255. $video_embed = '<iframe src="http://player.vimeo.com/video/'. $video_id.'?title=0&amp;byline=0&amp;portrait=0&amp;color=fc575e" width="560" height="315" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
  256. $result = array('video_thumb'=>$video_thumb, 'video_embed'=>$video_embed);
  257. return $result;
  258. }
  259. }
  260. error_log("got no id: $url");
  261. return false;
  262. }
  263. function syc_copy_post_image($url, $post_id){
  264. $time = current_time('mysql');
  265. if ( $post = get_post($post_id) ) {
  266. if ( substr( $post->post_date, 0, 4 ) > 0 )
  267. $time = $post->post_date;
  268. }
  269. //making sure there is a valid upload folder
  270. if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
  271. return false;
  272. error_log('finding basename in syc post copy:' . $url);
  273. $name = basename($url);
  274. $filename = wp_unique_filename($uploads['path'], $name);
  275. // Move the file to the uploads dir
  276. $new_file = $uploads['path'] . "/$filename";
  277. $uploaddir = wp_upload_dir();
  278. $path = str_replace($uploaddir["baseurl"], $uploaddir["basedir"], $url);
  279. error_log('copying to: ' . $path);
  280. if(!copy($path, $new_file))
  281. return false;
  282. // Set correct file permissions
  283. $stat = stat( dirname( $new_file ));
  284. $perms = $stat['mode'] & 0000666;
  285. @ chmod( $new_file, $perms );
  286. // Compute the URL
  287. $url = $uploads['url'] . "/$filename";
  288. if ( is_multisite() )
  289. delete_transient( 'dirsize_cache' );
  290. $type = wp_check_filetype($new_file);
  291. return array("file" => $new_file, "url" => $url, "type" => $type["type"]);
  292. }
  293. add_action('admin_init', 'syc_remove_dashboard_widgets');
  294. function syc_remove_dashboard_widgets() {
  295. // remove_meta_box('dashboard_right_now', 'dashboard', 'normal'); // right now
  296. remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); // recent comments
  297. // remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal'); // incoming links
  298. remove_meta_box('dashboard_plugins', 'dashboard', 'normal'); // plugins
  299. remove_meta_box('dashboard_quick_press', 'dashboard', 'normal'); // quick press
  300. // remove_meta_box('dashboard_recent_drafts', 'dashboard', 'normal'); // recent drafts
  301. remove_meta_box('dashboard_primary', 'dashboard', 'normal'); // wordpress blog
  302. remove_meta_box('dashboard_secondary', 'dashboard', 'normal'); // other wordpress news
  303. }
  304. function add_or_update_post_meta($post_id, $key, $value)
  305. {
  306. add_post_meta($post_id, $key, $value, true) or update_post_meta($post_id, $key, $value);
  307. }
  308. /**
  309. * Set the latest attachment as the featured image of the given post
  310. **/
  311. function syc_reset_featured_image($post_id){
  312. // get the last image added to the post
  313. $attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));
  314. if(sizeof($attachments) > 0){
  315. // set image as the post thumbnail
  316. set_post_thumbnail($post_id, $attachments[0]->ID);
  317. }
  318. }
  319. function modify_footer_admin () {
  320. echo 'Created by <a href="http://switchyardcreative.com">Switchyard Creative</a>. &nbsp;';
  321. echo 'Powered by <a href="http://WordPress.org">WordPress</a>.';
  322. }
  323. add_filter('admin_footer_text', 'modify_footer_admin');