1. cherryframework functions.php
  2. <?php
  3. /*
  4. * Set Proper Parent/Child theme paths for inclusion
  5. */
  6. @define( 'PARENT_DIR', get_template_directory() );
  7. @define( 'CHILD_DIR', get_stylesheet_directory() );
  8. @define( 'PARENT_URL', get_template_directory_uri() );
  9. @define( 'CHILD_URL', get_stylesheet_directory_uri() );
  10. @define( 'CURRENT_THEME', getCurrentTheme() );
  11. @define( 'FILE_WRITEABLE', is_writeable(PARENT_DIR.'/style.css'));
  12. /*
  13. * Variables array init
  14. *
  15. */
  16. $variablesArray = array(
  17. 'textColor' => '#000000',
  18. 'bodyBackground' => '#000000',
  19. 'baseFontFamily' => '#000000',
  20. 'baseFontSize' => '#000000',
  21. 'baseLineHeight' => '#000000',
  22. 'linkColor' => '#000000',
  23. 'linkColorHover' => '#000000'
  24. );
  25. /*
  26. * Definition current theme
  27. *
  28. */
  29. function getCurrentTheme() {
  30. if ( function_exists('wp_get_theme') ) {
  31. $theme = wp_get_theme();
  32. if ( $theme->exists() ) {
  33. $theme_name = $theme->Name;
  34. }
  35. } else {
  36. $theme_name = get_current_theme();
  37. }
  38. $theme_name = preg_replace("/\W/", "_", strtolower($theme_name) );
  39. return $theme_name;
  40. }
  41. /*
  42. * Comment some value from variables.less
  43. *
  44. */
  45. if ( CURRENT_THEME != 'cherry' )
  46. add_action('cherry_activation_hook', 'comment_child_var');
  47. function comment_child_var() {
  48. global $variablesArray;
  49. $file = CHILD_DIR .'/bootstrap/less/variables.less';
  50. if ( file_exists($file) ) {
  51. $allVariablessArray = file($file);
  52. foreach ($variablesArray as $key => $value) {
  53. foreach ($allVariablessArray as $k => $v) {
  54. $pos = strpos($v, $key);
  55. if ( $pos!==false && $pos == 1 ) {
  56. $allVariablessArray[$k] = '// ' . $v;
  57. break;
  58. }
  59. }
  60. }
  61. file_put_contents($file, $allVariablessArray);
  62. }
  63. }
  64. /*
  65. * Helper function to return the theme option value.
  66. * If no value has been saved, it returns $default.
  67. * Needed because options are saved as serialized strings.
  68. */
  69. if ( !function_exists( 'of_get_option' ) ) {
  70. function of_get_option($name, $default = false) {
  71. $optionsframework_settings = get_option('optionsframework');
  72. // Gets the unique option id
  73. $option_name = $optionsframework_settings['id'];
  74. if ( get_option($option_name) ) {
  75. $options = get_option($option_name);
  76. }
  77. if ( isset($options[$name]) ) {
  78. return $options[$name];
  79. } else {
  80. return $default;
  81. }
  82. }
  83. }
  84. /*
  85. * Unlink less cache files
  86. */
  87. add_action('cherry_activation_hook', 'clean_less_cache');
  88. function clean_less_cache() {
  89. if ( CURRENT_THEME == 'cherry' ) {
  90. $bootstrapInput = PARENT_DIR .'/less/bootstrap.less';
  91. $themeInput = PARENT_DIR .'/less/style.less';
  92. } else {
  93. $bootstrapInput = CHILD_DIR .'/bootstrap/less/bootstrap.less';
  94. $themeInput = CHILD_DIR .'/style.less';
  95. }
  96. $cacheFile1 = $bootstrapInput.".cache";
  97. $cacheFile2 = $themeInput.".cache";
  98. if (file_exists($cacheFile1)) unlink($cacheFile1);
  99. if (file_exists($cacheFile2)) unlink($cacheFile2);
  100. }
  101. if ( (is_admin() && ($pagenow == "themes.php")) && FILE_WRITEABLE ) {
  102. do_action('cherry_activation_hook');
  103. }
  104. /*
  105. * Loading theme textdomain
  106. */
  107. if ( !function_exists('cherry_theme_setup')) {
  108. function cherry_theme_setup() {
  109. load_theme_textdomain( CURRENT_THEME, PARENT_DIR . '/languages' );
  110. }
  111. add_action('after_setup_theme', 'cherry_theme_setup');
  112. }
  113. include_once (PARENT_DIR . '/includes/locals.php');
  114. // WPML compatibility
  115. // WPML filter for correct posts IDs for the current language Solution
  116. if ( function_exists( 'wpml_get_language_information' )) {
  117. update_option('suppress_filters', 0);
  118. } else {
  119. update_option('suppress_filters', 1);
  120. }
  121. // Register Flickr and recent posts widgets link label for translation
  122. function wpml_link_text_filter( $link_text, $widget_title ) {
  123. icl_translate( 'cherry', 'link_text_' . $widget_title, $link_text );
  124. }
  125. // Check if WPML is activated
  126. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
  127. add_filter( 'widget_linktext', 'wpml_link_text_filter', 10, 2 );
  128. }
  129. //Loading Custom function
  130. include_once (CHILD_DIR . '/includes/custom-function.php');
  131. //Loading jQuery and Scripts
  132. include_once (PARENT_DIR . '/includes/theme-scripts.php');
  133. //Widget and Sidebar
  134. include_once (CHILD_DIR . '/includes/sidebar-init.php');
  135. include_once (PARENT_DIR . '/includes/register-widgets.php');
  136. include_once (PARENT_DIR . '/includes/widgets/widgets-manager.php');
  137. //Theme initialization
  138. include_once (CHILD_DIR . '/includes/theme-init.php');
  139. //Additional function
  140. include_once (PARENT_DIR . '/includes/theme-function.php');
  141. //Shortcodes
  142. include_once (PARENT_DIR . '/includes/theme_shortcodes/columns.php');
  143. include_once (PARENT_DIR . '/includes/theme_shortcodes/shortcodes.php');
  144. include_once (PARENT_DIR . '/includes/theme_shortcodes/posts_grid.php');
  145. include_once (PARENT_DIR . '/includes/theme_shortcodes/posts_list.php');
  146. include_once (PARENT_DIR . '/includes/theme_shortcodes/mini_posts_list.php');
  147. include_once (PARENT_DIR . '/includes/theme_shortcodes/mini_posts_grid.php');
  148. include_once (PARENT_DIR . '/includes/theme_shortcodes/alert.php');
  149. include_once (PARENT_DIR . '/includes/theme_shortcodes/tabs.php');
  150. include_once (PARENT_DIR . '/includes/theme_shortcodes/toggle.php');
  151. include_once (PARENT_DIR . '/includes/theme_shortcodes/html.php');
  152. include_once (PARENT_DIR . '/includes/theme_shortcodes/misc.php');
  153. include_once (PARENT_DIR . '/includes/theme_shortcodes/service_box.php');
  154. include_once (PARENT_DIR . '/includes/theme_shortcodes/post_cycle.php');
  155. include_once (PARENT_DIR . '/includes/theme_shortcodes/carousel.php');
  156. include_once (PARENT_DIR . '/includes/theme_shortcodes/progressbar.php');
  157. include_once (PARENT_DIR . '/includes/theme_shortcodes/banner.php');
  158. include_once (PARENT_DIR . '/includes/theme_shortcodes/table.php');
  159. include_once (PARENT_DIR . '/includes/theme_shortcodes/hero_unit.php');
  160. include_once (PARENT_DIR . '/includes/theme_shortcodes/roundabout.php');
  161. include_once (PARENT_DIR . '/includes/theme_shortcodes/categories.php');
  162. include_once (PARENT_DIR . '/includes/theme_shortcodes/media.php');
  163. //tinyMCE includes
  164. include_once (PARENT_DIR . '/includes/theme_shortcodes/tinymce/tinymce_shortcodes.php');
  165. //Aqua Resizer for image cropping and resizing on the fly
  166. include_once (PARENT_DIR . '/includes/aq_resizer.php');
  167. // Add the pagemeta
  168. include_once (PARENT_DIR . '/includes/theme-pagemeta.php');
  169. // Add the postmeta
  170. include_once (PARENT_DIR . '/includes/theme-postmeta.php');
  171. // Add the postmeta to Portfolio posts
  172. include_once (PARENT_DIR . '/includes/theme-portfoliometa.php');
  173. // Add the postmeta to Slider posts
  174. include_once (PARENT_DIR . '/includes/theme-slidermeta.php');
  175. // Add the postmeta to Testimonials
  176. include_once (PARENT_DIR . '/includes/theme-testimeta.php');
  177. // Add the postmeta to Our Team posts
  178. include_once (PARENT_DIR . '/includes/theme-teammeta.php');
  179. //Loading options.php for theme customizer
  180. include_once (CHILD_DIR . '/options.php');
  181. include_once (PARENT_DIR . '/framework_options.php');
  182. //Plugin Activation
  183. include_once (CHILD_DIR . '/includes/class-tgm-plugin-activation.php');
  184. include_once (CHILD_DIR . '/includes/register-plugins.php');
  185. // Framework Data Management
  186. include_once (PARENT_DIR . '/admin/data_management/data_management_interface.php');
  187. // SEO Settings
  188. include_once (PARENT_DIR . '/admin/seo/seo_settings_page.php');
  189. // WP Pointers
  190. include_once (PARENT_DIR . '/includes/class.wp-help-pointers.php');
  191. // Embedding LESS compile
  192. if ( !class_exists('lessc') ) {
  193. include_once (PARENT_DIR .'/includes/lessc.inc.php');
  194. }
  195. include_once (PARENT_DIR .'/includes/less-compile.php');
  196. // include shop
  197. function include_shop(){
  198. if ( file_exists(get_stylesheet_directory().'/shop.php') ) {
  199. include_once (CHILD_DIR . '/shop.php');
  200. }
  201. }
  202. add_action('after_setup_theme', 'include_shop');
  203. // removes detailed login error information for security
  204. add_filter('login_errors',create_function('$a', "return null;"));
  205. /*
  206. * Loads the Options Panel
  207. *
  208. * If you're loading from a child theme use stylesheet_directory
  209. * instead of template_directory
  210. */
  211. if ( !function_exists( 'optionsframework_init' ) ) {
  212. define( 'OPTIONS_FRAMEWORK_DIRECTORY', PARENT_URL . '/admin/' );
  213. include_once dirname( __FILE__ ) . '/admin/options-framework.php';
  214. }
  215. /*
  216. * Removes Trackbacks from the comment cout
  217. *
  218. */
  219. if (!function_exists('comment_count')) {
  220. add_filter('get_comments_number', 'comment_count', 0);
  221. function comment_count( $count ) {
  222. if ( ! is_admin() ) {
  223. global $id;
  224. $comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
  225. return count($comments_by_type['comment']);
  226. } else {
  227. return $count;
  228. }
  229. }
  230. }
  231. /*
  232. * Post Formats
  233. *
  234. */
  235. $formats = array(
  236. 'aside',
  237. 'gallery',
  238. 'link',
  239. 'image',
  240. 'quote',
  241. 'audio',
  242. 'video');
  243. add_theme_support( 'post-formats', $formats );
  244. add_post_type_support( 'post', 'post-formats' );
  245. /*
  246. * Custom excpert length
  247. *
  248. */
  249. if(!function_exists('new_excerpt_length')) {
  250. function new_excerpt_length($length) {
  251. return 60;
  252. }
  253. add_filter('excerpt_length', 'new_excerpt_length');
  254. }
  255. // enable shortcodes in sidebar
  256. add_filter('widget_text', 'do_shortcode');
  257. // custom excerpt ellipses for 2.9+
  258. if(!function_exists('custom_excerpt_more')) {
  259. function custom_excerpt_more($more) {
  260. return theme_locals("read_more").' &raquo;';
  261. }
  262. add_filter('excerpt_more', 'custom_excerpt_more');
  263. }
  264. // no more jumping for read more link
  265. if(!function_exists('no_more_jumping')) {
  266. function no_more_jumping($post) {
  267. return '&nbsp;<a href="'.get_permalink().'" class="read-more">'.theme_locals("continue_reading").'</a>';
  268. }
  269. add_filter('excerpt_more', 'no_more_jumping');
  270. }
  271. // category id in body and post class
  272. if(!function_exists('category_id_class')) {
  273. function category_id_class($classes) {
  274. global $post;
  275. foreach((get_the_category()) as $category)
  276. $classes [] = 'cat-' . $category->cat_ID . '-id';
  277. return $classes;
  278. }
  279. add_filter('post_class', 'category_id_class');
  280. add_filter('body_class', 'category_id_class');
  281. }
  282. // Threaded Comments
  283. if(!function_exists('enable_threaded_comments')) {
  284. function enable_threaded_comments() {
  285. if (!is_admin()) {
  286. if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
  287. wp_enqueue_script('comment-reply');
  288. }
  289. }
  290. }
  291. add_action('get_header', 'enable_threaded_comments');
  292. }
  293. //remove auto loading rel=next post link in header
  294. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
  295. /*
  296. * Navigation with description
  297. *
  298. */
  299. if (! class_exists('description_walker')) {
  300. class description_walker extends Walker_Nav_Menu {
  301. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  302. global $wp_query;
  303. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  304. $class_names = $value = '';
  305. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  306. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  307. $class_names = ' class="'. esc_attr( $class_names ) . '"';
  308. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  309. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  310. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  311. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  312. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  313. $description = ! empty( $item->description ) ? '<span class="desc">'.esc_attr( $item->description ).'</span>' : '';
  314. if($depth != 0) {
  315. $description = $append = $prepend = "";
  316. }
  317. $item_output = $args->before;
  318. $item_output .= '<a'. $attributes .'>';
  319. $item_output .= $args->link_before;
  320. if (isset($prepend))
  321. $item_output .= $prepend;
  322. $item_output .= apply_filters( 'the_title', $item->title, $item->ID );
  323. if (isset($append))
  324. $item_output .= $append;
  325. $item_output .= $description.$args->link_after;
  326. $item_output .= '</a>';
  327. $item_output .= $args->after;
  328. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  329. }
  330. }
  331. }
  332. /*
  333. * Cherry update
  334. */
  335. $cherryTemplates = array(
  336. '404.php',
  337. 'archive.php',
  338. 'author.php',
  339. 'category.php',
  340. 'index.php',
  341. 'page.php',
  342. 'page-archives.php',
  343. 'page-faq.php',
  344. 'page-fullwidth.php',
  345. 'page-home.php',
  346. 'page-Portfolio2Cols-filterable.php',
  347. 'page-Portfolio3Cols-filterable.php',
  348. 'page-Portfolio4Cols-filterable.php',
  349. 'page-testi.php',
  350. 'search.php',
  351. 'single.php',
  352. 'single-portfolio.php',
  353. 'single-team.php',
  354. 'single-testi.php',
  355. 'tag.php'
  356. );
  357. $headerFooterPattern = '/(\<header.*?\>.*?\<\/header\>|\<footer.*?\>.*?\<\/footer\>)/is';
  358. if (is_user_logged_in() && current_user_can('update_themes')) {
  359. $updateErrors = array();
  360. $oldCherryVersion = get_option('cherry_version', '1.0');
  361. $currentCherryVersion = getCherryVersion($updateErrors);
  362. $cherryV2 = '2.0';
  363. $cherryForceUpdate = (bool) get_option('cherry_force_update', false);
  364. if (version_compare($oldCherryVersion, $cherryV2) == -1 || $cherryForceUpdate) {
  365. writeLog(PHP_EOL . date('Y-m-d H:i:s'));
  366. writeLog('Old CherryFramework version: ' . $oldCherryVersion);
  367. writeLog('Current CherryFramework version: ' . $currentCherryVersion);
  368. writeLog('Force update: ' . (int) $cherryForceUpdate);
  369. cherryUpdate($cherryTemplates, $headerFooterPattern, $updateErrors);
  370. //changeChildTheme
  371. changeChildTheme($updateErrors);
  372. //end changeChildTheme
  373. if (empty($updateErrors)) {
  374. update_option('cherry_version', $currentCherryVersion);
  375. update_option('cherry_force_update', 0);
  376. }
  377. }
  378. }
  379. function getCherryVersion(&$updateErrors) {
  380. $style = TEMPLATEPATH . '/style.css';
  381. $themeVersion = 0;
  382. if (function_exists('wp_get_theme')) {
  383. $theme = wp_get_theme(get_option('template', 'CherryFramework'));
  384. if (file_exists($style) && $theme->exists()) {
  385. $themeVersion = $theme->Version;
  386. } else {
  387. $updateErrors[] = $style . ' does not exist';
  388. writeLog($style . ' does not exist');
  389. }
  390. } elseif (function_exists('get_theme_data')) {
  391. if (file_exists($style)) {
  392. $theme = get_theme_data($style);
  393. $themeVersion = $theme['Version'];
  394. } else {
  395. $updateErrors[] = $style . ' does not exist';
  396. writeLog($style . ' does not exist');
  397. }
  398. } else {
  399. if (file_exists($style)) {
  400. $content = file_get_contents($style);
  401. if ($content) {
  402. $pattern = '/\s*version\s*:\s*([^\n]+)\s*/is';
  403. preg_match($pattern, $content, $matches);
  404. if (!empty($matches[1])) {
  405. $themeVersion = trim($matches[1]);
  406. }
  407. } else {
  408. $updateErrors[] = 'Failed to read ' . $style;
  409. writeLog('Failed to read ' . $style);
  410. }
  411. } else {
  412. $updateErrors[] = $style . ' does not exist';
  413. writeLog($style . ' does not exist');
  414. }
  415. }
  416. return $themeVersion;
  417. }
  418. function isCherryChildTheme($themePath, $name, &$updateErrors) {
  419. $style = $themePath . '/style.css';
  420. $themeTemplate = false;
  421. if (function_exists('wp_get_theme')) {
  422. $theme = wp_get_theme($name);
  423. if ($theme->exists()) {
  424. if ($theme->Stylesheet != $theme->Template) {
  425. $themeTemplate = $theme->Template;
  426. }
  427. } else {
  428. $updateErrors[] = 'Theme ' . $theme->Name . ' does not exist';
  429. writeLog('Theme ' . $theme->Name . ' does not exist');
  430. }
  431. } elseif (function_exists('get_theme_data')) {
  432. $theme = get_theme_data($style);
  433. $themeTemplate = $theme['Template'];
  434. } else {
  435. $content = file_get_contents($style);
  436. if ($content) {
  437. $pattern = '/\s*template\s*:\s*([^\n]+)\s*/is';
  438. preg_match($pattern, $content, $matches);
  439. if (!empty($matches[1])) {
  440. $themeTemplate = trim($matches[1]);
  441. }
  442. } else {
  443. $updateErrors[] = 'Failed to read ' . $style;
  444. writeLog('Failed to read ' . $style);
  445. }
  446. }
  447. return ($themeTemplate == 'CherryFramework') ? true : false;
  448. }
  449. function getHeaderFooterCode($headerFooterPattern) {
  450. $headerFooter = array('header', 'footer');
  451. $headerFooterCode = array('header' => null, 'footer' => null);
  452. foreach ($headerFooter as $name) {
  453. $filePath = TEMPLATEPATH . '/' . $name . '.php';
  454. if (file_exists($filePath)) {
  455. $content = file_get_contents($filePath);
  456. if ($content) {
  457. $matchesCount = preg_match($headerFooterPattern, $content, $matches);
  458. if ($matchesCount == 1 && !empty($matches[0])) {
  459. $headerFooterCode[$name] = $matches[0];
  460. } else {
  461. $updateErrors[] = 'header|footer code not found in ' . $filePath;
  462. writeLog('header|footer code not found in ' . $filePath);
  463. }
  464. } else {
  465. $updateErrors[] = 'Failed to read ' . $filePath;
  466. writeLog('Failed to read ' . $filePath);
  467. }
  468. } else {
  469. $updateErrors[] = $filePath . ' does not exist';
  470. writeLog($filePath . ' does not exist');
  471. }
  472. }
  473. return $headerFooterCode;
  474. }
  475. function backupFile($templatePath) {
  476. return copy($templatePath, $templatePath . '.bak');
  477. }
  478. function writeLog($message) {
  479. $logFile = TEMPLATEPATH . '/update.log';
  480. if (is_writable(TEMPLATEPATH)) {
  481. file_put_contents($logFile, $message . PHP_EOL, FILE_APPEND);
  482. }
  483. }
  484. function cherryUpdate($cherryTemplates, $headerFooterPattern, &$updateErrors) {
  485. $themesPath = get_theme_root();
  486. $skip = array('.', '..', 'CherryFramework', 'twentytwelve', 'twentyeleven', 'twentyten', 'index.php');
  487. $themes = array_diff(scandir($themesPath), $skip);
  488. if (!empty($themes)) {
  489. foreach ($themes as $theme) {
  490. $themePath = $themesPath . '/' . $theme;
  491. if (is_dir($themePath) && file_exists($themePath . '/style.css')) {
  492. $isCherryChildTheme = isCherryChildTheme($themePath, $theme, $updateErrors);
  493. if ($isCherryChildTheme) {
  494. if (is_writable($themePath)) {
  495. writeLog(PHP_EOL . 'Child theme: ' . $themePath);
  496. $files = scandir($themePath);
  497. $themeTemplates = array_intersect($files, $cherryTemplates);
  498. if (!empty($themeTemplates)) {
  499. foreach ($themeTemplates as $template) {
  500. $templatePath = $themePath . '/' . $template;
  501. if (is_writable($templatePath)) {
  502. $content = file_get_contents($templatePath);
  503. if ($content) {
  504. $headerFooterMatchesCount = preg_match_all($headerFooterPattern, $content, $headerFooterMatches);
  505. if ($headerFooterMatchesCount > 0) {
  506. $backupFile = backupFile($templatePath);
  507. if ($backupFile) {
  508. writeLog('Backup ' . $templatePath);
  509. $content = preg_replace($headerFooterPattern, '', $content, -1, $headerFooterReplaceCount);
  510. if (!is_null($content) && $headerFooterReplaceCount > 0) {
  511. writeLog('Replace ' . $headerFooterReplaceCount . ' header|footer');
  512. if (file_put_contents($templatePath, $content)) {
  513. writeLog('Save ' . $templatePath);
  514. } else {
  515. $updateErrors[] = 'Failed to save ' . $templatePath;
  516. writeLog('Failed to save ' . $templatePath);
  517. }
  518. }
  519. } else {
  520. $updateErrors[] = 'Failed to backup ' . $templatePath;
  521. writeLog('Failed to backup ' . $templatePath);
  522. }
  523. }
  524. } else {
  525. $updateErrors[] = 'Failed to read ' . $templatePath;
  526. writeLog('Failed to read ' . $templatePath);
  527. }
  528. } else {
  529. $updateErrors[] = $templatePath . ' is not writable';
  530. writeLog($templatePath . ' is not writable');
  531. }
  532. }
  533. }
  534. $headerFooter = array_intersect($files, array('header.php', 'footer.php'));
  535. if (!empty($headerFooter)) {
  536. $headerFooterCode = getHeaderFooterCode($headerFooterPattern);
  537. if (!is_null($headerFooterCode['header']) && !is_null($headerFooterCode['footer'])) {
  538. foreach ($headerFooter as $file) {
  539. $filePath = $themePath . '/' . $file;
  540. if (is_writable($filePath)) {
  541. $content = file_get_contents($filePath);
  542. if ($content) {
  543. $headerFooterMatchesCount = preg_match_all($headerFooterPattern, $content, $headerFooterMatches);
  544. if ($headerFooterMatchesCount === 0) {
  545. $backupFile = backupFile($filePath);
  546. if ($backupFile) {
  547. writeLog('Backup ' . $filePath);
  548. if ($file == 'header.php') {
  549. $content .= $headerFooterCode['header'];
  550. } elseif ($file == 'footer.php') {
  551. $content = $headerFooterCode['footer'] . $content;
  552. }
  553. writeLog('Add header|footer code in ' . $filePath);
  554. if (file_put_contents($filePath, $content)) {
  555. writeLog('Save ' . $filePath);
  556. } else {
  557. $updateErrors[] = 'Failed to save ' . $filePath;
  558. writeLog('Failed to save ' . $filePath);
  559. }
  560. } else {
  561. $updateErrors[] = 'Failed to backup ' . $filePath;
  562. writeLog('Failed to backup ' . $filePath);
  563. }
  564. }
  565. } else {
  566. $updateErrors[] = 'Failed to read ' . $filePath;
  567. writeLog('Failed to read ' . $filePath);
  568. }
  569. } else {
  570. $updateErrors[] = $filePath . ' is not writable';
  571. writeLog($filePath . ' is not writable');
  572. }
  573. }
  574. }
  575. }
  576. } else {
  577. $updateErrors[] = $themePath . ' is not writable';
  578. writeLog($themePath . ' is not writable');
  579. }
  580. }
  581. }
  582. }
  583. }
  584. }
  585. //changeChildTheme
  586. function changeChildTheme(&$updateErrors){
  587. $themesPath = get_theme_root();
  588. $skip = array('.', '..', 'CherryFramework', 'twentytwelve', 'twentyeleven', 'twentyten', 'index.php');
  589. $themes = array_diff(scandir($themesPath), $skip);
  590. if (!empty($themes)) {
  591. foreach ($themes as $theme) {
  592. $themePath = $themesPath . '/' . $theme;
  593. if (is_dir($themePath) && file_exists($themePath . '/style.css')) {
  594. $isCherryChildTheme = isCherryChildTheme($themePath, $theme, $updateErrors);
  595. if ($isCherryChildTheme) {
  596. if (is_writable($themePath)) {
  597. unset($slider_in_header, $slider_in_page_home, $pahe_home_content);
  598. writeLog(PHP_EOL . 'Child theme: ' . $themePath);
  599. $files = scandir($themePath);
  600. $header_php = array_intersect($files, array('header.php'));
  601. if (!empty($header_php)) {
  602. $templatePath = $themePath . '/header.php';
  603. $content = file_get_contents($templatePath);
  604. if (stripos($content, '"static/static-slider"')!==false) {
  605. $slider_in_header = true;
  606. }
  607. }
  608. if(!isset($slider_in_header)){
  609. if (is_dir($themePath."/wrapper")){
  610. $nestedFiles = scandir($themePath."/wrapper");
  611. $header_wrapper_php = array_intersect($nestedFiles, array('wrapper-header.php'));
  612. if (!empty($header_wrapper_php)) {
  613. $header_wrapper_php_path = $themePath . '/wrapper/wrapper-header.php';
  614. $content = file_get_contents($header_wrapper_php_path);
  615. if (stripos($content, '"static/static-slider"')!==false) {
  616. $slider_in_header = true;
  617. }
  618. }
  619. }
  620. }
  621. if(!isset($slider_in_header)){
  622. $page_home_php = array_intersect($files, array('page-home.php'));
  623. if (!empty($page_home_php)) {
  624. $templatePath = $themePath . '/page-home.php';
  625. $content = file($templatePath);
  626. for ($i=0, $arrayCount = count($content); $i < $arrayCount ; $i++) {
  627. if(stripos($content[$i], '"static/static-slider"')!==false){
  628. $slider_in_page_home = $i;
  629. }
  630. if(stripos($content[$i], '"page-home.php"')!==false){
  631. $pahe_home_content = $i;
  632. }
  633. }
  634. if(!isset($slider_in_page_home)){
  635. array_splice($content, $pahe_home_content+1, 0, "\t<div class=\"row\">\n\t\t<div class=\"span12\" data-motopress-type=\"static\" data-motopress-static-file=\"static/static-slider.php\">\n\t\t\t<?php get_template_part(\"static/static-slider\"); ?>\n\t\t</div>\n\t</div>\n");
  636. writeLog('Change page-home.php');
  637. if (file_put_contents($templatePath, $content)) {
  638. writeLog('Save ' . $templatePath);
  639. } else {
  640. $updateErrors[] = 'Failed to save ' . $templatePath;
  641. writeLog('Failed to save ' . $templatePath);
  642. }
  643. }
  644. }
  645. }
  646. $stylesheet = array_intersect($files, array('style.css'));
  647. $stylePath = $themePath . '/style.css';
  648. if (!empty($stylesheet)) {
  649. $styleContent = file($stylePath);
  650. if ($styleContent) {
  651. for ($i=0, $arrayCount = count($styleContent); $i < $arrayCount ; $i++) {
  652. if(stripos($styleContent[$i], "/*--")!==false){
  653. $styleContentChange = array();
  654. }
  655. if(isset($styleContentChange)){
  656. $styleContentChange[$i] = $styleContent[$i];
  657. }
  658. if(stripos($styleContent[$i], "--*/")!==false){
  659. break;
  660. }
  661. }
  662. array_push($styleContentChange, '@import url("main-style.css");');
  663. if (file_put_contents($stylePath, $styleContentChange)) {
  664. writeLog('Save ' . $stylePath);
  665. } else {
  666. $updateErrors[] = 'Failed to save ' . $stylePath;
  667. writeLog('Failed to save ' . $stylePath);
  668. }
  669. } else {
  670. $updateErrors[] = 'Failed to read ' . $stylesheet;
  671. writeLog('Failed to read ' . $stylesheet);
  672. }
  673. }
  674. if (is_dir($themePath."/static")){
  675. $nestedFiles = scandir($themePath."/static");
  676. $staticSlider = array_intersect($nestedFiles, array('static-slider.php'));
  677. $staticSliderPath = $themePath . '/static/static-slider.php';
  678. unset($sliderStatic, $new_function);
  679. if (!empty($staticSlider)) {
  680. $backupFileStatic = backupFile($staticSliderPath);
  681. if ($backupFileStatic) {
  682. $staticSliderContent = file($staticSliderPath);
  683. if ($staticSliderContent) {
  684. $static_slider_dom_1 = "<?php if(of_get_option('slider_type') != 'none_slider'){ ?>\n";
  685. $static_slider_dom_2 = "\t<?php get_slider_template_part(); ?>\n";
  686. $static_slider_dom_3 = "\n<?php }else{ ?>\n\t<div class=\"slider_off\"></div>\n<?php } ?>\n";
  687. for ($i=0, $arrayCount = count($staticSliderContent); $i < $arrayCount ; $i++) {
  688. if(stripos($staticSliderContent[$i], "get_template_part('slider')")!==false){
  689. $sliderStatic = $i;
  690. }
  691. if(stripos($staticSliderContent[$i], "get_slider_template_part")!==false){
  692. $new_function = $i;
  693. }
  694. }
  695. if(!isset($sliderStatic) && !isset($new_function)){
  696. $staticSliderContent = "<?php /* Static Name: Slider */ ?>\n<?php if(of_get_option('slider_type') != 'none_slider'){ ?>\n\t<div id=\"slider-wrapper\" class=\"slider\">\n\t\t<div class=\"container\">\n\t\t\t<?php get_slider_template_part(); ?>\n\t\t</div>\n\t</div><!-- .slider -->\n<?php }else{ ?>\n\t<div class=\"slider_off\"><!--slider off--></div>\n<?php } ?>";
  697. }else{
  698. if(!isset($new_function)){
  699. $staticSliderContent[$sliderStatic] = $static_slider_dom_2;
  700. array_splice($staticSliderContent, 1, 0, $static_slider_dom_1);
  701. array_push($staticSliderContent, $static_slider_dom_3);
  702. }
  703. }
  704. if (file_put_contents($staticSliderPath, $staticSliderContent)) {
  705. writeLog('Save ' . $staticSliderPath);
  706. } else {
  707. $updateErrors[] = 'Failed to save ' . $staticSliderPath;
  708. writeLog('Failed to save ' . $staticSliderPath);
  709. }
  710. } else {
  711. $updateErrors[] = 'Failed to read ' . $staticSlider;
  712. writeLog('Failed to read ' . $staticSlider);
  713. }
  714. }else {
  715. $updateErrors[] = 'Failed to backup ' . $staticSliderPath;
  716. writeLog('Failed to backup ' . $staticSliderPath);
  717. }
  718. }
  719. }
  720. } else {
  721. $updateErrors[] = $themePath . ' is not writable';
  722. writeLog($themePath . ' is not writable');
  723. }
  724. }
  725. }
  726. }
  727. }
  728. };
  729. //end changeChildTheme
  730. if (has_action('after_switch_theme')) {
  731. add_action('after_switch_theme', 'activateCherryForceUpdate', 10, 0);
  732. } else {
  733. if (is_admin() && isset($_GET['activated']) && $pagenow == 'themes.php') {
  734. activateCherryForceUpdate();
  735. }
  736. }
  737. function activateCherryForceUpdate() {
  738. global $cherryTemplates;
  739. global $headerFooterPattern;
  740. $style = STYLESHEETPATH . '/style.css';
  741. $themeTemplate = false;
  742. if (function_exists('wp_get_theme')) {
  743. $theme = wp_get_theme();
  744. if (file_exists($style) && $theme->exists()) {
  745. if ($theme->Stylesheet != $theme->Template) {
  746. $themeTemplate = $theme->Template;
  747. }
  748. } else {
  749. writeLog($style . ' does not exist');
  750. }
  751. } elseif (function_exists('get_theme_data')) {
  752. if (file_exists($style)) {
  753. $theme = get_theme_data($style);
  754. $themeTemplate = $theme['Template'];
  755. } else {
  756. writeLog($style . ' does not exist');
  757. }
  758. } else {
  759. if (file_exists($style)) {
  760. $content = file_get_contents($style);
  761. if ($content) {
  762. $pattern = '/\s*template\s*:\s*([^\n]+)\s*/is';
  763. preg_match($pattern, $content, $matches);
  764. if (!empty($matches[1])) {
  765. $themeTemplate = trim($matches[1]);
  766. }
  767. } else {
  768. writeLog('Failed to read ' . $style);
  769. }
  770. } else {
  771. writeLog($style . ' does not exist');
  772. }
  773. }
  774. if ($themeTemplate == 'CherryFramework') {
  775. $themePath = get_stylesheet_directory();
  776. $files = scandir($themePath);
  777. $themeTemplates = array_intersect($files, $cherryTemplates);
  778. $cherryForceUpdate = false;
  779. if (!empty($themeTemplates)) {
  780. foreach ($themeTemplates as $template) {
  781. $templatePath = $themePath . '/' . $template;
  782. $content = file_get_contents($templatePath);
  783. if ($content) {
  784. $headerFooterMatchesCount = preg_match($headerFooterPattern, $content);
  785. if ($headerFooterMatchesCount == 1) {
  786. $cherryForceUpdate = true;
  787. break;
  788. }
  789. } else {
  790. writeLog('Failed to read ' . $templatePath);
  791. }
  792. }
  793. }
  794. if ($cherryForceUpdate) {
  795. update_option('cherry_force_update', 1);
  796. } else {
  797. update_option('cherry_force_update', 0);
  798. }
  799. }
  800. }
  801. //------------------------------------------------------
  802. // slider function
  803. //------------------------------------------------------
  804. if (!function_exists("my_post_type_slider")) {
  805. function my_post_type_slider() {
  806. register_post_type( 'slider',
  807. array(
  808. 'label' => theme_locals("slides"),
  809. 'singular_label' => theme_locals("slides"),
  810. '_builtin' => false,
  811. 'exclude_from_search' => true, // Exclude from Search Results
  812. 'capability_type' => 'page',
  813. 'public' => true,
  814. 'show_ui' => true,
  815. 'show_in_nav_menus' => false,
  816. 'rewrite' => array(
  817. 'slug' => 'slide-view',
  818. 'with_front' => FALSE,
  819. ),
  820. 'query_var' => "slide", // This goes to the WP_Query schema
  821. 'menu_icon' => get_template_directory_uri() . '/includes/images/icon_slides.png',
  822. 'supports' => array(
  823. 'title',
  824. // 'custom-fields',
  825. 'thumbnail'
  826. )
  827. )
  828. );
  829. }
  830. add_action('init', 'my_post_type_slider');
  831. }
  832. if (!function_exists("get_slider_template_part")) {
  833. function get_slider_template_part() {
  834. switch (of_get_option('slider_type')) {
  835. case "accordion_slider":
  836. $slider_type = "accordion";
  837. break;
  838. default:
  839. $slider_type = "slider";
  840. }
  841. return get_template_part($slider_type);
  842. }
  843. }
  844. //------------------------------------------------------
  845. // Warning notice
  846. //------------------------------------------------------
  847. add_action( 'admin_notices', 'warning_notice' );
  848. function warning_notice() {
  849. global $pagenow;
  850. $pageHidden = array('admin.php');
  851. if (!get_user_meta(get_current_user_id(), '_wp_hide_notice', true) && is_admin() && !FILE_WRITEABLE && !in_array($pagenow, $pageHidden)) {
  852. printf('<div class="updated"><strong><p>'.theme_locals('warning_notice_2').'</p><p>'.theme_locals('warning_notice_3').'</p><p><a href="'.esc_url(add_query_arg( 'wp_nag', wp_create_nonce( 'wp_nag' ))).'">'.theme_locals('dismiss_notice').'</a></p></strong></div>');
  853. }
  854. }
  855. //------------------------------------------------------
  856. // Post Meta
  857. //------------------------------------------------------
  858. $global_meta_elements = array();
  859. function get_post_metadata( $args = array() ) {
  860. global $global_meta_elements;
  861. if(array_key_exists('meta_elements', $args)){
  862. $global_meta_elements = array_unique(array_merge($global_meta_elements, $args['meta_elements']));
  863. }
  864. $meta_elements_empty = isset($args['meta_elements']) ? false : true ;
  865. $defaults = array(
  866. 'meta_elements' => array('start_unite', 'date', 'author', 'permalink', 'end_unite', 'start_unite', 'categories', 'tags', 'end_unite', 'start_unite', 'comment', 'views', 'like', 'dislike', 'end_unite'),
  867. 'meta_class' => 'post_meta',
  868. 'meta_before' => '',
  869. 'meta_after' => ''
  870. );
  871. $args = wp_parse_args( $args, $defaults );
  872. $post_meta_type = (of_get_option('post_meta') == 'true' || of_get_option('post_meta') == '') ? 'line' : of_get_option('post_meta');
  873. if($meta_elements_empty){
  874. foreach ($global_meta_elements as $key) {
  875. if($key != 'end_unite || start_unite'){
  876. unset($args['meta_elements'][array_search($key, $args['meta_elements'])]);
  877. }
  878. }
  879. }
  880. if($post_meta_type!='false'){
  881. $post_ID = get_the_ID();
  882. $post_type = get_post_type($post_ID);
  883. $icon_tips_before = ($post_meta_type == 'icon') ? '<div class="tips">' : '';
  884. $icon_tips_after = ($post_meta_type == 'icon') ? '</div>' : '';
  885. $user_login = is_user_logged_in() ? true : false;
  886. $user_id = $user_login ? get_current_user_id() : "";
  887. $voting_class = $user_login ? 'ajax_voting ' : 'not_voting ';
  888. $voting_url = PARENT_URL.'/includes/voting.php?post_ID='.$post_ID.'&amp;get_user_ID='.$user_id;
  889. $get_voting_array = cherry_getPostVoting($post_ID, $user_id);
  890. $user_voting = $get_voting_array['user_voting'];
  891. echo $args['meta_before'].'<div class="'.$args['meta_class'].' meta_type_'.$post_meta_type.'">';
  892. foreach ($args['meta_elements'] as $value) {
  893. switch ($value) {
  894. case 'date':
  895. if(of_get_option('post_date') != 'no'){ ?>
  896. <div class="post_date">
  897. <i class="icon-calendar"></i>
  898. <?php echo $icon_tips_before . '<time datetime="' . get_the_time('Y-m-d\TH:i:s') . '">' . get_the_date() . '</time>' . $icon_tips_after; ?>
  899. </div>
  900. <?php
  901. }
  902. break;
  903. case 'author':
  904. if(of_get_option('post_author') != 'no'){ ?>
  905. <div class="post_author">
  906. <i class="icon-user"></i>
  907. <?php
  908. echo $icon_tips_before;
  909. the_author_posts_link();
  910. echo $icon_tips_after;
  911. ?>
  912. </div>
  913. <?php
  914. }
  915. break;
  916. case 'permalink':
  917. if(of_get_option('post_permalink') != 'no'){ ?>
  918. <div class="post_permalink">
  919. <i class="icon-link"></i>
  920. <?php echo $icon_tips_before.'<a href="'.get_permalink().'" title="'.get_the_title().'">'.theme_locals('permalink_to').'</a>'.$icon_tips_after; ?>
  921. </div>
  922. <?php
  923. }
  924. break;
  925. case 'categories':
  926. if(of_get_option('post_category') != 'no'){ ?>
  927. <div class="post_category">
  928. <i class="icon-bookmark"></i>
  929. <?php
  930. echo $icon_tips_before;
  931. ($post_type != 'post') ? the_terms($post_ID, $post_type.'_category','',', ') : the_category(', ');
  932. echo $icon_tips_after;
  933. ?>
  934. </div>
  935. <?php
  936. }
  937. break;
  938. case 'tags':
  939. if(of_get_option('post_tag') != 'no'){ ?>
  940. <div class="post_tag">
  941. <i class="icon-tag"></i>
  942. <?php
  943. echo $icon_tips_before;
  944. if(get_the_tags() || has_term('', $post_type.'_tag', $post_ID)){
  945. echo ($post_type != 'post') ? the_terms($post_ID, $post_type.'_tag','',', ') : the_tags('', ', ');
  946. } else {
  947. echo theme_locals('has_not_tags');
  948. }
  949. echo $icon_tips_after;
  950. ?>
  951. </div>
  952. <?php
  953. }
  954. break;
  955. case 'comment':
  956. if(of_get_option('post_comment') != 'no'){ ?>
  957. <div class="post_comment">
  958. <i class="icon-comments"></i>
  959. <?php
  960. echo $icon_tips_before;
  961. comments_popup_link(theme_locals('no_comments'), theme_locals('comment'), '% '.theme_locals('comments'), theme_locals('comments_link'), theme_locals('comments_closed'));
  962. echo $icon_tips_after;
  963. ?>
  964. </div>
  965. <?php
  966. }
  967. break;
  968. case 'views':
  969. if(of_get_option('post_views') != 'no'){ ?>
  970. <div class="post_views" title="<?php echo theme_locals('number_views'); ?>">
  971. <i class="icon-eye-open"></i>
  972. <?php echo $icon_tips_before.cherry_getPostViews($post_ID).$icon_tips_after; ?>
  973. </div>
  974. <?php
  975. }
  976. break;
  977. case 'dislike':
  978. if(of_get_option('post_dislike') != 'no'){
  979. $dislike_url = ($user_login && $user_voting=='none') ? 'href="'.$voting_url.'&amp;voting=dislike"' : '';
  980. $dislike_count = $get_voting_array['dislike_count'];
  981. $dislike_title = $user_login ? theme_locals('dislike') : theme_locals('not_voting');
  982. $dislike_class = ($user_voting == 'dislike') ? 'user_dislike ' : '';
  983. if($user_voting!='none'){
  984. $voting_class = "user_voting ";
  985. }
  986. ?>
  987. <div class="post_dislike">
  988. <a <?php echo $dislike_url; ?> class="<?php echo $voting_class.$dislike_class; ?>" title="<?php echo $dislike_title; ?>" date-type="dislike" >
  989. <i class="icon-thumbs-down"></i>
  990. <?php echo $icon_tips_before.'<span class="voting_count">'.$dislike_count.'</span>'.$icon_tips_after; ?>
  991. </a>
  992. </div>
  993. <?php
  994. }
  995. break;
  996. case 'like':
  997. if(of_get_option('post_like') != 'no'){
  998. $like_url = ($user_login && $user_voting=='none') ? 'href="'.$voting_url.'&amp;voting=like"' : '';
  999. $like_count = $get_voting_array['like_count'];
  1000. $like_title = $user_login ? theme_locals('like') : theme_locals('not_voting');
  1001. $like_class = ($user_voting == 'like') ? 'user_like ' : '';
  1002. if($user_voting!='none'){
  1003. $voting_class = "user_voting ";
  1004. }
  1005. ?>
  1006. <div class="post_like">
  1007. <a <?php echo $like_url; ?> class="<?php echo $voting_class.$like_class; ?>" title="<?php echo $like_title; ?>" date-type="like" >
  1008. <i class="icon-thumbs-up"></i>
  1009. <?php echo $icon_tips_before.'<span class="voting_count">'.$like_count.'</span>'.$icon_tips_after; ?>
  1010. </a>
  1011. </div>
  1012. <?php
  1013. }
  1014. break;
  1015. case 'start_unite':
  1016. echo '<div class="post_meta_unite clearfix">';
  1017. break;
  1018. case 'end_unite':
  1019. echo '</div>';
  1020. break;
  1021. }
  1022. }
  1023. echo '</div>'.$args['meta_after'];
  1024. }
  1025. }
  1026. //------------------------------------------------------
  1027. // Post Views
  1028. //------------------------------------------------------
  1029. function cherry_getPostViews($postID){
  1030. return (get_post_meta($postID, 'post_views_count', true) == '') ? "0" : get_post_meta($postID, 'post_views_count', true);
  1031. }
  1032. function cherry_setPostViews($postID){
  1033. $count_key = 'post_views_count';
  1034. $count = get_post_meta($postID, $count_key, true);
  1035. if($count==''){
  1036. $count = 1;
  1037. }else{
  1038. $count++;
  1039. }
  1040. update_post_meta($postID, $count_key, $count);
  1041. }
  1042. //------------------------------------------------------
  1043. // Post voting
  1044. //------------------------------------------------------
  1045. function cherry_getPostVoting($postID, $user_id){
  1046. $like_count = (get_post_meta($postID, 'post_like', true) == false) ? "0" : get_post_meta($postID, 'post_like', true);
  1047. $dislike_count = (get_post_meta($postID, 'post_dislike', true) == false) ? "0" : get_post_meta($postID, 'post_dislike', true);
  1048. $user_like_array = get_post_meta($postID, 'user_like');
  1049. $user_dislike_array = get_post_meta($postID, 'user_dislike');
  1050. $user_voting = 'none';
  1051. if(in_array($user_id, $user_like_array)){
  1052. $user_voting = 'like';
  1053. }else if(in_array($user_id, $user_dislike_array)){
  1054. $user_voting = 'dislike';
  1055. }
  1056. return array('like_count' => $like_count, 'dislike_count' => $dislike_count, 'user_voting' => $user_voting);
  1057. }
  1058. //------------------------------------------------------
  1059. // Get team social networks
  1060. //------------------------------------------------------
  1061. function cherry_get_post_networks($args = array()){
  1062. global $post;
  1063. extract(
  1064. wp_parse_args(
  1065. $args,
  1066. array(
  1067. 'post_id' => get_the_ID(),
  1068. 'class' => 'post_networks',
  1069. 'before_title' => '<h4>',
  1070. 'after_title' => '</h4>',
  1071. 'display_title' => true,
  1072. 'output_type' => 'echo'
  1073. )
  1074. )
  1075. );
  1076. $networks_array = explode(" ", get_option('fields_id_value'.$post_id, ''));
  1077. if($networks_array[0]!=''){
  1078. $count = 0;
  1079. $network_title = get_post_meta($post_id, 'network_title', true);
  1080. $output = '<div class="'.$class.'">';
  1081. $output .= $network_title && $display_title ? $before_title.$network_title.$after_title : '';
  1082. $output .= '<ul class="clearfix unstyled">';
  1083. foreach ($networks_array as $networks_id) {
  1084. $network_array = explode(";", get_option('network_'.$post_id.'_'.$networks_id, array('','','')));
  1085. $output .= '<li class="network_'.$count.'">';
  1086. $output .= $network_array[2] ? '<a href="'.$network_array[2].'" title="'.$network_array[1].'">' : '' ;
  1087. $output .= $network_array[0] ? '<span class="'.$network_array[0].'"></span>' :'';
  1088. $output .= $network_array[1] ? '<span class="network_title">'.$network_array[1].'</span>' : '' ;
  1089. $output .= $network_array[2] ? '</a>' : '' ;
  1090. $output .= '</li>';
  1091. ++$count;
  1092. }
  1093. $output .= '</ul></div>';
  1094. if($output_type == 'echo'){
  1095. echo $output;
  1096. }else{
  1097. return $output;
  1098. }
  1099. }
  1100. }
  1101. ?>

cherryframework functions.php