PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/data/wpcom-themes/hemingway/functions.php

https://gitlab.com/Blueprint-Marketing/wordpress-unit-tests
PHP | 521 lines | 450 code | 62 blank | 9 comment | 22 complexity | 613ea3a1284a1eb9c2a83dde48fbb57c MD5 | raw file
  1. <?php
  2. $themecolors = array(
  3. 'bg' => '000000',
  4. 'text' => 'bfbfbf',
  5. 'link' => 'ffffff',
  6. 'border' => '000000'
  7. );
  8. // this varies but the single page content width seems to be 607px max
  9. $content_width = 600;
  10. register_sidebar( array(
  11. 'name' => __('Bottom 1'),
  12. 'id' => 'bottom-1',
  13. 'before_widget' => '<span class="widget">',
  14. 'after_widget' => '</span>',
  15. 'before_title' => '<h2>',
  16. 'after_title' => '</h2>' ) );
  17. register_sidebar( array(
  18. 'name' => __('Bottom 2'),
  19. 'id' => 'bottom-2',
  20. 'before_widget' => '<span class="widget">',
  21. 'after_widget' => '</span>',
  22. 'before_title' => '<h2>',
  23. 'after_title' => '</h2>' ) );
  24. register_sidebar( array(
  25. 'name' => __('Bottom 3'),
  26. 'id' => 'bottom-3',
  27. 'before_widget' => '<span class="widget">',
  28. 'after_widget' => '</span>',
  29. 'before_title' => '<h2>',
  30. 'after_title' => '</h2>' ) );
  31. class Hemingway
  32. {
  33. var $raw_blocks;
  34. var $available_blocks;
  35. var $style;
  36. var $version;
  37. function add_available_block($block_name, $block_ref)
  38. {
  39. $blocks = $this->available_blocks;
  40. if (!$blocks[$block_ref]){
  41. $blocks[$block_ref] = $block_name;
  42. update_option('hem_available_blocks', $blocks);
  43. wp_cache_flush();
  44. }
  45. }
  46. function get_available_blocks()
  47. // This function returns an array of available blocks
  48. // in the format of $arr[block_ref] = block_name
  49. {
  50. $this->available_blocks = get_option('hem_available_blocks');
  51. return $this->available_blocks;
  52. }
  53. function get_block_contents($block_place)
  54. // Returns an array of block_refs in specififed block
  55. {
  56. if (!$this->raw_blocks){
  57. $this->raw_blocks = get_option('hem_blocks');
  58. }
  59. return $this->raw_blocks[$block_place];
  60. }
  61. function add_block_to_place($block_place, $block_ref)
  62. {
  63. $block_contents = $this->get_block_contents($block_place);
  64. if (in_array($block_ref, $block_contents))
  65. return true;
  66. $block_contents[] = $block_ref;
  67. $this->raw_blocks[$block_place] = $block_contents;
  68. update_option('hem_blocks', $this->raw_blocks);
  69. wp_cache_flush(); // I was having caching issues
  70. return true;
  71. }
  72. function remove_block_in_place($block_place, $block_ref)
  73. {
  74. $block_contents = $this->get_block_contents($block_place);
  75. if (!in_array($block_ref, $block_contents))
  76. return true;
  77. $key = array_search($block_ref, $block_contents);
  78. unset($block_contents[$key]);
  79. $this->raw_blocks[$block_place] = $block_contents;
  80. update_option('hem_blocks', $this->raw_blocks);
  81. wp_cache_flush(); // I was having caching issues
  82. return true;
  83. }
  84. // Templating functions
  85. function get_block_output($block_place)
  86. {
  87. $blocks = $this->get_block_contents($block_place);
  88. foreach($blocks as $key => $block ){
  89. include (TEMPLATEPATH . '/blocks/' . $block . '.php');
  90. }
  91. }
  92. function get_style(){
  93. $this->style = get_option('hem_style');
  94. }
  95. }
  96. $hemingway = new Hemingway();
  97. $hemingway->get_available_blocks();
  98. $hemingway->get_style();
  99. $hemingway->version = "0.13";
  100. // Options
  101. $default_blocks = Array(
  102. 'recent_entries' => 'Recent Entries',
  103. 'about_page' => 'About Page',
  104. 'category_listing' => 'Category Listing',
  105. 'blogroll' => 'Blogroll',
  106. 'pages' => 'Pages',
  107. 'monthly_archives' => 'Monthly Archives'
  108. );
  109. $default_block_locations = Array(
  110. 'block_1' => Array('about_page'),
  111. 'block_2' => Array('recent_entries'),
  112. 'block_3' => Array('category_listing'),
  113. 'block_4' => Array(),
  114. 'block_5' => Array(),
  115. 'block_6' => Array()
  116. );
  117. if (!get_option('hem_version') || get_option('hem_version') < $hemingway->version){
  118. // Hemingway isn't installed, so we'll need to add options
  119. if (!get_option('hem_version') )
  120. add_option('hem_version', $hemingway->version, 'Hemingway Version installed');
  121. else
  122. update_option('hem_version', $hemingway->version);
  123. if (!get_option('hem_available_blocks') )
  124. add_option('hem_available_blocks', $default_blocks, 'A list of available blocks for Hemingway');
  125. if (!get_option('hem_blocks') )
  126. add_option('hem_blocks', $default_block_locations, 'An array of blocks and their contents');
  127. if (!get_option('hem_style') )
  128. add_option('hem_style', '', 'Location of custom style sheet');
  129. }
  130. // Ajax Stuff
  131. if ($_GET['hem_action'] == 'add_block'){
  132. $block_ref = $_GET['block_ref'];
  133. $block_place = $_GET['block_place'];
  134. $block_name = $hemingway->available_blocks[$block_ref];
  135. $hemingway->add_block_to_place($block_place, $block_ref);
  136. ob_end_clean(); // Kill preceding output
  137. $output = '<ul>';
  138. foreach($hemingway->get_block_contents($block_place) as $key => $block_ref){
  139. $block_name = $hemingway->available_blocks[$block_ref];
  140. $output .= '<li>' . $block_name . ' (<a href="#" onclick="remove_block(\'' . $block_place . '\', \'' . $block_ref . '\');">remove</a>)</li>';
  141. }
  142. $output .= '</ul>';
  143. echo $output;
  144. exit(); // Kill any more output
  145. }
  146. if ($_GET['hem_action'] == 'remove_block'){
  147. $block_ref = $_GET['block_ref'];
  148. $block_place = $_GET['block_place'];
  149. $hemingway->remove_block_in_place($block_place, $block_ref);
  150. ob_end_clean(); // Kill preceding output
  151. $output = '<ul>';
  152. foreach($hemingway->get_block_contents($block_place) as $key => $block_ref){
  153. $block_name = $hemingway->available_blocks[$block_ref];
  154. $output .= '<li>' . $block_name . ' (<a href="#" onclick="remove_block(\'' . $block_place . '\', \'' . $block_ref . '\');">remove</a>)</li>';
  155. }
  156. $output .= '</ul>';
  157. echo $output;
  158. exit(); // Kill any more output
  159. }
  160. if ($_POST['custom_styles']){
  161. update_option('hem_style', $_POST['custom_styles']);
  162. wp_cache_flush();
  163. $message = 'Styles updated!';
  164. }
  165. if ($_POST['block_ref']){
  166. $hemingway->add_available_block($_POST['display_name'], $_POST['block_ref']);
  167. $hemingway->get_available_blocks();
  168. $message = 'Block added!';
  169. }
  170. // Stuff
  171. add_action ('admin_menu', 'hemingway_menu');
  172. $hem_loc = '../themes/' . basename(dirname($file));
  173. function hemingway_scripts() {
  174. $dir = get_bloginfo('template_directory');
  175. wp_enqueue_script('prototype');
  176. wp_enqueue_script('dragdrop', $dir . '/admin/js/dragdrop.js', false, 1);
  177. wp_enqueue_script('effects', $dir . '/admin/js/effects.js', false, 1);
  178. }
  179. function hemingway_menu() {
  180. $page = add_submenu_page('themes.php', 'Hemingway Options', 'Hemingway Options', 5, $hem_loc . 'functions.php', 'menu');
  181. add_action('load-' . $page, 'hemingway_scripts');
  182. }
  183. function menu() {
  184. global $hem_loc, $hemingway, $message;
  185. ?>
  186. <!--
  187. Okay, so I don't honestly know how legit this is, but I want a more intuitive interface
  188. so I'm going to import scriptaculous. There's a good chance this is going to mess stuff up
  189. for some people :)
  190. -->
  191. <script type="text/javascript">
  192. function remove_block(block_place, block_ref){
  193. url = 'themes.php?page=functions.php&hem_action=remove_block&block_place=' + block_place + '&block_ref=' + block_ref;
  194. new Ajax.Updater(block_place, url,
  195. {
  196. evalScripts:true, asynchronous:true
  197. }
  198. )
  199. }
  200. </script>
  201. <style>
  202. .block{
  203. width:200px;
  204. height:200px;
  205. border:1px solid #CCC;
  206. float:left;
  207. margin:20px 1em 20px 0;
  208. padding:10px;
  209. display:inline;
  210. }
  211. .block ul{
  212. padding:0;
  213. margin:0;
  214. }
  215. .block ul li{
  216. margin:0 0 5px 0;
  217. list-style-type:none;
  218. }
  219. .block-active{
  220. border:1px solid #333;
  221. background:#F2F8FF;
  222. }
  223. #addables li{
  224. list-style-type:none;
  225. margin:1em 1em 1em 0;
  226. background:#EAEAEA;
  227. border:1px solid #DDD;
  228. padding:3px;
  229. width:215px;
  230. float:left;
  231. cursor:move;
  232. }
  233. ul#addables{
  234. margin:0;
  235. padding:0;
  236. width:720px;
  237. position:relative;
  238. }
  239. </style>
  240. <?php if($message) : ?>
  241. <div id="message" class="updated fade"><p><?php echo $message; ?></p></div>
  242. <?php endif; ?>
  243. <div class="wrap" style="position:relative;">
  244. <h2><?php _e('Hemingway Options'); ?></h2>
  245. <h3>Color Options</h3>
  246. <p>Choose a primary color for your site:</p>
  247. <form name="dofollow" action="" method="post">
  248. <input type="hidden" name="page_options" value="'dofollow_timeout'" />
  249. <p><label><input name="custom_styles" type="radio" value="none" <?php if ($hemingway->style == 'none') echo 'checked="checked"'; ?> />
  250. Black</label></p>
  251. <p><label><input name="custom_styles" type="radio" value="white.css" <?php if ($hemingway->style == 'white.css') echo 'checked="checked"'; ?> /> White</label></p>
  252. <input type="submit" value="Update Color &raquo;" />
  253. </form>
  254. <h3>Hemingway's Bottombar&trade;</h3>
  255. <p>Drag and drop the different blocks into their place below. After you drag the block to the area, it will update with the new contents automatically.</p>
  256. <p>*Note: Widgets take prefernce over these blocks.</p>
  257. <ul id="addables">
  258. <?php foreach($hemingway->available_blocks as $ref => $name) : ?>
  259. <li id="<?php echo $ref; ?>" class="blocks"><?php echo $name; ?></li>
  260. <script type="text/javascript">new Draggable('<?php echo $ref; ?>', {revert:true})</script>
  261. <?php endforeach; ?>
  262. </ul>
  263. <div class="clear"></div>
  264. <div class="block" id="block_1">
  265. <ul>
  266. <?php
  267. foreach($hemingway->get_block_contents('block_1') as $key => $block_ref) :
  268. $block_name = $hemingway->available_blocks[$block_ref];
  269. ?>
  270. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_1', '<?php echo $block_ref; ?>');">remove</a>)</li>
  271. <?php endforeach; ?>
  272. </ul>
  273. </div>
  274. <script type="text/javascript">
  275. Droppables.add(
  276. 'block_1', {
  277. accept:'blocks',
  278. onDrop:function(element){
  279. new Ajax.Updater('block_1', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_1&block_ref=' + element.id,
  280. {
  281. evalScripts:true, asynchronous:true
  282. }
  283. )
  284. },
  285. hoverclass:'block-active'
  286. }
  287. )
  288. </script>
  289. <div class="block" id="block_2">
  290. <ul>
  291. <?php
  292. foreach($hemingway->get_block_contents('block_2') as $key => $block_ref) :
  293. $block_name = $hemingway->available_blocks[$block_ref];
  294. ?>
  295. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_2', '<?php echo $block_ref; ?>');">remove</a>)</li>
  296. <?php endforeach; ?>
  297. </ul>
  298. </div>
  299. <script type="text/javascript">
  300. Droppables.add(
  301. 'block_2', {
  302. accept:'blocks',
  303. onDrop:function(element){
  304. new Ajax.Updater('block_2', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_2&block_ref=' + element.id,
  305. {
  306. evalScripts:true, asynchronous:true
  307. }
  308. )
  309. },
  310. hoverclass:'block-active'
  311. }
  312. )
  313. </script>
  314. <div class="block" id="block_3">
  315. <ul>
  316. <?php
  317. foreach($hemingway->get_block_contents('block_3') as $key => $block_ref) :
  318. $block_name = $hemingway->available_blocks[$block_ref];
  319. ?>
  320. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_3', '<?php echo $block_ref; ?>');">remove</a>)</li>
  321. <?php endforeach; ?>
  322. </ul>
  323. </div>
  324. <script type="text/javascript">
  325. Droppables.add(
  326. 'block_3', {
  327. accept:'blocks',
  328. onDrop:function(element){
  329. new Ajax.Updater('block_3', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_3&block_ref=' + element.id,
  330. {
  331. evalScripts:true, asynchronous:true
  332. }
  333. )
  334. },
  335. hoverclass:'block-active'
  336. }
  337. )
  338. </script>
  339. <!-- Maybe later...
  340. <div class="clear"></div>
  341. <div class="block" id="block_4">
  342. Block 4
  343. <ul>
  344. <?php
  345. foreach($hemingway->get_block_contents('block_4') as $key => $block_ref) :
  346. $block_name = $hemingway->available_blocks[$block_ref];
  347. ?>
  348. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_4', '<?php echo $block_ref; ?>');">remove</a>)</li>
  349. <?php endforeach; ?>
  350. </ul>
  351. </div>
  352. <script type="text/javascript">
  353. Droppables.add(
  354. 'block_4', {
  355. accept:'blocks',
  356. onDrop:function(element){
  357. new Ajax.Updater('block_4', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_4&block_ref=' + element.id,
  358. {
  359. evalScripts:true, asynchronous:true
  360. }
  361. )
  362. },
  363. hoverclass:'block-active'
  364. }
  365. )
  366. </script>
  367. <div class="block" id="block_5">
  368. Block 5
  369. <ul>
  370. <?php
  371. foreach($hemingway->get_block_contents('block_5') as $key => $block_ref) :
  372. $block_name = $hemingway->available_blocks[$block_ref];
  373. ?>
  374. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_5', '<?php echo $block_ref; ?>');">remove</a>)</li>
  375. <?php endforeach; ?>
  376. </ul>
  377. </div>
  378. <script type="text/javascript">
  379. Droppables.add(
  380. 'block_5', {
  381. accept:'blocks',
  382. onDrop:function(element){
  383. new Ajax.Updater('block_5', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_5&block_ref=' + element.id,
  384. {
  385. evalScripts:true, asynchronous:true
  386. }
  387. )
  388. },
  389. hoverclass:'block-active'
  390. }
  391. )
  392. </script>
  393. <div class="block" id="block_6">
  394. Block 6
  395. <ul>
  396. <?php
  397. foreach($hemingway->get_block_contents('block_6') as $key => $block_ref) :
  398. $block_name = $hemingway->available_blocks[$block_ref];
  399. ?>
  400. <li><?php echo $block_name; ?> (<a href="#" onclick="remove_block('block_6', '<?php echo $block_ref; ?>');">remove</a>)</li>
  401. <?php endforeach; ?>
  402. </ul>
  403. </div>
  404. <script type="text/javascript">
  405. Droppables.add(
  406. 'block_6', {
  407. accept:'blocks',
  408. onDrop:function(element){
  409. new Ajax.Updater('block_6', 'themes.php?page=functions.php&hem_action=add_block&block_place=block_6&block_ref=' + element.id,
  410. {
  411. evalScripts:true, asynchronous:true
  412. }
  413. )
  414. },
  415. hoverclass:'block-active'
  416. }
  417. )
  418. </script>
  419. -->
  420. <div class="clear"></div>
  421. <?php
  422. $blocks_dir = @ dir(ABSPATH . '/wp-content/themes/' . get_template() . '/blocks');
  423. if ($blocks_dir) {
  424. while(($file = $blocks_dir->read()) !== false) {
  425. if (!preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file))
  426. $blocks_files[] = $file;
  427. }
  428. }
  429. if ($blocks_dir || $blocks_files) {
  430. foreach($blocks_files as $blocks_file) {
  431. $block_ref = preg_replace('/\.php/', '', $blocks_file);
  432. if (!array_key_exists($block_ref, $hemingway->available_blocks)){
  433. ?>
  434. <h3>You have uninstalled blocks!</h3>
  435. <p>Give the block <strong><?php echo $block_ref; ?></strong> a display name (such as "About Page")</p>
  436. <form action="" name="dofollow" method="post">
  437. <input type="hidden" name="block_ref" value="<?php echo $block_ref; ?>" />
  438. <?php echo $block_ref; ?> : <input type="text" name="display_name" />
  439. <input type="submit" value="Save" />
  440. </form>
  441. <?
  442. }
  443. }
  444. }
  445. ?>
  446. </div>
  447. <?php
  448. }
  449. ?>