PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/canvas/includes/woo-layout/classes/woo-layout.class.php

https://bitbucket.org/russwittmann/baseball
PHP | 667 lines | 272 code | 190 blank | 205 comment | 66 complexity | 29d1c3ca467aedf2434c61144a759af8 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Layout Manager
  4. *
  5. * This is the layout manager class, containing all processing and setup functionality
  6. * for managing the dimensions of the various layout types available.
  7. *
  8. * @package WooFramework
  9. * @subpackage Module
  10. *
  11. *-----------------------------------------------------------------------------------
  12. CLASS INFORMATION
  13. Date Created: 2011-03-21.
  14. Author: Matty.
  15. Since: 4.0.0
  16. TABLE OF CONTENTS
  17. - var $plugin_prefix
  18. - var $plugin_path
  19. - var $plugin_url
  20. - var $version
  21. - var $woo_options_prefix
  22. - var $admin_page
  23. - var $layouts
  24. - var $layouts_info
  25. - var $gutter
  26. - function Woo_Layout () (Constructor)
  27. - function init ()
  28. - function register_admin_screen ()
  29. - function admin_screen ()
  30. - function admin_screen_help ()
  31. - function enqueue_scripts ()
  32. - function enqueue_styles ()
  33. - function enqueue_frontend_styles ()
  34. - function get_layout_info ()
  35. - function load_dynamic_css ()
  36. - function setup_layouts ()
  37. - function setup_layout_information ()
  38. - function add_exporter_data ()
  39. -----------------------------------------------------------------------------------*/
  40. class Woo_Layout {
  41. /*----------------------------------------
  42. Class Variables
  43. ----------------------------------------
  44. * Setup of variable placeholders, to be
  45. * populated when the constructor runs.
  46. ----------------------------------------*/
  47. var $plugin_prefix;
  48. var $plugin_path;
  49. var $plugin_url;
  50. var $version;
  51. var $woo_options_prefix;
  52. var $admin_page;
  53. var $layouts;
  54. var $layouts_info;
  55. var $gutter;
  56. /*----------------------------------------
  57. Constructor Function
  58. ----------------------------------------
  59. * Constructor function.
  60. * Sets up the class and registers
  61. * variable action hooks.
  62. * Params:
  63. * - String $plugin_prefix
  64. * - String $plugin_path
  65. * - String $plugin_url
  66. ----------------------------------------*/
  67. function Woo_Layout ( $plugin_prefix, $plugin_path, $plugin_url, $version ) {
  68. $this->plugin_prefix = $plugin_prefix;
  69. $this->plugin_path = $plugin_path;
  70. $this->plugin_url = $plugin_url;
  71. $this->version = $version;
  72. $this->woo_options_prefix = 'woo';
  73. $this->init();
  74. } // End Constructor
  75. /*----------------------------------------
  76. init()
  77. ----------------------------------------
  78. * This guy runs the show.
  79. * Rocket boosters... engage!
  80. ----------------------------------------*/
  81. function init () {
  82. // Enqueue the dynamic CSS data.
  83. if ( get_option( $this->woo_options_prefix . '_layout_manager_enable' ) == 'true' )
  84. add_action( 'wp_print_styles', array( &$this, 'enqueue_frontend_styles' ), 10 );
  85. if ( is_admin() ) {
  86. // Register the admin screen.
  87. add_action( 'admin_menu', array( &$this, 'register_admin_screen' ), 11 );
  88. // Execute certain code only on the specific admin screen.
  89. if ( is_admin( $this->admin_page ) ) {
  90. // Add contextual help.
  91. add_action( 'contextual_help', array( &$this, 'admin_screen_help' ), 10, 3 );
  92. }
  93. // Make sure our data is added to the WooFramework settings exporter.
  94. add_filter( 'wooframework_export_query_inner', array( &$this, 'add_exporter_data' ) );
  95. } // End IF Statement
  96. // Setup default layouts.
  97. $this->setup_layouts();
  98. // Setup default layout information.
  99. $this->setup_layout_information();
  100. // Generate the dynamic CSS data.
  101. if ( get_option( $this->woo_options_prefix . '_layout_manager_enable') == 'true' )
  102. add_action( 'template_redirect', array( &$this, 'load_dynamic_css' ) );
  103. } // End init()
  104. /*----------------------------------------
  105. register_admin_screen()
  106. ----------------------------------------
  107. * Register the admin screen in WordPress.
  108. ----------------------------------------*/
  109. function register_admin_screen () {
  110. if ( function_exists( 'add_submenu_page' ) ) {
  111. $this->admin_page = add_submenu_page('woothemes', __( 'Layout Manager', 'woothemes' ), __( 'Layout Manager', 'woothemes' ), 'manage_options', 'woo-layout-manager', array( &$this, 'admin_screen' ) );
  112. // Admin screen logic.
  113. add_action( 'load-' . $this->admin_page, array( &$this, 'admin_screen_logic' ) );
  114. // Admin screen JavaScript.
  115. add_action( 'admin_print_scripts-' . $this->admin_page, array( &$this, 'enqueue_scripts' ) );
  116. // Admin screen CSS.
  117. add_action( 'admin_print_styles-' . $this->admin_page, array( &$this, 'enqueue_styles' ) );
  118. }
  119. } // End register_admin_screen()
  120. /*----------------------------------------
  121. admin_screen()
  122. ----------------------------------------
  123. * Load the admin screen.
  124. ----------------------------------------*/
  125. function admin_screen () {
  126. // Keep the screen XHTML separate and load it from that file.
  127. include_once( $this->plugin_path . '/screens/admin.php' );
  128. } // End admin_screen()
  129. /*----------------------------------------
  130. admin_screen_help()
  131. ----------------------------------------
  132. * Contextual help on the admin screen.
  133. ----------------------------------------*/
  134. function admin_screen_help ( $contextual_help, $screen_id, $screen ) {
  135. // $contextual_help .= var_dump($screen); // use this to help determine $screen->id
  136. if ( $this->admin_page == $screen->id ) {
  137. $contextual_help =
  138. '<p>' . __('Welcome to the Woo Layout Manager!', 'woothemes') . '</p>' .
  139. '<p>' . __('Here are a few notes on using this screen.', 'woothemes') . '</p>' .
  140. '<p>' . __('Fill in the area you\'d like to customise and hit the "Save All Changes" button. It\'s as easy as that!', 'woothemes') . '</p>' .
  141. '<p><strong>' . __('For more information:', 'woothemes') . '</strong></p>' .
  142. '<p>' . sprintf( __('<a href="%s" target="_blank">WooThemes Support Forums</a>', 'woothemes'), 'http://forum.woothemes.com/' ) . '</p>';
  143. } // End IF Statement
  144. return $contextual_help;
  145. } // End admin_screen_help()
  146. /*----------------------------------------
  147. admin_screen_logic()
  148. ----------------------------------------
  149. * The processing logic used on the
  150. * admin screen.
  151. ----------------------------------------*/
  152. function admin_screen_logic () {
  153. // Reset logic.
  154. $is_processed = false;
  155. if ( isset( $_POST['woolayout_reset'] ) && check_admin_referer( 'woolayout-options-update' ) ) {
  156. update_option( $this->plugin_prefix . 'stored_layouts' );
  157. update_option( $this->woo_options_prefix . '_layout_manager_enable', 'false');
  158. $is_processed = true;
  159. wp_redirect( admin_url( 'admin.php?page=woo-layout-manager&reset=true' ) );
  160. }
  161. // Save logic.
  162. if ( isset( $_POST['woolayout_update'] ) && check_admin_referer( 'woolayout-options-update' ) ) {
  163. $fields_to_skip = array( 'woolayout_update', '_wp_http_referer', '_wpnonce' );
  164. $posted_data = $_POST;
  165. // Update Layout Manager Enable
  166. if ( ( isset($_POST['woo_layout_manager_enable']) ) && ( $_POST['woo_layout_manager_enable'] == 'true' ) ) {
  167. update_option( $this->woo_options_prefix . '_layout_manager_enable', 'true');
  168. } else {
  169. update_option( $this->woo_options_prefix . '_layout_manager_enable', 'false');
  170. }
  171. // Make sure we skip over the fields we don't need,
  172. // and validate the values that we do need to make sure
  173. // that they're all numeric values less than or equal to 100.
  174. foreach ( $posted_data as $k => $v ) {
  175. if ( in_array( $k, $fields_to_skip ) ) {
  176. unset( $posted_data[$k] );
  177. } else {
  178. // Get the woo_options array and update the necessary fields.
  179. $options = get_option( $this->woo_options_prefix . '_options' );
  180. $has_new_options = false;
  181. if ( is_array( $posted_data[$k] ) ) {
  182. $has_valid_values = true;
  183. // Validate the values.
  184. foreach ( $posted_data[$k] as $i => $j ) {
  185. foreach ( $posted_data[$k][$i] as $l => $m ) {
  186. if ( is_numeric( $m ) && ( $m <= 100 ) ) {} else { $has_valid_values = false; break; }
  187. }
  188. }
  189. // Set anything greater than 100 equal to 100.
  190. foreach ( $posted_data[$k] as $i => $j ) {
  191. foreach ( $posted_data[$k][$i] as $l => $m ) {
  192. if ( is_numeric( $m ) && ( $m <= 100 ) ) {} else { $posted_data[$k][$i][$l] = 100; }
  193. }
  194. }
  195. // Setup the values to be saved.
  196. if ( $has_valid_values ) {
  197. $posted_data[$k] = $v;
  198. }
  199. // Make sure that all values provided for each section add up to 100.
  200. foreach ( $posted_data[$k] as $i => $j ) {
  201. $total = 0;
  202. foreach ( $posted_data[$k][$i] as $l => $m ) {
  203. $total += $m;
  204. }
  205. if ( $total < 100 ) {
  206. $remainder = 100 - $total;
  207. $posted_data[$k][$i]['content'] += $remainder;
  208. }
  209. }
  210. } else {
  211. // Update non-layout options.
  212. update_option( $k, $v );
  213. if ( is_array( $options ) ) {
  214. $options[$k] = $v;
  215. $has_new_options = true;
  216. }
  217. }
  218. // If options in woo_options have been changed, update the woo_options array.
  219. if ( $has_new_options ) {
  220. update_option( $this->woo_options_prefix . '_options', $options );
  221. }
  222. }
  223. }
  224. if ( is_array( $posted_data ) ) {
  225. $is_updated = update_option( $this->plugin_prefix . 'stored_layouts', $posted_data );
  226. // Redirect to make sure the latest changes are reflected.
  227. wp_redirect( admin_url( 'admin.php?page=woo-layout-manager&updated=true' ) );
  228. }
  229. $is_processed = true;
  230. }
  231. } // End admin_screen_logic()
  232. /*----------------------------------------
  233. enqueue_scripts()
  234. ----------------------------------------
  235. * Enqueue the necessary JavaScript files.
  236. ----------------------------------------*/
  237. function enqueue_scripts () {
  238. wp_register_script( 'jquery-layout-min', $this->plugin_url . 'assets/js/jquery.layout.min.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-draggable', 'jquery-ui-resizable' ), '1.2.0', true );
  239. wp_register_script( 'woo-layout-functions', $this->plugin_url . 'assets/js/functions.js', array( 'jquery', 'jquery-layout-min' ), '1.0.0', true );
  240. wp_enqueue_script( 'woo-layout-functions' ); // The dependant JavaScript files will be enqueued automatically.
  241. } // End enqueue_scripts()
  242. /*----------------------------------------
  243. enqueue_styles()
  244. ----------------------------------------
  245. * Enqueue the necessary CSS files.
  246. ----------------------------------------*/
  247. function enqueue_styles () {
  248. wp_register_style( 'woo-admin-interface', get_template_directory_uri() . '/functions/admin-style.css' );
  249. wp_register_style( 'woo-layout-interface', $this->plugin_url . '/assets/css/interface.css' );
  250. wp_enqueue_style( 'woo-admin-interface' );
  251. wp_enqueue_style( 'woo-layout-interface' );
  252. } // End enqueue_styles()
  253. /*----------------------------------------
  254. enqueue_frontend_styles()
  255. ----------------------------------------
  256. * Enqueue the dynamic CSS to be used on
  257. * the theme's frontend.
  258. ----------------------------------------*/
  259. function enqueue_frontend_styles () {
  260. $url = home_url();
  261. $sep = '?';
  262. if ( ! get_option( 'permalink_structure' ) || ( isset( $_GET['lang'] ) ) ) { $sep = '&'; }
  263. if ( is_singular() ) {
  264. global $post;
  265. $url = get_permalink( $post->ID );
  266. }
  267. if ( stristr( $url, '?' ) === false ) {
  268. } else {
  269. $sep = '&';
  270. }
  271. wp_register_style( 'woo-layout-dynamic', urldecode( $url . $sep . 'woo-layout-css=load' ) );
  272. wp_enqueue_style( 'woo-layout-dynamic' );
  273. } // End enqueue_frontend_styles()
  274. /*----------------------------------------
  275. get_layout_info()
  276. ----------------------------------------
  277. * Get layout info for the current screen.
  278. ----------------------------------------*/
  279. function get_layout_info () {
  280. global $post, $woo_layout_manager;
  281. // Determine the width and layout type in use.
  282. $layout = 'two-col-left';
  283. $container_width = 940;
  284. $container_class = '';
  285. if ( is_singular() ) {
  286. $stored_layout = get_post_meta( $post->ID, 'layout', true );
  287. if ( $stored_layout ) { $layout = $stored_layout; }
  288. }
  289. $woo_layouts = get_option( 'woo_layout_' . 'stored_layouts' );
  290. $woo_options = get_option( 'woo_options' );
  291. $db_layout = get_option( 'woo_layout' );
  292. $db_layout_width = get_option( 'woo_layout_width' );
  293. if ( ! empty( $db_layout_width ) ) {
  294. $container_width = intval( $db_layout_width );
  295. if ( $container_width != '940px' ) { $container_class = '-' . $container_width; }
  296. }
  297. $data = array();
  298. $data['layout'] = $layout;
  299. $data['container_width'] = $container_width;
  300. $data['container_class'] = $container_class;
  301. $data['one_percent'] = ( $container_width / 100 );
  302. $data['gutter'] = 20;
  303. $data['width_main'] = ceil( ( $data['one_percent'] * $woo_layout_manager->layouts[$layout]['content'] ) - $data['gutter'] );
  304. if ( isset( $woo_layout_manager->layouts[$layout] ) && array_key_exists( 'primary', $woo_layout_manager->layouts[$layout] ) ) {
  305. $data['width_primary'] = ceil( ( $data['one_percent'] * $woo_layout_manager->layouts[$layout]['primary'] ) - $data['gutter'] );
  306. } else {
  307. $data['width_primary'] = 0;
  308. }
  309. if ( isset( $woo_layout_manager->layouts[$layout] ) && array_key_exists( 'secondary', $woo_layout_manager->layouts[$layout] ) ) {
  310. $data['width_secondary'] = ceil( ( $data['one_percent'] * $woo_layout_manager->layouts[$layout]['secondary'] ) - $data['gutter'] );
  311. } else {
  312. $data['width_secondary'] = 0;
  313. }
  314. if ( is_array( $woo_layouts ) && is_array( $woo_options ) ) {
  315. // If the selected page doesn't have a specific layout, use the default.
  316. if ( ! $stored_layout ) {
  317. if ( ! empty( $db_layout ) ) {
  318. $layout = $db_layout;
  319. }
  320. }
  321. $data['layout'] = $layout;
  322. $data['container_width'] = $container_width;
  323. $data['container_class'] = $container_class;
  324. $data['one_percent'] = ( $container_width / 100 );
  325. $data['gutter'] = 20;
  326. $data['width_main'] = ceil( ( $data['one_percent'] * $woo_layouts['layouts'][$layout]['content'] ) - $data['gutter'] );
  327. $data['width_primary'] = ceil( ( $data['one_percent'] * $woo_layouts['layouts'][$layout]['primary'] ) - $data['gutter'] );
  328. $data['width_secondary'] = ceil( ( $data['one_percent'] * @$woo_layouts['layouts'][$layout]['secondary'] ) - $data['gutter'] );
  329. }
  330. return $data;
  331. } // End get_layout_info()
  332. /*----------------------------------------
  333. load_dynamic_css()
  334. ----------------------------------------
  335. * Load the dynamic CSS data.
  336. ----------------------------------------*/
  337. function load_dynamic_css () {
  338. if ( isset( $_GET['woo-layout-css'] ) && $_GET['woo-layout-css'] == 'load' ) {
  339. header( 'Content-Type: text/css' );
  340. // Determine the width and layout type in use.
  341. $layout = 'two-col-left';
  342. $container_width = 940;
  343. $container_class = '';
  344. $data = $this->get_layout_info();
  345. $layout = $data['layout'];
  346. $container_width = $data['container_width'];
  347. $container_class = $data['container_class'];
  348. $one_percent = $data['one_percent'];
  349. // Setup the default gutter spacing.
  350. $gutter = $data['gutter'];
  351. // Begin output of dynamic CSS.
  352. $css = '';
  353. $width_main = $data['width_main'];
  354. $width_primary = $data['width_primary'];
  355. $width_secondary = $data['width_secondary'];
  356. $width_maincontainer = 0;
  357. if ( $width_secondary ) { $width_maincontainer = ceil( ( $container_width - $width_secondary ) - $gutter ); }
  358. if ( $width_maincontainer ) { $css .= '.' . $layout . $container_class . ' #main-sidebar-container { width: ' . $width_maincontainer . 'px' . '; }' . "\n"; }
  359. if ( $width_main ) { $css .= '.' . $layout . $container_class . ' #main-sidebar-container #main { width: ' . $width_main . 'px' . '; }' . "\n"; }
  360. if ( $width_primary ) { $css .= '.' . $layout . $container_class . ' #main-sidebar-container #sidebar { width: ' . $width_primary . 'px' . '; }' . "\n"; }
  361. if ( $width_secondary ) { $css .= '.' . $layout . $container_class . ' #sidebar-alt { width: ' . $width_secondary . 'px' . '; }' . "\n"; }
  362. // $css .= "\n" . '/* Business Page Template */' . "\n";
  363. /*
  364. if ( $width_main ) { $css .= '.' . $layout . $container_class . ' .business #main-sidebar-container { width: ' . $container_width . 'px' . '; }' . "\n"; }
  365. if ( $width_secondary ) { $css .= '.' . $layout . $container_class . ' .business #main-sidebar-container #main { margin-left: ' . $width_secondary . 'px; }' . "\n"; }
  366. if ( $width_secondary ) { $css .= '.' . $layout . $container_class . ' .business #sidebar-alt { position: relative; }' . "\n"; }
  367. */
  368. $css .= "\n" . '/* Magazine Page Template */' . "\n";
  369. if ( $width_main ) { $css .= '.' . $layout . $container_class . ' .magazine #loopedSlider { width: ' . ( $width_main ) . 'px' . '; }' . "\n"; }
  370. if ( $width_main ) { $css .= '.' . $layout . $container_class . ' .magazine #loopedSlider .container { width: ' . ( $width_main ) . 'px' . '; }' . "\n"; }
  371. echo $css;
  372. die();
  373. }
  374. } // End load_dynamic_css()
  375. /*----------------------------------------
  376. setup_layouts()
  377. ----------------------------------------
  378. * Setup layouts.
  379. ----------------------------------------*/
  380. function setup_layouts() {
  381. $this->gutter = 10;
  382. $this->layouts = array();
  383. // One Column
  384. $one_col = array( 'content' => '100' );
  385. $this->layouts['one-col'] = $one_col;
  386. // Two Columns Left
  387. $two_col_left = array( 'content' => '65', 'primary' => '30' );
  388. $this->layouts['two-col-left'] = $two_col_left;
  389. // Two Columns Right
  390. $two_col_right = array( 'primary' => '30', 'content' => '65' );
  391. $this->layouts['two-col-right'] = $two_col_right;
  392. // Three Columns Left
  393. $three_col_left = array( 'content' => '55', 'primary' => '30', 'secondary' => '15' );
  394. $this->layouts['three-col-left'] = $three_col_left;
  395. // Three Columns Middle
  396. $three_col_middle = array( 'secondary' => '15', 'content' => '55', 'primary' => '30' );
  397. $this->layouts['three-col-middle'] = $three_col_middle;
  398. // Three Columns Right
  399. $three_col_right = array( 'secondary' => '15', 'primary' => '30', 'content' => '55' );
  400. $this->layouts['three-col-right'] = $three_col_right;
  401. // Merge the stored layout information with our current defaults.
  402. $stored_values = get_option( $this->plugin_prefix . 'stored_layouts' );
  403. if ( is_array( $stored_values ) && is_array( $stored_values['layouts'] ) && count( $stored_values['layouts'] ) ) {
  404. foreach ( $stored_values['layouts'] as $k => $v ) {
  405. if ( is_array( $v ) ) {
  406. $this->layouts[$k] = $v;
  407. }
  408. }
  409. }
  410. } // End setup_layouts()
  411. /*----------------------------------------
  412. setup_layout_information()
  413. ----------------------------------------
  414. * Setup layout meta information
  415. * (name, description, etc).
  416. ----------------------------------------*/
  417. function setup_layout_information() {
  418. $this->layouts_info = array();
  419. // One Column
  420. $one_col = array( 'name' => __( 'One Column', 'woothemes' ), 'description' => __( 'One column with no sidebars.', 'woothemes' ) );
  421. // $this->layouts_info['one-col'] = $one_col;
  422. // Two Columns Left
  423. $two_col_left = array( 'name' => __( 'Two Columns (Content on the Left)', 'woothemes' ), 'description' => __( 'Two columns with the main content column on the left.', 'woothemes' ) );
  424. $this->layouts_info['two-col-left'] = $two_col_left;
  425. // Two Columns Right
  426. $two_col_right = array( 'name' => __( 'Two Columns (Content on the Right)', 'woothemes' ), 'description' => __( 'Two columns with the main content column on the right.', 'woothemes' ) );
  427. $this->layouts_info['two-col-right'] = $two_col_right;
  428. // Three Columns Left
  429. $three_col_left = array( 'name' => __( 'Three Columns (Content on the Left)', 'woothemes' ), 'description' => __( 'Three columns with the main content column on the left.', 'woothemes' ) );
  430. $this->layouts_info['three-col-left'] = $three_col_left;
  431. // Three Columns Middle
  432. $three_col_middle = array( 'name' => __( 'Three Columns (Content in the Middle)', 'woothemes' ), 'description' => __( 'Three columns with the main content column in the middle.', 'woothemes' ) );
  433. $this->layouts_info['three-col-middle'] = $three_col_middle;
  434. // Three Columns Right
  435. $three_col_right = array( 'name' => __( 'Three Columns (Content on the Right)', 'woothemes' ), 'description' => __( 'Three columns with the main content column on the right.', 'woothemes' ) );
  436. $this->layouts_info['three-col-right'] = $three_col_right;
  437. } // End setup_layout_information()
  438. /*----------------------------------------
  439. add_exporter_data()
  440. ----------------------------------------
  441. * Add our saved data to the WooFramework
  442. * data exporter.
  443. ----------------------------------------*/
  444. function add_exporter_data ( $data ) {
  445. $data .= " OR option_name = '" . $this->plugin_prefix . "stored_layouts" . "'";
  446. return $data;
  447. } // End add_exporter_data()
  448. } // End Class
  449. ?>