Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 73.69 KB | None | 0 0
  1. <?php
  2. /************* GLOBAL CONTENT WIDTH ***************/
  3. if ( ! isset( $content_width ) ) {
  4.      $content_width = 750;
  5. }
  6.  
  7. if ( ! function_exists( 'cb_adjust_cw' ) ) {
  8.     function cb_adjust_cw() {
  9.  
  10.         global $content_width, $post;
  11.         if ( $post != NULL ) {
  12.             $cb_post_id = $post->ID;
  13.             $cb_fw_post = get_post_meta( $cb_post_id, 'cb_full_width_post', true );
  14.         } else {
  15.             $cb_fw_post = NULL;
  16.         }
  17.         if ( ( is_page_template( 'template-full-width.php' ) ) || ( $cb_fw_post == 'nosidebar' ) ) {
  18.             $content_width = 1140;
  19.         }
  20.     }
  21. }
  22.  
  23. add_theme_support( 'custom-header' );
  24. add_theme_support( 'custom-background' );
  25. add_theme_support( 'woocommerce' );
  26. add_action( 'template_redirect', 'cb_adjust_cw' );
  27.  
  28. /************* LOAD NEEDED FILES ***************/
  29.  
  30. require_once get_template_directory() . '/library/core.php';
  31. require_once get_template_directory() . '/library/translation/translation.php';
  32.  
  33. // Fire Up The Framework
  34. add_filter( 'ot_show_pages', '__return_false' );
  35. add_filter( 'ot_show_new_layout', '__return_false' );
  36. add_filter( 'ot_theme_mode', '__return_true' );
  37. load_template(  get_template_directory()  . '/option-tree/ot-loader.php' );
  38. load_template( get_template_directory(). '/option-tree/includes/meta-boxes.php' );
  39. require_once get_template_directory().'/library/cb-to.php';
  40. require_once get_template_directory().'/library/cb-tgm.php';
  41.  
  42. /************* THUMBNAIL SIZE OPTIONS *************/
  43. add_image_size('cb-80-60', 80, 60, true); // Used on sidebar widgets and small thumbnails
  44. add_image_size('cb-282-232', 282, 232, true ); // Slider 1
  45. add_image_size('cb-300-200', 300, 200, true ); // Used on Style A
  46. add_image_size('cb-300-250', 300, 250, true ); // Used on grids
  47. add_image_size('cb-360-240', 360, 240, true ); // Used on blog style B, Module B, Module C, Latest Post Widget Big Style
  48. add_image_size('cb-480-240', 480, 240, true ); // Used on featured post in mega menu
  49. add_image_size('cb-430-270', 430, 270, true ); // Used on slider widget, top review widget
  50. add_image_size('cb-400-250', 400, 250, true ); // Used on grid 5
  51. add_image_size('cb-600-250', 600, 250, true ); // Used on grid 4 and 6
  52. add_image_size('cb-600-400', 600, 400, true ); // Used on grid 4, 5 and 6
  53. add_image_size('cb-750-400', 750, 400, true ); // Used on standard featured image, slider 2 section b/d
  54. add_image_size('cb-1200-520', 1200, 520, true ); // Used on full-width featured image, slider 2 full-width
  55. add_image_size('cb-1400-700', 1400, 700, true ); // Used on Parallax/Full-background featured images
  56.  
  57. if ( function_exists('buddypress') ) {
  58.  
  59.     if ( !defined( 'BP_AVATAR_FULL_WIDTH' ) ) {
  60.         define ( 'BP_AVATAR_FULL_WIDTH', 300 );
  61.     }
  62.  
  63.     if ( !defined( 'BP_AVATAR_FULL_HEIGHT' ) ) {
  64.         define ( 'BP_AVATAR_FULL_HEIGHT', 300 );
  65.     }
  66.  
  67.     if ( !defined( 'BP_AVATAR_THUMB_HEIGHT' ) ) {
  68.         define ( 'BP_AVATAR_THUMB_HEIGHT', 80 );
  69.     }
  70.  
  71.     if ( !defined( 'BP_AVATAR_THUMB_WIDTH' ) ) {
  72.         define ( 'BP_AVATAR_THUMB_WIDTH', 80 );
  73.     }
  74.  
  75. }
  76.  
  77. /*********************
  78. SCRIPTS & ENQUEUEING
  79. *********************/
  80.  
  81. if (!is_admin()) {
  82.  
  83.     function blog_script() {
  84.         //wp_enqueue_script('script-yandex', get_template_directory_uri() . '/library/js/yandex.js');
  85.     }
  86.     add_action('wp_enqueue_scripts', 'blog_script');
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. if ( ! function_exists( 'cb_script_loaders' ) ) {
  94.     function cb_script_loaders() {
  95.         // enqueue base scripts and styles
  96.         add_action('wp_enqueue_scripts', 'cb_scripts_and_styles', 999);
  97.         // enqueue admin scripts and styles
  98.         add_action('admin_enqueue_scripts', 'cb_post_admin_scripts_and_styles');
  99.         // ie conditional wrapper
  100.         add_filter( 'style_loader_tag', 'cb_ie_conditional', 10, 2 );
  101.     }
  102. }
  103. add_action('after_setup_theme','cb_script_loaders', 15);
  104.  
  105. if ( ! function_exists( 'cb_scripts_and_styles' ) ) {
  106.     function cb_scripts_and_styles() {
  107.       if (!is_admin()) {
  108.         // Modernizr (without media query polyfill)
  109.         wp_register_script( 'cb-modernizr',  get_template_directory_uri(). '/library/js/modernizr.custom.min.js', array(), '2.6.2', false );
  110.         wp_enqueue_script('cb-modernizr'); // enqueue it
  111.         // Register main stylesheet for RTL/LTR
  112.         if ( is_rtl() ) {
  113.             wp_register_style( 'cb-main-stylesheet',  get_template_directory_uri() . '/library/css/style-rtl.css', array(), '3.1', 'all' );
  114.         } else {
  115.             $cb_responsive_style = ot_get_option('cb_responsive_onoff', 'on');
  116.             $cb_max_theme_width = ot_get_option('cb_max_theme_width', 'default');
  117.             if ( $cb_responsive_style == 'on' ) {
  118.                
  119.                 if ( $cb_max_theme_width == 'default' ) {
  120.                     wp_register_style( 'cb-main-stylesheet',  get_template_directory_uri() . '/library/css/style.css', array(), '3.1', 'all' );
  121.                 } else {
  122.                     wp_register_style( 'cb-main-stylesheet',  get_template_directory_uri() . '/library/css/style-1020px.css', array(), '3.1', 'all' );
  123.                 }
  124.  
  125.             } else {
  126.  
  127.                 if ( $cb_max_theme_width == 'default' ) {
  128.                     wp_register_style( 'cb-main-stylesheet',  get_template_directory_uri() . '/library/css/style-unresponsive.css', array(), '3.1', 'all' );
  129.                 } else {
  130.                     wp_register_style( 'cb-main-stylesheet',  get_template_directory_uri() . '/library/css/style-1020px-unresponsive.css', array(), '3.1', 'all' );
  131.                 }
  132.             }
  133.         }
  134.         wp_enqueue_style('cb-main-stylesheet'); // enqueue it
  135.         // Register fonts
  136.         $cb_font = cb_fonts();
  137.         wp_register_style( 'cb-font-stylesheet',  $cb_font[0], array(), '3.1', 'all' );
  138.         wp_enqueue_style('cb-font-stylesheet'); // enqueue it
  139.         // register font awesome stylesheet
  140.         wp_register_style('fontawesome',  get_template_directory_uri(). '/library/css/fontawesome/css/font-awesome.min.css', array(), '4.1.0', 'all');
  141.         wp_enqueue_style('fontawesome'); // enqueue it
  142.         // ie-only stylesheet
  143.         wp_register_style( 'cb-ie-only',  get_template_directory_uri(). '/library/css/ie.css', array(), '3.1' );
  144.         wp_enqueue_style('cb-ie-only'); // enqueue it
  145.         if ( class_exists('Woocommerce') ) {
  146.             wp_register_style( 'cb-woocommerce-stylesheet',  get_template_directory_uri() . '/woocommerce/css/woocommerce.css', array(), '3.1', 'all' );
  147.             wp_enqueue_style('cb-woocommerce-stylesheet'); // enqueue it
  148.         }
  149.         // comment reply script for threaded comments
  150.         if ( is_singular() && comments_open() && ( get_option( 'thread_comments' ) == 1) ) {
  151.             global $wp_scripts;
  152.             $wp_scripts->add_data( 'comment-reply', 'group', 1 );
  153.             wp_enqueue_script( 'comment-reply' ); // enqueue it
  154.         }
  155.         // Load Flexslider
  156.         wp_register_script( 'cb-flexslider',  get_template_directory_uri() . '/library/js/jquery.flexslider-min.js', array( 'jquery' ),'', true);
  157.         wp_enqueue_script( 'cb-flexslider' ); // enqueue it
  158.         if ( is_single() == true) {
  159.             // Load Cookie
  160.             wp_register_script( 'cb-cookie',  get_template_directory_uri() . '/library/js/cookie.min.js', array( 'jquery' ),'', true);
  161.             wp_enqueue_script( 'cb-cookie' ); // enqueue it
  162.         }
  163.         // Load Selectivizr
  164.         wp_register_script( 'cb-select',  get_template_directory_uri() . '/library/js/selectivizr-min.js', array( 'jquery' ),'', true);
  165.         wp_enqueue_script( 'cb-select' ); // enqueue it
  166.         // Load lightbox
  167.         $cb_lightbox_onoff = ot_get_option('cb_lightbox_onoff', 'on');
  168.         if ( $cb_lightbox_onoff != 'off' ) {
  169.             wp_register_script( 'cb-lightbox',  get_template_directory_uri() . '/library/js/jquery.fs.boxer.min.js', array( 'jquery' ),'', true);
  170.             wp_enqueue_script( 'cb-lightbox' ); // enqueue it
  171.         }
  172.         // Load Extra Needed Javascript
  173.         wp_register_script( 'cb-js-ext',  get_template_directory_uri() . '/library/js/jquery.ext.js', array( 'jquery' ),'', true);
  174.         wp_enqueue_script( 'cb-js-ext' ); // enqueue it
  175.  
  176.         // Load scripts
  177.         wp_register_script( 'cb-js',  get_template_directory_uri() . '/library/js/cb-scripts.js', array( 'jquery' ), '3.1' , true);
  178.         wp_enqueue_script( 'cb-js' ); // enqueue it
  179.       }
  180.     }
  181. }
  182.  
  183. if ( ! function_exists( 'cb_post_admin_scripts_and_styles' ) ) {
  184.     function cb_post_admin_scripts_and_styles($hook) {
  185.  
  186.         // loading admin styles only on edit + posts + new posts
  187.         if ( $hook == 'widgets.php' || $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'profile.php' || $hook == 'appearance_page_ot-theme-options' || $hook == 'user-edit.php' || $hook == 'edit-tags.php' ) {
  188.             wp_register_style( 'cb-admin-css',  get_template_directory_uri(). '/library/css/admin.css', array(), '3.1' );
  189.             wp_enqueue_style('cb-admin-css'); // enqueue it
  190.             wp_register_script( 'admin-js',  get_template_directory_uri() . '/library/js/cb-admin.js', array(), '3.1', true);
  191.             wp_enqueue_script( 'admin-js' ); // enqueue it
  192.             wp_enqueue_script( 'suggest' ); // enqueue it
  193.         }
  194.     }
  195. }
  196.  
  197. // adding the conditional wrapper around ie8 stylesheet
  198. // source: Gary Jones - http://code.garyjones.co.uk/ie-conditional-style-sheets-wordpress/
  199. // GPLv2 or newer license
  200. if ( ! function_exists( 'cb_ie_conditional' ) ) {
  201.     function cb_ie_conditional( $tag, $handle ) {
  202.         if ( ( 'cb-ie-only' == $handle ) || ( 'cb-select' == $handle ) ) {
  203.             $tag = '<!--[if lt IE 9]>' . "\n" . $tag . '<![endif]-->' . "\n";
  204.         }
  205.         return $tag;
  206.     }
  207. }
  208.  
  209. // Sidebars & Widgetizes Areas
  210. if ( ! function_exists( 'cb_register_sidebars' ) ) {
  211.     function cb_register_sidebars() {
  212.  
  213.         $cb_footer_layout = ot_get_option('cb_footer_layout', 'cb-footer-a');
  214.  
  215.         // Main Sidebar
  216.         register_sidebar(array(
  217.             'name' => 'Global Sidebar',
  218.             'id' => 'sidebar-global',
  219.             'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  220.             'after_widget' => '</div>',
  221.             'before_title' => '<h3 class="cb-sidebar-widget-title">',
  222.             'after_title' => '</h3>'
  223.         ));
  224.         // After Post
  225.         register_sidebar(array(
  226.             'name' => 'After Post',
  227.             'id' => 'after-post',
  228.             'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  229.             'after_widget' => '</div>',
  230.             'before_title' => '<h3 class="cb-block-title">',
  231.             'after_title' => '</h3>'
  232.         ));
  233.  
  234.         // Footer Widget 1
  235.         register_sidebar( array(
  236.             'name' => 'Footer 1',
  237.             'id' => 'footer-1',
  238.             'before_widget' => '<div id="%1$s" class="cb-footer-widget %2$s">',
  239.             'after_widget' => '</div>',
  240.             'before_title' => '<h3 class="cb-footer-widget-title"><span>',
  241.             'after_title' => '</span></h3>'
  242.         ));
  243.  
  244.         // Footer Widget 2
  245.         register_sidebar( array(
  246.             'name' => 'Footer 2',
  247.             'id' => 'footer-2',
  248.             'before_widget' => '<div id="%1$s" class="cb-footer-widget %2$s">',
  249.             'after_widget' => '</div>',
  250.             'before_title' => '<h3 class="cb-footer-widget-title"><span>',
  251.             'after_title' => '</span></h3>'
  252.         ));
  253.  
  254.         // Footer Widget 3
  255.         register_sidebar( array(
  256.             'name' => 'Footer 3',
  257.             'id' => 'footer-3',
  258.             'before_widget' => '<div id="%1$s" class="cb-footer-widget %2$s">',
  259.             'after_widget' => '</div>',
  260.             'before_title' => '<h3 class="cb-footer-widget-title"><span>',
  261.             'after_title' => '</span></h3>'
  262.         ));
  263.  
  264.         if ( $cb_footer_layout == 'cb-footer-b' ) {
  265.             // Footer Widget 4
  266.             register_sidebar( array(
  267.                 'name' => 'Footer 4',
  268.                 'id' => 'footer-4',
  269.                 'before_widget' => '<div id="%1$s" class="cb-footer-widget %2$s">',
  270.                 'after_widget' => '</div>',
  271.                 'before_title' => '<h3 class="cb-footer-widget-title"><span>',
  272.                 'after_title' => '</span></h3>'
  273.             ));
  274.         }
  275.  
  276.         register_sidebar(
  277.             array(
  278.                 'name' => 'Valenti Multi-Widgets Area',
  279.                 'id' => 'cb_multi_widgets',
  280.                 'description' => '1- Drag multiple widgets here 2- Drag the "Valenti Multi-Widget Widget" to the sidebar where you want to show the multi-widgets.',
  281.                 'before_widget' => '<div id="%1$s" class="widget cb-multi-widget tabbertab %2$s">',
  282.                 'after_widget' => '</div>',
  283.                 'before_title' => '<h3 class="widget-title">',
  284.                 'after_title' => '</h3>'
  285.             )
  286.         );
  287.  
  288.         if ( function_exists( 'bbpress' ) ) {
  289.             register_sidebar( array(
  290.                 'name' => 'bbPress Sidebar',
  291.                 'id' => 'sidebar-bbpress',
  292.                 'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  293.                 'after_widget' => '</div>',
  294.                 'before_title' => '<h3 class="cb-sidebar-widget-title">',
  295.                 'after_title' => '</h3>'
  296.             ));
  297.         }
  298.  
  299.         if ( function_exists( 'buddypress' ) ) {
  300.             register_sidebar( array(
  301.                 'name' => 'BuddyPress Sidebar',
  302.                 'id' => 'sidebar-buddypress',
  303.                 'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  304.                 'after_widget' => '</div>',
  305.                 'before_title' => '<h3 class="cb-sidebar-widget-title">',
  306.                 'after_title' => '</h3>'
  307.             ));
  308.         }
  309.  
  310.         if ( class_exists( 'Woocommerce' ) ) {
  311.             register_sidebar( array(
  312.                 'name' => 'WooCommerce Sidebar',
  313.                 'id' => 'sidebar-woocommerce',
  314.                 'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  315.                 'after_widget' => '</div>',
  316.                 'before_title' => '<h3 class="cb-sidebar-widget-title">',
  317.                 'after_title' => '</h3>'
  318.             ));
  319.         }
  320.  
  321.         $cb_pages = get_pages( array( 'post_status' =>  array('publish', 'pending', 'private', 'draft') ) );
  322.         foreach ( $cb_pages as $page ) {
  323.  
  324.             $cb_custom_fields = get_post_custom($page->ID);
  325.             if ( isset( $cb_custom_fields['cb_page_custom_sidebar'][0] ) ) { $cb_page_sidebar = $cb_custom_fields['cb_page_custom_sidebar'][0]; } else { $cb_page_sidebar = 'off'; }
  326.             if ( isset( $cb_custom_fields['_wp_page_template'][0] ) ) { $cb_page_template = $cb_custom_fields['_wp_page_template'][0]; } else { $cb_page_template = 'off'; }
  327.  
  328.                 if ( ( $cb_page_sidebar == '2' ) && ( $cb_page_template != 'page-valenti-builder.php' ) ) {
  329.                     register_sidebar( array(
  330.                         'name' => $page->post_title .' (Page)',
  331.                         'id' => 'page-'.$page->ID . '-sidebar',
  332.                         'description' => 'This is the ' . $page->post_title . ' sidebar',
  333.                         'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  334.                         'after_widget' => '</div>',
  335.                         'before_title' => '<h3 class="cb-sidebar-widget-title">',
  336.                       'after_title' => '</h3>'
  337.                     ) );
  338.                 }
  339.                 if ( $cb_page_template == 'page-valenti-builder.php' ) {
  340.  
  341.                     // Homepage Section B Sidebar
  342.                     register_sidebar(array(
  343.                         'name' => 'Section B Sidebar ('.$page->post_title .' page)',
  344.                         'id' => 'sidebar-hp-b-'.$page->ID,
  345.                         'description' => 'Page: ' . $page->post_title,
  346.                         'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  347.                         'after_widget' => '</div>',
  348.                         'before_title' => '<h3 class="cb-sidebar-widget-title">',
  349.                         'after_title' => '</h3>'
  350.                     ));
  351.                     // Homepage Section D Sidebar
  352.                     register_sidebar(array(
  353.                         'name' => 'Section D Sidebar (' . $page->post_title . ' page)',
  354.                         'id' => 'sidebar-hp-d-' . $page->ID,
  355.                         'description' => 'This is Sidebar D for ' . $page->post_title,
  356.                         'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  357.                         'after_widget' => '</div>',
  358.                         'before_title' => '<h3 class="cb-sidebar-widget-title">',
  359.                         'after_title' => '</h3>'
  360.                     ));
  361.                 }
  362.         }
  363.  
  364.         if ( function_exists('get_tax_meta') ) {
  365.  
  366.             $cb_categories = get_categories( array( 'hide_empty'=> 0 ) );
  367.  
  368.             foreach ( $cb_categories as $cb_cat ) {
  369.  
  370.                 $cb_cat_onoff = get_tax_meta( $cb_cat->cat_ID, 'cb_cat_sidebar');
  371.  
  372.                 if ( $cb_cat_onoff == 'on' ) {
  373.                             register_sidebar( array(
  374.                                     'name' => $cb_cat->cat_name,
  375.                                     'id' => $cb_cat->category_nicename . '-sidebar',
  376.                                     'description' => 'This is the ' . $cb_cat->cat_name . ' sidebar',
  377.                                     'before_widget' => '<div id="%1$s" class="cb-sidebar-widget %2$s">',
  378.                                     'after_widget' => '</div>',
  379.                                     'before_title' => '<h3 class="cb-sidebar-widget-title">',
  380.                                     'after_title' => '</h3>'
  381.                                 ) );
  382.                 }
  383.  
  384.            }
  385.         }
  386.     }
  387. }
  388.  
  389. add_filter( 'ot_post_formats', '__return_true' );
  390.  
  391. if ( ! function_exists( 'cb_ot_meta_box_post_format_video' ) ) {
  392.     function cb_ot_meta_box_post_format_video() {
  393.         return array(
  394.             'id'        => 'ot-post-format-video',
  395.             'title'     => 'Valenti Post Format: Video',
  396.             'desc'      => '',
  397.             'pages'     => 'post',
  398.             'context'   => 'side',
  399.             'priority'  => 'low',
  400.             'fields'    => array(
  401.               array(
  402.                 'id'      => 'cb_video_embed_code_post',
  403.                 'label'   => '',
  404.                 'desc'    => 'Video iframe embed code',
  405.                 'std'     => '',
  406.                 'type'    => 'textarea'
  407.               )
  408.             )
  409.         );
  410.     }
  411. }
  412. add_filter( 'ot_meta_box_post_format_video', 'cb_ot_meta_box_post_format_video' );
  413.  
  414. if ( ! function_exists( 'cb_ot_meta_box_post_format_gallery' ) ) {
  415.     function cb_ot_meta_box_post_format_gallery() {
  416.         return array(
  417.  
  418.             'id'        => 'ot-post-format-gallery',
  419.             'title'     => 'Valenti Post Format: Gallery',
  420.             'desc'      => '',
  421.             'pages'     => 'post',
  422.             'context'   => 'side',
  423.             'priority'  => 'low',
  424.             'fields'    => array(
  425.              
  426.             )
  427.         );
  428.     }
  429. }
  430. add_filter( 'ot_meta_box_post_format_gallery', 'cb_ot_meta_box_post_format_gallery' );
  431.  
  432. if ( ! function_exists( 'cb_ot_meta_box_post_format_audio' ) ) {
  433.     function cb_ot_meta_box_post_format_audio() {
  434.  
  435.         return array(
  436.             'id'        => 'ot-post-format-audio',
  437.             'title'     => __( 'Valenti Post Format: Audio', 'option-tree' ),
  438.             'desc'      => '',
  439.             'pages'     => 'post',
  440.             'context'   => 'side',
  441.             'priority'  => 'low',
  442.             'fields'    => array(
  443.               array(
  444.                 'id'      => 'cb_soundcloud_embed_code_post',
  445.                 'label'   => '',
  446.                 'desc'    => 'Audio Embed Code',
  447.                 'std'     => '',
  448.                 'type'    => 'textarea'
  449.               )
  450.             )
  451.         );
  452.     }
  453. }
  454. add_filter( 'ot_meta_box_post_format_audio', 'cb_ot_meta_box_post_format_audio' );
  455.  
  456. if ( ! function_exists( 'cb_ot_type_radio_image_src' ) ) {
  457.     function cb_ot_type_radio_image_src( $src ) {
  458.         return  get_template_directory_uri() . '/option-tree/assets/images/options' . $src;
  459.     }
  460. }
  461. add_filter( 'ot_type_radio_image_src', 'cb_ot_type_radio_image_src' );
  462.  
  463. if ( ! function_exists( 'cb_ot_upload_text' ) ) {
  464.     function cb_ot_upload_text() {
  465.         return 'Insert';
  466.     }
  467. }
  468. add_filter( 'ot_upload_text', 'cb_ot_upload_text' );
  469.  
  470. if ( ! function_exists( 'cb_ot_header_version_text' ) ) {
  471.     function cb_ot_header_version_text() {
  472.         return '';
  473.     }
  474. }
  475. add_filter( 'ot_header_version_text', 'cb_ot_header_version_text' );
  476.  
  477. if ( ! function_exists( 'cb_ot_header_logo_link' ) ) {
  478.     function cb_ot_header_logo_link() {
  479.         return '<img src="' . get_template_directory_uri() . '/option-tree/assets/images/logo.png">';
  480.     }
  481. }
  482. add_filter( 'ot_header_logo_link', 'cb_ot_header_logo_link' );
  483.  
  484.  
  485. /*
  486.  *
  487.  *      Age Limit
  488.  *
  489.  * */
  490.  
  491. /* Добавляем блоки в основную колонку на страницах постов и пост. страниц */
  492. if ( ! function_exists( 'agelimit_add_custom_box' ) ) {
  493.     function agelimit_add_custom_box() {
  494.         $screens = array( 'post', 'page' );
  495.         foreach ( $screens as $screen ) {
  496.             add_meta_box( 'agelimit', __( "Age Limit", 'agelimit' ), 'agelimit_meta_box_callback', $screen, 'side' );
  497.         }
  498.     }
  499. }
  500. add_action( 'add_meta_boxes', 'agelimit_add_custom_box' );
  501.  
  502. /* HTML код блока */
  503. if ( ! function_exists( 'agelimit_meta_box_callback' ) ) {
  504.     function agelimit_meta_box_callback() {
  505.         // Используем nonce для верификации
  506.         wp_nonce_field( plugin_basename( __FILE__ ), 'agelimit_noncename' );
  507.  
  508.         global $post;
  509.         $agelimit_value = get_post_meta( $post->ID, 'agelimit_plugin_value', true );
  510.  
  511.         // Поля формы для введения данных
  512.         echo '<label for="agelimit_plugin_value">' . __( "Age Limit", 'agelimit' ) . '</label> ';
  513.         echo '
  514.         <select id="agelimit_plugin_value" name="agelimit_plugin_value">
  515.             <option value="0"> ' . __( "No", 'agelimit' ) . ' </option>
  516.             <option value="3" ' . ( $agelimit_value == '3' ? ' selected="selected" ' : ' ' ) . '> 3+ </option>
  517.             <option value="7" ' . ( $agelimit_value == '7' ? ' selected="selected" ' : ' ' ) . '> 7+ </option>
  518.             <option value="12" ' . ( $agelimit_value == '12' ? ' selected="selected" ' : ' ' ) . '> 12+ </option>
  519.             <option value="16" ' . ( $agelimit_value == '16' ? ' selected="selected" ' : ' ' ) . '> 16+ </option>
  520.             <option value="18" ' . ( $agelimit_value == '18' ? ' selected="selected" ' : ' ' ) . '> 18+ </option>
  521.         </select>
  522.     ';
  523.     }
  524. }
  525.  
  526. /* Сохраняем данные, когда пост сохраняется */
  527. if ( ! function_exists( 'agelimit_save_postdata' ) ) {
  528.     function agelimit_save_postdata( $post_id ) {
  529.         // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  530.         if ( ! wp_verify_nonce( $_POST['agelimit_noncename'], plugin_basename( __FILE__ ) ) ) {
  531.             return $post_id;
  532.         }
  533.  
  534.         // проверяем, если это автосохранение ничего не делаем с данными нашей формы.
  535.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  536.             return $post_id;
  537.         }
  538.  
  539.         // проверяем разрешено ли пользователю указывать эти данные
  540.         if ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) {
  541.             return $post_id;
  542.         } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
  543.             return $post_id;
  544.         }
  545.  
  546.         // Убедимся что поле установлено.
  547.         if ( ! isset( $_POST['agelimit_plugin_value'] ) ) {
  548.             return $post_id;
  549.         }
  550.  
  551.         // Все ОК.
  552.         // Обновляем данные в базе данных.
  553.         update_post_meta( $post_id, 'agelimit_plugin_value', intval( $_POST['agelimit_plugin_value'] ) );
  554.  
  555.         return $post_id;
  556.     }
  557. }
  558. add_action( 'save_post', 'agelimit_save_postdata' );
  559.  
  560. /*Вывод блока ограничений*/
  561. if ( ! function_exists( 'agelimit_box' ) ) {
  562.     function agelimit_box() {
  563.         global $post;
  564.         $agelimit_value = get_post_meta( $post->ID, 'agelimit_plugin_value', true );
  565.         if ( ! empty( $agelimit_value ) ) {
  566.             echo '
  567.             <div class="agelimit_box">' . $agelimit_value . '+</div>
  568.         ';
  569.         }
  570.  
  571.         return;
  572.     }
  573. }
  574. add_shortcode( 'agelimit_box', 'agelimit_box' );
  575.  
  576.  
  577. /*
  578.  *
  579.  *      Game Box
  580.  *
  581.  * */
  582.  
  583. /* Добавляем блоки в основную колонку на страницах постов и пост. страниц */
  584. if ( ! function_exists( 'gamebox_add_custom_box' ) ) {
  585.     function gamebox_add_custom_box() {
  586.         $screens = array( 'post', 'page' );
  587.         foreach ( $screens as $screen ) {
  588.             add_meta_box( 'gamebox', __( "Game Properties", 'gamebox' ), 'gamebox_meta_box_callback', $screen );
  589.         }
  590.     }
  591. }
  592. add_action( 'add_meta_boxes', 'gamebox_add_custom_box' );
  593.  
  594. /* HTML код блока */
  595. if ( ! function_exists( 'gamebox_meta_box_callback' ) ) {
  596.  
  597.     function gamebox_meta_box_callback() {
  598.  
  599.         global $post;
  600.  
  601.         //получаем текущую категорию
  602.         $cur_post        = get_the_category( $post->ID );
  603.         $cur_post        = current( $cur_post );
  604.         $cur_post_cat_id = $cur_post->cat_ID;
  605.  
  606.         //получаем список категорий игр
  607.         $cats = array();
  608.  
  609.         //получаем список категорий
  610.         $categories_info = get_categories(
  611.             array(
  612.                 'child_of'   => 3,       //игры
  613.                 'hide_empty' => 0
  614.             )
  615.         );
  616.         foreach ( (array) $categories_info as $_info ) {
  617.             $cats[ $_info->term_id ] = $_info->term_id;
  618.         }
  619.  
  620.         if ( ! in_array( $cur_post_cat_id, $cats ) ) {
  621.             echo '<span>Current post is not in the category of games.</span>';
  622.  
  623.             return;
  624.         }
  625.  
  626.         // Используем nonce для верификации
  627.         wp_nonce_field( plugin_basename( __FILE__ ), 'gamebox_noncename' );
  628.  
  629.         global $post;
  630.         global $wpdb;
  631.         //проверяем таблицу
  632.         genres_check_tbl();
  633.  
  634.         echo '
  635.             <style>
  636.                 .gamebox_box_input{
  637.                     width: 100%;
  638.                 }
  639.                 .gamebox_timestamp {
  640.                     line-height: 30px;
  641.                 }
  642.                 .gamebox_timestamp input, .gamebox_timestamp select {
  643.                     height: 26px;
  644.                     vertical-align: top;
  645.                 }
  646.                 .gamebox_timestamp .gamebox_dd {
  647.                     width: 2em;
  648.                 }
  649.                 .gamebox_timestamp .gamebox_yyyy {
  650.                     width: 3.4em;
  651.                 }
  652.                 .gamebox_timestamp_platform select {
  653.                     vertical-align: top;
  654.                 }
  655.             </style>
  656.             <div class="gamebox_box">
  657.         ';
  658.  
  659.         //Альтернативное название игры
  660.         echo '
  661.             <label>' . __( 'Alternative Title', 'gamebox' ) . '</label><br/>
  662.             <input class="gamebox_box_input" type="text" name="gamebox_alt_title" value="' . get_post_meta( $post->ID, 'gamebox_alt_title', true ) . '" /><br/>
  663.         ';
  664.  
  665.         //Разработчик
  666.         echo '
  667.             <label>' . __( 'Developer', 'gamebox' ) . '</label><br/>
  668.             <input class="gamebox_box_input" type="text" name="gamebox_developer" value="' . get_post_meta( $post->ID, 'gamebox_developer', true ) . '" /><br/>
  669.         ';
  670.  
  671.         //Издатель
  672.         echo '
  673.             <label>' . __( 'Publisher', 'gamebox' ) . '</label><br/>
  674.             <input class="gamebox_box_input" type="text" name="gamebox_publisher" value="' . get_post_meta( $post->ID, 'gamebox_publisher', true ) . '" /><br/>
  675.         ';
  676.  
  677.         //Издатель ru
  678.         echo '
  679.             <label>' . __( 'Publisher in Russia', 'gamebox' ) . '</label><br/>
  680.             <input class="gamebox_box_input" type="text" name="gamebox_publisher_ru" value="' . get_post_meta( $post->ID, 'gamebox_publisher_ru', true ) . '" /><br/>
  681.         ';
  682.  
  683.         //Дата выхода
  684.         $unix_date = get_post_meta( $post->ID, 'gamebox_date', true );
  685.         if ( ! empty( $unix_date ) ) {
  686.             $mm   = date( "m", $unix_date );
  687.             $dd   = date( "d", $unix_date );
  688.             $yyyy = date( "Y", $unix_date );
  689.         }
  690.         echo '
  691.             <label>' . __( 'Released Date', 'gamebox' ) . '</label><br/>
  692.             <div class="gamebox_timestamp">
  693.                 <select name="gamebox_mm">
  694.                     <option value="00">      </option>
  695.                     <option value="01" ' . ( $mm == '01' ? ' selected="selected" ' : ' ' ) . '>01-Jan</option>
  696.                     <option value="02" ' . ( $mm == '02' ? ' selected="selected" ' : ' ' ) . '>02-Feb</option>
  697.                     <option value="03" ' . ( $mm == '03' ? ' selected="selected" ' : ' ' ) . '>03-Mar</option>
  698.                     <option value="04" ' . ( $mm == '04' ? ' selected="selected" ' : ' ' ) . '>04-Apr</option>
  699.                     <option value="05" ' . ( $mm == '05' ? ' selected="selected" ' : ' ' ) . '>05-May</option>
  700.                     <option value="06" ' . ( $mm == '06' ? ' selected="selected" ' : ' ' ) . '>06-Jun</option>
  701.                     <option value="07" ' . ( $mm == '07' ? ' selected="selected" ' : ' ' ) . '>07-Jul</option>
  702.                     <option value="08" ' . ( $mm == '08' ? ' selected="selected" ' : ' ' ) . '>08-Aug</option>
  703.                     <option value="09" ' . ( $mm == '09' ? ' selected="selected" ' : ' ' ) . '>09-Sep</option>
  704.                     <option value="10" ' . ( $mm == '10' ? ' selected="selected" ' : ' ' ) . '>10-Oct</option>
  705.                     <option value="11" ' . ( $mm == '11' ? ' selected="selected" ' : ' ' ) . '>11-Nov</option>
  706.                     <option value="12" ' . ( $mm == '12' ? ' selected="selected" ' : ' ' ) . '>12-Dec</option>
  707.                 </select>
  708.                 <input type="text" autocomplete="off" maxlength="2" size="2" value="' . $dd . '" name="gamebox_dd" class="gamebox_dd">,
  709.                 <input type="text" autocomplete="off" maxlength="4" size="4" value="' . $yyyy . '" name="gamebox_yyyy" class="gamebox_yyyy">
  710.             </div>
  711.         ';
  712.  
  713.         //Дата выхода ru
  714.         $unix_date = get_post_meta( $post->ID, 'gamebox_date_ru', true );
  715.         if ( ! empty( $unix_date ) ) {
  716.             $mm   = date( "m", $unix_date );
  717.             $dd   = date( "d", $unix_date );
  718.             $yyyy = date( "Y", $unix_date );
  719.         }
  720.         echo '
  721.             <label>' . __( 'Released Date in Russia', 'gamebox' ) . '</label><br/>
  722.             <div class="gamebox_timestamp">
  723.                 <select name="gamebox_mm_ru">
  724.                     <option value="00">      </option>
  725.                     <option value="01" ' . ( $mm == '01' ? ' selected="selected" ' : ' ' ) . '>01-Jan</option>
  726.                     <option value="02" ' . ( $mm == '02' ? ' selected="selected" ' : ' ' ) . '>02-Feb</option>
  727.                     <option value="03" ' . ( $mm == '03' ? ' selected="selected" ' : ' ' ) . '>03-Mar</option>
  728.                     <option value="04" ' . ( $mm == '04' ? ' selected="selected" ' : ' ' ) . '>04-Apr</option>
  729.                     <option value="05" ' . ( $mm == '05' ? ' selected="selected" ' : ' ' ) . '>05-May</option>
  730.                     <option value="06" ' . ( $mm == '06' ? ' selected="selected" ' : ' ' ) . '>06-Jun</option>
  731.                     <option value="07" ' . ( $mm == '07' ? ' selected="selected" ' : ' ' ) . '>07-Jul</option>
  732.                     <option value="08" ' . ( $mm == '08' ? ' selected="selected" ' : ' ' ) . '>08-Aug</option>
  733.                     <option value="09" ' . ( $mm == '09' ? ' selected="selected" ' : ' ' ) . '>09-Sep</option>
  734.                     <option value="10" ' . ( $mm == '10' ? ' selected="selected" ' : ' ' ) . '>10-Oct</option>
  735.                     <option value="11" ' . ( $mm == '11' ? ' selected="selected" ' : ' ' ) . '>11-Nov</option>
  736.                     <option value="12" ' . ( $mm == '12' ? ' selected="selected" ' : ' ' ) . '>12-Dec</option>
  737.                 </select>
  738.                 <input type="text" autocomplete="off" maxlength="2" size="2" value="' . $dd . '" name="gamebox_dd_ru" class="gamebox_dd">,
  739.                 <input type="text" autocomplete="off" maxlength="4" size="4" value="' . $yyyy . '" name="gamebox_yyyy_ru" class="gamebox_yyyy">
  740.             </div>
  741.         ';
  742.         echo "<br>";
  743.  
  744.         //Жанр
  745.         $genres_data = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "genres WHERE 1", "ARRAY_A" );
  746.         $genres_post = explode( ",", get_post_meta( $post->ID, 'gamebox_genres', true ) );
  747.         echo '
  748.             <label>' . __( 'Genres', 'gamebox' ) . '</label><br/>
  749.         ';
  750.         foreach ( (array) $genres_data as $_name => $_info ) {
  751.             $checked = "";
  752.             if ( in_array( $_info["id"], $genres_post ) ) {
  753.                 $checked = ' checked="checked" ';
  754.             }
  755.             echo '
  756.                 <label class="selectit"><input type="checkbox" ' . $checked . ' name="gamebox_genres[]" value="' . $_info["id"] . '"> ' . $_info["name"] . '</label><br/>
  757.             ';
  758.         }
  759.         echo "<br>";
  760.  
  761.         //Платформа
  762.         $platforms_info  = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "platforms WHERE 1", "ARRAY_A" );
  763.         $platforms_post  = explode( ",", get_post_meta( $post->ID, 'gamebox_platforms', true ) );
  764.         $platforms_dates = get_post_meta( $post->ID, 'gamebox_platforms_dates', true );
  765.  
  766.         echo '
  767.             <label>' . __( 'Platforms', 'gamebox' ) . '</label><br/>
  768.         ';
  769.         foreach ( (array) $platforms_info as $_name => $_info ) {
  770.             $checked = "";
  771.             $mm      = "";
  772.             $dd      = "";
  773.             $yyyy    = "";
  774.             if ( in_array( $_info["id"], $platforms_post ) ) {
  775.                 $checked = ' checked="checked" ';
  776.             }
  777.  
  778.             if ( ! empty( $platforms_dates[ $_info["id"] ] ) ) {
  779.                 $mm   = date( "m", $platforms_dates[ $_info["id"] ] );
  780.                 $dd   = date( "d", $platforms_dates[ $_info["id"] ] );
  781.                 $yyyy = date( "Y", $platforms_dates[ $_info["id"] ] );
  782.             }
  783.  
  784.             echo '
  785.                 <div>
  786.                     <label class="selectit" style="float:left; min-width:200px;"><input type="checkbox" ' . $checked . ' name="gamebox_platforms[' . $_info["id"] . ']" value="' . $_info["id"] . '"> ' . $_info["name"] . '</label>
  787.                     <div class="gamebox_timestamp_platform">
  788.                         <select name="gamebox_platform_mm[' . $_info["id"] . ']">
  789.                             <option value="00">      </option>
  790.                             <option value="01" ' . ( $mm == '01' ? ' selected="selected" ' : ' ' ) . '>01-Jan</option>
  791.                             <option value="02" ' . ( $mm == '02' ? ' selected="selected" ' : ' ' ) . '>02-Feb</option>
  792.                             <option value="03" ' . ( $mm == '03' ? ' selected="selected" ' : ' ' ) . '>03-Mar</option>
  793.                             <option value="04" ' . ( $mm == '04' ? ' selected="selected" ' : ' ' ) . '>04-Apr</option>
  794.                             <option value="05" ' . ( $mm == '05' ? ' selected="selected" ' : ' ' ) . '>05-May</option>
  795.                             <option value="06" ' . ( $mm == '06' ? ' selected="selected" ' : ' ' ) . '>06-Jun</option>
  796.                             <option value="07" ' . ( $mm == '07' ? ' selected="selected" ' : ' ' ) . '>07-Jul</option>
  797.                             <option value="08" ' . ( $mm == '08' ? ' selected="selected" ' : ' ' ) . '>08-Aug</option>
  798.                             <option value="09" ' . ( $mm == '09' ? ' selected="selected" ' : ' ' ) . '>09-Sep</option>
  799.                             <option value="10" ' . ( $mm == '10' ? ' selected="selected" ' : ' ' ) . '>10-Oct</option>
  800.                             <option value="11" ' . ( $mm == '11' ? ' selected="selected" ' : ' ' ) . '>11-Nov</option>
  801.                             <option value="12" ' . ( $mm == '12' ? ' selected="selected" ' : ' ' ) . '>12-Dec</option>
  802.                         </select>
  803.                         <input type="text" autocomplete="off" maxlength="2" size="2" value="' . $dd . '" name="gamebox_platform_dd[' . $_info["id"] . ']" class="gamebox_platform_dd">,
  804.                         <input type="text" autocomplete="off" maxlength="4" size="4" value="' . $yyyy . '" name="gamebox_platform_yyyy[' . $_info["id"] . ']" class="gamebox_platform_yyyy">
  805.                     </div>
  806.                 </div>
  807.             ';
  808.  
  809.         }
  810.         echo "<br>";
  811.         echo '</div>';
  812.     }
  813. }
  814.  
  815. /* Сохраняем данные, когда пост сохраняется */
  816. if ( ! function_exists( 'gamebox_save_postdata' ) ) {
  817.     function gamebox_save_postdata( $post_id ) {
  818.         // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  819.         if ( ! wp_verify_nonce( $_POST['gamebox_noncename'], plugin_basename( __FILE__ ) ) ) {
  820.             return $post_id;
  821.         }
  822.  
  823.         // проверяем, если это автосохранение ничего не делаем с данными нашей формы.
  824.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  825.             return $post_id;
  826.         }
  827.  
  828.         // проверяем разрешено ли пользователю указывать эти данные
  829.         if ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) {
  830.             return $post_id;
  831.         } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
  832.             return $post_id;
  833.         }
  834.  
  835.         // Обновляем данные в базе данных.
  836.         if ( isset( $_POST['gamebox_alt_title'] ) ) {
  837.             update_post_meta( $post_id, 'gamebox_alt_title', sanitize_text_field( $_POST['gamebox_alt_title'] ) );
  838.         }
  839.         if ( isset( $_POST['gamebox_developer'] ) ) {
  840.             update_post_meta( $post_id, 'gamebox_developer', sanitize_text_field( $_POST['gamebox_developer'] ) );
  841.         }
  842.         if ( isset( $_POST['gamebox_publisher'] ) ) {
  843.             update_post_meta( $post_id, 'gamebox_publisher', sanitize_text_field( $_POST['gamebox_publisher'] ) );
  844.         }
  845.         if ( isset( $_POST['gamebox_publisher_ru'] ) ) {
  846.             update_post_meta( $post_id, 'gamebox_publisher_ru', sanitize_text_field( $_POST['gamebox_publisher_ru'] ) );
  847.         }
  848.         if ( isset( $_POST['gamebox_genres'] ) && ! empty( $_POST['gamebox_genres'] ) ) {
  849.             update_post_meta( $post_id, 'gamebox_genres', implode( ",", $_POST['gamebox_genres'] ) );
  850.         }
  851.         if ( isset( $_POST['gamebox_mm'] ) && isset( $_POST['gamebox_dd'] ) && isset( $_POST['gamebox_yyyy'] ) ) {
  852.             $unix_date = "";
  853.             $mm        = intval( $_POST['gamebox_mm'] );
  854.             $dd        = intval( $_POST['gamebox_dd'] );
  855.             $yyyy      = intval( $_POST['gamebox_yyyy'] );
  856.             if ( ! empty( $dd ) && $dd < 32 && ! empty( $mm ) && $mm < 13 && ! empty( $yyyy ) && $yyyy > 1970 ) {
  857.                 $unix_date = mktime( 0, 0, 0, $mm, $dd, $yyyy );
  858.             }
  859.  
  860.             if ( ! empty( $unix_date ) ) {
  861.                 update_post_meta( $post_id, 'gamebox_date', $unix_date );
  862.             }
  863.         }
  864.         if ( isset( $_POST['gamebox_mm_ru'] ) && isset( $_POST['gamebox_dd_ru'] ) && isset( $_POST['gamebox_yyyy_ru'] ) ) {
  865.             $unix_date = "";
  866.             $mm        = intval( $_POST['gamebox_mm_ru'] );
  867.             $dd        = intval( $_POST['gamebox_dd_ru'] );
  868.             $yyyy      = intval( $_POST['gamebox_yyyy_ru'] );
  869.             if ( ! empty( $dd ) && $dd < 32 && ! empty( $mm ) && $mm < 13 && ! empty( $yyyy ) && $yyyy > 1970 ) {
  870.                 $unix_date = mktime( 0, 0, 0, $mm, $dd, $yyyy );
  871.             }
  872.  
  873.             if ( ! empty( $unix_date ) ) {
  874.                 update_post_meta( $post_id, 'gamebox_date_ru', $unix_date );
  875.             }
  876.         }
  877.         if ( isset( $_POST['gamebox_platforms'] ) && ! empty( $_POST['gamebox_platforms'] ) ) {
  878.  
  879.             //сохраняем список платформ
  880.             update_post_meta( $post_id, 'gamebox_platforms', implode( ",", $_POST['gamebox_platforms'] ) );
  881.  
  882.             //сохраняем список дат для платформ
  883.             foreach ( (array) $_POST['gamebox_platforms'] as $_platform_key => $_platform_id ) {
  884.                 $mm   = intval( $_POST['gamebox_platform_mm'][ $_platform_id ] );
  885.                 $dd   = intval( $_POST['gamebox_platform_dd'][ $_platform_id ] );
  886.                 $yyyy = intval( $_POST['gamebox_platform_yyyy'][ $_platform_id ] );
  887.  
  888.                 if ( ! empty( $dd ) && $dd < 32 && ! empty( $mm ) && $mm < 13 && ! empty( $yyyy ) && $yyyy > 1970 ) {
  889.                     $result_data[ $_platform_id ] = mktime( 0, 0, 0, $mm, $dd, $yyyy );
  890.                 }
  891.  
  892.             }
  893.             if ( ! empty( $result_data ) ) {
  894.                 update_post_meta( $post_id, 'gamebox_platforms_dates', $result_data );
  895.             }
  896.         }
  897.  
  898.         return $post_id;
  899.     }
  900. }
  901. add_action( 'save_post', 'gamebox_save_postdata' );
  902.  
  903.  
  904. /*
  905.  *
  906.  *      Genres List
  907.  *
  908.  * */
  909.  
  910. /* Добавляем в меню */
  911. if ( ! function_exists( 'register_genres_options_page' ) ) {
  912.     function register_genres_options_page() {
  913.         add_submenu_page( 'edit.php', __( 'Genres', 'gamebox' ), __( 'Genres', 'gamebox' ), 'manage_options', 'genres', 'genres_options_callback' );
  914.     }
  915. }
  916. add_action( 'admin_menu', 'register_genres_options_page' );
  917.  
  918. /* HTML код блока */
  919. if ( ! function_exists( 'genres_options_callback' ) ) {
  920.     function genres_options_callback() {
  921.  
  922.         //страница редактирования
  923.         if ( ! empty( $_GET["action"] ) && $_GET["action"] == "edit_genre" ) {
  924.             genres_edit_form();
  925.  
  926.             return;
  927.         }
  928.  
  929.         //получаем данные
  930.         $genres_data = get_genres_data();
  931.  
  932.         // контент страницы
  933.         echo '<h2>' . __( "Genres", "gamebox" ) . '</h2>';
  934.         echo '
  935.             <div class="form-wrap" style="width: 500px;">
  936.                 <h3>' . __( "Add Genre", "gamebox" ) . '</h3>
  937.                 <form method="post">
  938.                     <div class="form-field">
  939.                         <label for="genre-name">' . __( "Name", "gamebox" ) . '</label>
  940.                         <input type="text" size="40" value="" id="genre-name" name="new_genre_name" style="width: 500px;">
  941.                     </div>
  942.  
  943.                     <p class="submit">
  944.                         <input type="submit" value="' . __( "Add Genre", "gamebox" ) . '" class="button button-primary" id="submit" name="add_genre">
  945.                     </p>
  946.                 </form>
  947.             </div>
  948.         ';
  949.         //вывод существующих
  950.         if ( ! empty( $genres_data ) ) {
  951.             echo '
  952.                 <table class="wp-list-table widefat fixed tags" style="width: 500px;">
  953.                     <thead>
  954.                         <tr>
  955.                             <th style="" class="manage-column column-name" id="name" scope="col">
  956.                                 <span>' . __( "Name", "gamebox" ) . '</span>
  957.                             </th>
  958.                         </tr>
  959.                     </thead>
  960.  
  961.                     <tbody data-wp-lists="list:tag" id="the-list">
  962.             ';
  963.  
  964.             $i = 0;
  965.             foreach ( (array) $genres_data as $_info ) {
  966.                 $alternate_class = $i % 2 == 0 ? "alternate" : "";
  967.                 echo '
  968.                         <tr class="' . $alternate_class . '" id="tag-3">
  969.                             <td class="name column-name">
  970.                                 <strong>
  971.                                     <a title="Edit" href="edit.php?page=genres&action=edit_genre&genre_id=' . $_info["id"] . '" class="row-title">
  972.                                         ' . $_info["name"] . '
  973.                                     </a>
  974.                                 </strong>
  975.                                 <br>
  976.  
  977.                                 <div class="row-actions">
  978.                                     <span class="edit">
  979.                                         <a href="edit.php?page=genres&action=edit_genre&genre_id=' . $_info["id"] . '">
  980.                                             ' . __( "Edit", "gamebox" ) . '
  981.                                         </a> |
  982.                                     </span>
  983.                                     <span class="delete">
  984.                                         <a onclick="return confirm(\'Are you sure?\')" href="edit.php?page=genres&action=delete_genre&genre_id=' . $_info["id"] . '">
  985.                                             ' . __( "Delete", "gamebox" ) . '
  986.                                         </a>
  987.                                     </span>
  988.                                 </div>
  989.                             </td>
  990.                         </tr>
  991.                 ';
  992.                 $i ++;
  993.             }
  994.             echo '
  995.                     </tbody>
  996.                 </table>
  997.             ';
  998.         }
  999.  
  1000.         return;
  1001.     }
  1002. }
  1003.  
  1004. /* HTML код блока редактирования */
  1005. if ( ! function_exists( 'genres_edit_form' ) ) {
  1006.     function genres_edit_form() {
  1007.         $location = 'edit.php?page=genres';
  1008.  
  1009.         $genre_id = intval( $_GET["genre_id"] );
  1010.         if ( empty( $genre_id ) ) {
  1011.             wp_redirect( $location );
  1012.         }
  1013.  
  1014.         //получаем данные
  1015.         $genre_data = get_genres_data($genre_id);
  1016.         $genre_data = $genre_data[0];
  1017.         if ( empty( $genre_data ) ) {
  1018.             wp_redirect( $location );
  1019.         }
  1020.  
  1021.         echo '<h2>' . __( "Edit Genre", "gamebox" ) . '</h2>';
  1022.         echo '
  1023.             <div class="form-wrap" style="width: 500px;">
  1024.                 <form method="post" action="edit.php?page=genres&action=update_genre&genre_id=' . $genre_data["id"] . '">
  1025.                     <div class="form-field">
  1026.                         <label for="genre-name">' . __( "Name", "gamebox" ) . '</label>
  1027.                         <input type="text" size="40" value="' . $genre_data["name"] . '" id="genre-name" name="new_genre_name" style="width: 500px;">
  1028.                     </div>
  1029.  
  1030.                     <p class="submit">
  1031.                         <input type="submit" value="' . __( "Update Genre", "gamebox" ) . '" class="button button-primary" id="submit" name="update_genre">
  1032.                     </p>
  1033.                 </form>
  1034.             </div>
  1035.         ';
  1036.  
  1037.         return;
  1038.     }
  1039. }
  1040.  
  1041. /* Проверка существования таблицы*/
  1042. if ( ! function_exists( 'genres_check_tbl' ) ) {
  1043.     function genres_check_tbl() {
  1044.         global $wpdb;
  1045.         $table_name = $wpdb->prefix . "genres";
  1046.         if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
  1047.             $sql = "
  1048.                 CREATE TABLE " . $table_name . " (
  1049.                 id mediumint(9) NOT NULL AUTO_INCREMENT,
  1050.                 name tinytext NOT NULL,
  1051.                 UNIQUE KEY id (id)
  1052.             );";
  1053.             require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1054.             dbDelta( $sql );
  1055.         }
  1056.  
  1057.         return true;
  1058.     }
  1059. }
  1060.  
  1061. /* Сохраняем данные */
  1062. if ( ! function_exists( 'genres_edit' ) ) {
  1063.     function genres_edit() {
  1064.         global $wpdb;
  1065.         $location   = 'edit.php?page=genres';
  1066.         $table_name = $wpdb->prefix . "genres";
  1067.  
  1068.         //проверяем таблицу
  1069.         genres_check_tbl();
  1070.  
  1071.         //ADD
  1072.         if ( isset( $_POST["add_genre"] ) && ! empty( $_POST["new_genre_name"] ) ) {
  1073.             $wpdb->insert(
  1074.                 $table_name,
  1075.                 array( 'name' => esc_sql( $_POST["new_genre_name"] ) )
  1076.             );
  1077.         }
  1078.         //DELETE
  1079.         if ( $_GET["action"] == "delete_genre" ) {
  1080.             $genre_id = intval( $_GET["genre_id"] );
  1081.             if ( ! empty( $genre_id ) ) {
  1082.                 $wpdb->delete( $table_name, array( 'ID' => $genre_id ) );
  1083.             }
  1084.             wp_redirect( $location );
  1085.         }
  1086.         //UPDATE
  1087.         if ( $_GET["action"] == "update_genre" && ! empty( $_POST["new_genre_name"] ) ) {
  1088.             $genre_id = intval( $_GET["genre_id"] );
  1089.             if ( empty( $genre_id ) ) {
  1090.                 wp_redirect( $location );
  1091.             }
  1092.  
  1093.             //обновляем данные
  1094.             $wpdb->update( $table_name,
  1095.                 array( 'name' => esc_sql( $_POST["new_genre_name"] ) ),
  1096.                 array( 'ID' => $genre_id ),
  1097.                 array( '%s' ),
  1098.                 array( '%d' )
  1099.             );
  1100.             wp_redirect( $location );
  1101.         }
  1102.     }
  1103. }
  1104. add_action( 'admin_menu', 'genres_edit' );
  1105.  
  1106. /* Получаем данные */
  1107. if ( ! function_exists( 'get_genres_data' ) ) {
  1108.     function get_genres_data($id = "") {
  1109.         global $wpdb;
  1110.  
  1111.         //проверяем таблицу
  1112.         genres_check_tbl();
  1113.  
  1114.         if(!empty($id)){
  1115.             $where_id = " AND id=".intval($id);
  1116.         }
  1117.  
  1118.         //получаем данные
  1119.         $genres_data = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "genres WHERE 1".$where_id, "ARRAY_A" );
  1120.  
  1121.         return $genres_data;
  1122.     }
  1123. }
  1124.  
  1125. /*
  1126.  *
  1127.  *      Platforms List
  1128.  *
  1129.  * */
  1130.  
  1131. /* Добавляем в меню */
  1132. if ( ! function_exists( 'register_platforms_options_page' ) ) {
  1133.     function register_platforms_options_page() {
  1134.         add_submenu_page( 'edit.php', __( 'Platforms', 'gamebox' ), __( 'Platforms', 'gamebox' ), 'manage_options', 'platforms', 'platforms_options_callback' );
  1135.     }
  1136. }
  1137. add_action( 'admin_menu', 'register_platforms_options_page' );
  1138.  
  1139. /* HTML код блока */
  1140. if ( ! function_exists( 'platforms_options_callback' ) ) {
  1141.     function platforms_options_callback() {
  1142.  
  1143.         //страница редактирования
  1144.         if ( ! empty( $_GET["action"] ) && $_GET["action"] == "edit_platform" ) {
  1145.             platforms_edit_form();
  1146.             return;
  1147.         }
  1148.  
  1149.         //получаем данные
  1150.         $platforms_data = get_platforms_data();
  1151.  
  1152.         // контент страницы
  1153.         echo '<h2>' . __( "Platforms", "gamebox" ) . '</h2>';
  1154.         echo '
  1155.             <div class="form-wrap" style="width: 500px;">
  1156.                 <h3>' . __( "Add Platform", "gamebox" ) . '</h3>
  1157.                 <form method="post">
  1158.                     <div class="form-field">
  1159.                         <label for="platform-name">' . __( "Name", "gamebox" ) . '</label>
  1160.                         <input type="text" size="40" value="" id="platform-name" name="new_platform_name" style="width: 500px;">
  1161.                     </div>
  1162.  
  1163.                     <p class="submit">
  1164.                         <input type="submit" value="' . __( "Add Platform", "gamebox" ) . '" class="button button-primary" id="submit" name="add_platform">
  1165.                     </p>
  1166.                 </form>
  1167.             </div>
  1168.         ';
  1169.         //вывод существующих
  1170.         if ( ! empty( $platforms_data ) ) {
  1171.             echo '
  1172.                 <table class="wp-list-table widefat fixed tags" style="width: 500px;">
  1173.                     <thead>
  1174.                         <tr>
  1175.                             <th style="" class="manage-column column-name" id="name" scope="col">
  1176.                                 <span>' . __( "Name", "gamebox" ) . '</span>
  1177.                             </th>
  1178.                         </tr>
  1179.                     </thead>
  1180.  
  1181.                     <tbody data-wp-lists="list:tag" id="the-list">
  1182.             ';
  1183.  
  1184.             $i = 0;
  1185.             foreach ( (array) $platforms_data as $_info ) {
  1186.                 $alternate_class = $i % 2 == 0 ? "alternate" : "";
  1187.                 echo '
  1188.                         <tr class="' . $alternate_class . '" id="tag-3">
  1189.                             <td class="name column-name">
  1190.                                 <strong>
  1191.                                     <a title="Edit" href="edit.php?page=platforms&action=edit_platform&platform_id=' . $_info["id"] . '" class="row-title">
  1192.                                         ' . $_info["name"] . '
  1193.                                     </a>
  1194.                                 </strong>
  1195.                                 <br>
  1196.  
  1197.                                 <div class="row-actions">
  1198.                                     <span class="edit">
  1199.                                         <a href="edit.php?page=platforms&action=edit_platform&platform_id=' . $_info["id"] . '">
  1200.                                             ' . __( "Edit", "gamebox" ) . '
  1201.                                         </a> |
  1202.                                     </span>
  1203.                                     <span class="delete">
  1204.                                         <a onclick="return confirm(\'Are you sure?\')" href="edit.php?page=platforms&action=delete_platform&platform_id=' . $_info["id"] . '">
  1205.                                             ' . __( "Delete", "gamebox" ) . '
  1206.                                         </a>
  1207.                                     </span>
  1208.                                 </div>
  1209.                             </td>
  1210.                         </tr>
  1211.                 ';
  1212.                 $i ++;
  1213.             }
  1214.             echo '
  1215.                     </tbody>
  1216.                 </table>
  1217.             ';
  1218.         }
  1219.  
  1220.         return;
  1221.     }
  1222. }
  1223.  
  1224. /* HTML код блока редактирования */
  1225. if ( ! function_exists( 'platforms_edit_form' ) ) {
  1226.     function platforms_edit_form() {
  1227.         $location = 'edit.php?page=platforms';
  1228.  
  1229.         $platform_id = intval( $_GET["platform_id"] );
  1230.         if ( empty( $platform_id ) ) {
  1231.             wp_redirect( $location );
  1232.         }
  1233.  
  1234.         //получаем данные
  1235.         $platform_data = get_platforms_data($platform_id);
  1236.         $platform_data = $platform_data[0];
  1237.         if ( empty( $platform_data ) ) {
  1238.             wp_redirect( $location );
  1239.         }
  1240.  
  1241.         echo '<h2>' . __( "Edit Platform", "gamebox" ) . '</h2>';
  1242.         echo '
  1243.             <div class="form-wrap" style="width: 500px;">
  1244.                 <form method="post" action="edit.php?page=platforms&action=update_platform&platform_id=' . $platform_data["id"] . '">
  1245.                     <div class="form-field">
  1246.                         <label for="platform-name">' . __( "Name", "gamebox" ) . '</label>
  1247.                         <input type="text" size="40" value="' . $platform_data["name"] . '" id="platform-name" name="new_platform_name" style="width: 500px;">
  1248.                     </div>
  1249.  
  1250.                     <p class="submit">
  1251.                         <input type="submit" value="' . __( "Update Platform", "gamebox" ) . '" class="button button-primary" id="submit" name="update_platform">
  1252.                     </p>
  1253.                 </form>
  1254.             </div>
  1255.         ';
  1256.  
  1257.         return;
  1258.     }
  1259. }
  1260.  
  1261. /* Проверка существования таблицы*/
  1262. if ( ! function_exists( 'platforms_check_tbl' ) ) {
  1263.     function platforms_check_tbl() {
  1264.         global $wpdb;
  1265.         $table_name = $wpdb->prefix . "platforms";
  1266.         if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) != $table_name ) {
  1267.             $sql = "
  1268.                 CREATE TABLE " . $table_name . " (
  1269.                 id mediumint(9) NOT NULL AUTO_INCREMENT,
  1270.                 name tinytext NOT NULL,
  1271.                 UNIQUE KEY id (id)
  1272.             );";
  1273.             require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  1274.             dbDelta( $sql );
  1275.         }
  1276.  
  1277.         return true;
  1278.     }
  1279. }
  1280.  
  1281. /* Сохраняем данные */
  1282. if ( ! function_exists( 'platforms_edit' ) ) {
  1283.     function platforms_edit() {
  1284.         global $wpdb;
  1285.         $location   = 'edit.php?page=platforms';
  1286.         $table_name = $wpdb->prefix . "platforms";
  1287.  
  1288.         //проверяем таблицу
  1289.         platforms_check_tbl();
  1290.  
  1291.         //ADD
  1292.         if ( isset( $_POST["add_platform"] ) && ! empty( $_POST["new_platform_name"] ) ) {
  1293.             $wpdb->insert(
  1294.                 $table_name,
  1295.                 array( 'name' => esc_sql( $_POST["new_platform_name"] ) )
  1296.             );
  1297.         }
  1298.         //DELETE
  1299.         if ( $_GET["action"] == "delete_platform" ) {
  1300.             $platform_id = intval( $_GET["platform_id"] );
  1301.             if ( ! empty( $platform_id ) ) {
  1302.                 $wpdb->delete( $table_name, array( 'ID' => $platform_id ) );
  1303.             }
  1304.             wp_redirect( $location );
  1305.         }
  1306.         //UPDATE
  1307.         if ( $_GET["action"] == "update_platform" && ! empty( $_POST["new_platform_name"] ) ) {
  1308.             $platform_id = intval( $_GET["platform_id"] );
  1309.             if ( empty( $platform_id ) ) {
  1310.                 wp_redirect( $location );
  1311.             }
  1312.  
  1313.             //обновляем данные
  1314.             $wpdb->update( $table_name,
  1315.                 array( 'name' => esc_sql( $_POST["new_platform_name"] ) ),
  1316.                 array( 'ID' => $platform_id ),
  1317.                 array( '%s' ),
  1318.                 array( '%d' )
  1319.             );
  1320.             wp_redirect( $location );
  1321.         }
  1322.     }
  1323. }
  1324. add_action( 'admin_menu', 'platforms_edit' );
  1325.  
  1326. /* Получаем данные */
  1327. if ( ! function_exists( 'get_platforms_data' ) ) {
  1328.     function get_platforms_data($id = "") {
  1329.         global $wpdb;
  1330.  
  1331.         //проверяем таблицу
  1332.         platforms_check_tbl();
  1333.  
  1334.         if(!empty($id)){
  1335.             $where_id = " AND id=".intval($id);
  1336.         }
  1337.  
  1338.         //получаем данные
  1339.         $platforms_data = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "platforms WHERE 1".$where_id, "ARRAY_A" );
  1340.  
  1341.         return $platforms_data;
  1342.     }
  1343. }
  1344.  
  1345.  
  1346. /*
  1347.  *
  1348.  *      Game Link
  1349.  *
  1350.  * */
  1351.  
  1352. /* Добавляем блоки в основную колонку на страницах постов и пост. страниц */
  1353. if ( ! function_exists( 'gamelink_add_custom_box' ) ) {
  1354.     function gamelink_add_custom_box() {
  1355.         $screens = array( 'post', 'page' );
  1356.         foreach ( $screens as $screen ) {
  1357.             add_meta_box( 'gamelink', __( "Linked to Game", 'gamelink' ), 'gamelink_meta_box_callback', $screen );
  1358.         }
  1359.     }
  1360. }
  1361. add_action( 'add_meta_boxes', 'gamelink_add_custom_box' );
  1362.  
  1363. /* HTML код блока */
  1364. if ( ! function_exists( 'gamelink_meta_box_callback' ) ) {
  1365.     function gamelink_meta_box_callback() {
  1366.         global $post;
  1367.  
  1368.         //получаем текущую категорию
  1369.         $cur_post        = get_the_category( $post->ID );
  1370.         $cur_post        = current( $cur_post );
  1371.         $cur_post_cat_id = $cur_post->cat_ID;
  1372.  
  1373.         //получаем список категорий игр
  1374.         $cats = gamelink_get_cats_list();
  1375.  
  1376.         if ( empty( $cur_post_cat_id ) ) {
  1377.             echo '<span>Current post with no category.</span>';
  1378.             return;
  1379.         }
  1380.         if ( in_array( $cur_post_cat_id, $cats ) ) {
  1381.             echo '<span>Current post is in the category of games.</span>';
  1382.             return;
  1383.         }
  1384.  
  1385.         // Используем nonce для верификации
  1386.         wp_nonce_field( plugin_basename( __FILE__ ), 'gamelink_noncename' );
  1387.  
  1388.         //получаем список игр
  1389.         $games = gamelink_get_games_list();
  1390.  
  1391.         $gamelink_value = get_post_meta( $post->ID, 'gamelink_parent_id', true );
  1392.  
  1393.         if ( empty( $games ) ) {
  1394.             return;
  1395.         }
  1396.  
  1397.         // Поля формы для введения данных
  1398.         echo '<label for="gamelink_parent_id">' . __( "Games", 'gamelink' ) . '</label> ';
  1399.         echo '
  1400.             <select id="gamelink_parent_id" name="gamelink_parent_id">
  1401.                 <option value="0"> ' . __( "No", 'gamelink' ) . ' </option>
  1402.  
  1403.         ';
  1404.  
  1405.         foreach ( (array) $games as $_id => $_name ) {
  1406.             echo '<option value=" ' . $_id . ' " ' . ( $gamelink_value == $_id ? ' selected="selected" ' : ' ' ) . '> ' . $_name . ' </option>';
  1407.         }
  1408.  
  1409.         echo '
  1410.             </select>
  1411.         ';
  1412.     }
  1413. }
  1414.  
  1415. /* получаем список категорий */
  1416. if ( ! function_exists( 'gamelink_get_cats_list' ) ) {
  1417.     function gamelink_get_cats_list() {
  1418.         $cats = array();
  1419.  
  1420.         //получаем список категорий
  1421.         $categories_info = get_categories(
  1422.             array(
  1423.                 'child_of'   => 3,       //игры
  1424.                 'hide_empty' => 0
  1425.             )
  1426.         );
  1427.  
  1428.         if ( empty( $categories_info ) ) {
  1429.             return $cats;
  1430.         }
  1431.  
  1432.         foreach ( (array) $categories_info as $_info ) {
  1433.             $cats[ $_info->term_id ] = $_info->term_id;
  1434.         }
  1435.  
  1436.         return $cats;
  1437.     }
  1438. }
  1439.  
  1440. /* получаем список игр */
  1441. if ( ! function_exists( 'gamelink_get_games_list' ) ) {
  1442.     function gamelink_get_games_list() {
  1443.         $games = array();
  1444.  
  1445.         //получаем список категорий игр
  1446.         $cats = gamelink_get_cats_list();
  1447.  
  1448.         if ( empty( $cats ) ) {
  1449.             return $games;
  1450.         }
  1451.  
  1452.         //получаем список игр
  1453.         foreach ( (array) $cats as $_cat_id ) {
  1454.             $posts = get_posts( array( 'category' => $_cat_id ) );
  1455.             if ( ! empty( $posts ) ) {
  1456.                 foreach ( (array) $posts as $_post_info ) {
  1457.                     $games[ $_post_info->ID ] = $_post_info->post_title;
  1458.                 }
  1459.             }
  1460.         }
  1461.  
  1462.         asort( $games );
  1463.  
  1464.         return $games;
  1465.     }
  1466. }
  1467.  
  1468. /* Сохраняем данные, когда пост сохраняется */
  1469. if ( ! function_exists( 'gamelink_save_postdata' ) ) {
  1470.     function gamelink_save_postdata( $post_id ) {
  1471.         // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  1472.         if ( ! wp_verify_nonce( $_POST['gamelink_noncename'], plugin_basename( __FILE__ ) ) ) {
  1473.             return $post_id;
  1474.         }
  1475.  
  1476.         // проверяем, если это автосохранение ничего не делаем с данными нашей формы.
  1477.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  1478.             return $post_id;
  1479.         }
  1480.  
  1481.         // проверяем разрешено ли пользователю указывать эти данные
  1482.         if ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) {
  1483.             return $post_id;
  1484.         } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
  1485.             return $post_id;
  1486.         }
  1487.  
  1488.         // Убедимся что поле установлено.
  1489.         if ( ! isset( $_POST['gamelink_parent_id'] ) ) {
  1490.             return $post_id;
  1491.         }
  1492.  
  1493.         // Все ОК.
  1494.         // Обновляем данные в базе данных.
  1495.         update_post_meta( $post_id, 'gamelink_parent_id', intval( $_POST['gamelink_parent_id'] ) );
  1496.  
  1497.         return $post_id;
  1498.     }
  1499. }
  1500. add_action( 'save_post', 'gamelink_save_postdata' );
  1501.  
  1502. /* Получаем данные */
  1503. if ( ! function_exists( 'get_gamelink_data' ) ) {
  1504.     function get_gamelink_data() {
  1505.         global $wpdb;
  1506.         global $post;
  1507.  
  1508.         $data = $wpdb->get_results( "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key='gamelink_parent_id' AND meta_value=" . $post->ID, "ARRAY_A" );
  1509.  
  1510.         if ( ! empty( $data ) ) {
  1511.             foreach ( (array) $data as $_info ) {
  1512.                 $result[] = $_info["post_id"];
  1513.             }
  1514.         }
  1515.  
  1516.         return $result;
  1517.     }
  1518. }
  1519.  
  1520.  
  1521.  
  1522. /*
  1523.  *
  1524.  *      Approve by Jurist
  1525.  *
  1526.  * */
  1527.  
  1528. /* Добавляем блоки в основную колонку на страницах постов и пост. страниц */
  1529. if ( ! function_exists( 'jurist_custom_box' ) ) {
  1530.     function jurist_custom_box() {
  1531.         $screens = array( 'post', 'page' );
  1532.         foreach ( $screens as $screen ) {
  1533.             add_meta_box( 'jurist', __( "Jurist Box", 'jurist' ), 'jurist_meta_box_callback', $screen, 'side' );
  1534.         }
  1535.     }
  1536. }
  1537.  
  1538. add_action( 'add_meta_boxes', 'jurist_custom_box' );
  1539.  
  1540.  
  1541. /* HTML код блока */
  1542. if ( ! function_exists( 'jurist_meta_box_callback' ) ) {
  1543.     function jurist_meta_box_callback() {
  1544.         // Используем nonce для верификации
  1545.         wp_nonce_field( plugin_basename( __FILE__ ), 'jurist_noncename' );
  1546.  
  1547.         global $post;
  1548.         $jurist_value = get_post_meta( $post->ID, 'jurist_plugin_value', true );
  1549.  
  1550.         global $current_user;
  1551.         $user_roles = $current_user->roles;
  1552.         $user_role = array_shift($user_roles);
  1553.  
  1554.         if ($user_role !== "jurist") {
  1555.             if ( $jurist_value == "1" ) {
  1556.                 echo '<span style="color: green; font-weight: bold;">Одобрено</span>';
  1557.             } else {
  1558.                 echo '<span style="color: red; font-weight: bold;">Не одобрено</span>';
  1559.             }
  1560.             return;
  1561.         }
  1562.  
  1563.         // Поля формы для введения данных
  1564.         echo '<label for="jurist_plugin_value">' . __( "Ободрено: ", 'jurist' ) . '</label> ';
  1565.         echo '
  1566.         <select id="jurist_plugin_value" name="jurist_plugin_value">
  1567.             <option value="0" ' . ( $jurist_value == '0' ? ' selected="selected" ' : ' ' ) . '> Нет </option>
  1568.             <option value="1" ' . ( $jurist_value == '1' ? ' selected="selected" ' : ' ' ) . '> Да </option>
  1569.         </select>
  1570.     ';
  1571.     }
  1572. }
  1573.  
  1574. /* Сохраняем данные, когда пост сохраняется */
  1575. if ( ! function_exists( 'jurist_save_postdata' ) ) {
  1576.     function jurist_save_postdata( $post_id ) {
  1577.         // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  1578.         if ( ! wp_verify_nonce( $_POST['jurist_noncename'], plugin_basename( __FILE__ ) ) ) {
  1579.             return $post_id;
  1580.         }
  1581.  
  1582.         // проверяем, если это автосохранение ничего не делаем с данными нашей формы.
  1583.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  1584.             return $post_id;
  1585.         }
  1586.  
  1587.         // проверяем разрешено ли пользователю указывать эти данные
  1588.         if ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) {
  1589.             return $post_id;
  1590.         } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
  1591.             return $post_id;
  1592.         }
  1593.  
  1594.         // Убедимся что поле установлено.
  1595.         if ( ! isset( $_POST['jurist_plugin_value'] ) ) {
  1596.             return $post_id;
  1597.         }
  1598.  
  1599.         // Все ОК.
  1600.         // Обновляем данные в базе данных.
  1601.         update_post_meta( $post_id, 'jurist_plugin_value', intval( $_POST['jurist_plugin_value'] ) );
  1602.  
  1603.         return $post_id;
  1604.     }
  1605. }
  1606. add_action( 'save_post', 'jurist_save_postdata' );
  1607.  
  1608. /* Показываем поле в списке постов */
  1609. if ( ! function_exists( 'jurist_column' ) ) {
  1610.     function jurist_column( $cols ) {
  1611.         $cols['jurist_column'] = "Юрист";
  1612.  
  1613.         return $cols;
  1614.     }
  1615. }
  1616. add_filter( 'manage_posts_columns', 'jurist_column', 5 );
  1617.  
  1618. if ( ! function_exists( 'display_jurist_column' ) ) {
  1619.     function display_jurist_column( $col, $id ) {
  1620.         if ( $col !== "jurist_column" ) {
  1621.             return;
  1622.         }
  1623.  
  1624.         $jurist_value = get_post_meta( $id, 'jurist_plugin_value', true );
  1625.         if ( $jurist_value == "1" ) {
  1626.             echo '<span style="color: green; font-weight: bold;">Да</span>';
  1627.         } else {
  1628.             echo '<span style="color: red; font-weight: bold;">Нет</span>';
  1629.         }
  1630.     }
  1631. }
  1632. add_action( 'manage_posts_custom_column', 'display_jurist_column', 5, 2 );
  1633.  
  1634.  
  1635. function publication_confirm_message_function(){
  1636.  
  1637.     global $post;
  1638.     $jurist_value = get_post_meta( $post->ID, 'jurist_plugin_value', true );
  1639.     $corrector_value = get_post_meta( $post->ID, 'corrector_plugin_value', true );
  1640.     if ( $jurist_value == "1" && $corrector_value == "1") {
  1641.         return true;
  1642.     } elseif($jurist_value == "0") {
  1643.         $publication_confirm_message = 'Эта запись не одобрена Юристом. Вы хотите ее опубликовать? Если да, то нажмите OK, если нет, то нажмите ОТМЕНА.';
  1644.         echo '<script type="text/javascript"><!--
  1645.         var publish = document.getElementById("publish");
  1646.         if (publish !== null) publish.onclick = function(){
  1647.             return confirm("' . $publication_confirm_message . '");
  1648.         };
  1649.         // --></script>';
  1650.     } elseif($corrector_value == "0") {
  1651.         $publication_confirm_message = 'Эта запись не одобрена Корректором. Вы хотите ее опубликовать? Если да, то нажмите OK, если нет, то нажмите ОТМЕНА.';
  1652.         echo '<script type="text/javascript"><!--
  1653.         var publish = document.getElementById("publish");
  1654.         if (publish !== null) publish.onclick = function(){
  1655.             return confirm("' . $publication_confirm_message . '");
  1656.         };
  1657.         // --></script>';
  1658.     }
  1659.     return true;
  1660. }
  1661. add_action('admin_footer', 'publication_confirm_message_function');
  1662.  
  1663. //..............................................................................................................................
  1664.  
  1665. /*
  1666.  *
  1667.  *      Approve by Corrector
  1668.  *
  1669.  * */
  1670.  
  1671. /* Добавляем блоки в основную колонку на страницах постов и пост. страниц */
  1672. if ( ! function_exists( 'corrector_custom_box' ) ) {
  1673.     function corrector_custom_box() {
  1674.         $screens = array( 'post', 'page' );
  1675.         foreach ( $screens as $screen ) {
  1676.             add_meta_box( 'corrector', __( "Corrector Box", 'corrector' ), 'corrector_meta_box_callback', $screen, 'side' );
  1677.         }
  1678.     }
  1679. }
  1680.  
  1681. add_action( 'add_meta_boxes', 'corrector_custom_box' );
  1682.  
  1683.  
  1684. /* HTML код блока */
  1685. if ( ! function_exists( 'corrector_meta_box_callback' ) ) {
  1686.     function corrector_meta_box_callback() {
  1687.         // Используем nonce для верификации
  1688.         wp_nonce_field( plugin_basename( __FILE__ ), 'corrector_noncename' );
  1689.  
  1690.         global $post;
  1691.         $corrector_value = get_post_meta( $post->ID, 'corrector_plugin_value', true );
  1692.  
  1693.         global $current_user;
  1694.         $user_roles = $current_user->roles;
  1695.         $user_role = array_shift($user_roles);
  1696.  
  1697.         if ($user_role !== "corrector") {
  1698.             if ( $corrector_value == "1" ) {
  1699.                 echo '<span style="color: green; font-weight: bold;">Одобрено</span>';
  1700.             } else {
  1701.                 echo '<span style="color: red; font-weight: bold;">Не одобрено</span>';
  1702.             }
  1703.             return;
  1704.         }
  1705.  
  1706.         // Поля формы для введения данных
  1707.         echo '<label for="corrector_plugin_value">' . __( "Ободрено: ", 'corrector' ) . '</label> ';
  1708.         echo '
  1709.         <select id="corrector_plugin_value" name="corrector_plugin_value">
  1710.             <option value="0" ' . ( $corrector_value == '0' ? ' selected="selected" ' : ' ' ) . '> Нет </option>
  1711.             <option value="1" ' . ( $corrector_value == '1' ? ' selected="selected" ' : ' ' ) . '> Да </option>
  1712.         </select>
  1713.     ';
  1714.     }
  1715. }
  1716.  
  1717. /* Сохраняем данные, когда пост сохраняется */
  1718. if ( ! function_exists( 'corrector_save_postdata' ) ) {
  1719.     function corrector_save_postdata( $post_id ) {
  1720.         // проверяем nonce нашей страницы, потому что save_post может быть вызван с другого места.
  1721.         if ( ! wp_verify_nonce( $_POST['corrector_noncename'], plugin_basename( __FILE__ ) ) ) {
  1722.             return $post_id;
  1723.         }
  1724.  
  1725.         // проверяем, если это автосохранение ничего не делаем с данными нашей формы.
  1726.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  1727.             return $post_id;
  1728.         }
  1729.  
  1730.         // проверяем разрешено ли пользователю указывать эти данные
  1731.         if ( 'page' == $_POST['post_type'] && ! current_user_can( 'edit_page', $post_id ) ) {
  1732.             return $post_id;
  1733.         } elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
  1734.             return $post_id;
  1735.         }
  1736.  
  1737.         // Убедимся что поле установлено.
  1738.         if ( ! isset( $_POST['corrector_plugin_value'] ) ) {
  1739.             return $post_id;
  1740.         }
  1741.  
  1742.         // Все ОК.
  1743.         // Обновляем данные в базе данных.
  1744.         update_post_meta( $post_id, 'corrector_plugin_value', intval( $_POST['corrector_plugin_value'] ) );
  1745.  
  1746.         return $post_id;
  1747.     }
  1748. }
  1749. add_action( 'save_post', 'corrector_save_postdata' );
  1750.  
  1751. /* Показываем поле в списке постов */
  1752. if ( ! function_exists( 'corrector_column' ) ) {
  1753.     function corrector_column( $cols ) {
  1754.         $cols['corrector_column'] = "Корректор";
  1755.  
  1756.         return $cols;
  1757.     }
  1758. }
  1759. add_filter( 'manage_posts_columns', 'corrector_column', 5 );
  1760.  
  1761. if ( ! function_exists( 'display_corrector_column' ) ) {
  1762.     function display_corrector_column( $col, $id ) {
  1763.         if ( $col !== "corrector_column" ) {
  1764.             return;
  1765.         }
  1766.  
  1767.         $corrector_value = get_post_meta( $id, 'corrector_plugin_value', true );
  1768.         if ( $corrector_value == "1" ) {
  1769.             echo '<span style="color: green; font-weight: bold;">Да</span>';
  1770.         } else {
  1771.             echo '<span style="color: red; font-weight: bold;">Нет</span>';
  1772.         }
  1773.     }
  1774. }
  1775. add_action( 'manage_posts_custom_column', 'display_corrector_column', 5, 2 );
  1776.  
  1777. //..............................................................................................................................
  1778.  
  1779.  
  1780. /**
  1781.  * Popular posts tracking
  1782.  *
  1783.  * Tracks the number of logged out user views for a post in a custom field
  1784.  */
  1785. if ( ! function_exists( 'base_track_popular_posts' ) ) {
  1786.     function base_track_popular_posts() {
  1787.  
  1788.         // Only run the process for single posts, pages and post types when the user is logged out
  1789.         if ( is_singular() ) {
  1790.  
  1791.             global $post;
  1792.             $custom_field = '_base_popular_posts_count';
  1793.  
  1794.             // Set/check session
  1795.             if ( !session_id() )
  1796.                 session_start();
  1797.  
  1798.             // Only track a one view per post for a single visitor session to avoid duplications
  1799.             if ( !isset( $_SESSION["popular-posts-count-{$post->ID}"] ) ) {
  1800.  
  1801.                 // Update view count
  1802.                 $view_count = get_post_meta( $post->ID, $custom_field, true );
  1803.                 $stored_count = ( isset($view_count) && !empty($view_count) ) ? ( intval($view_count) + 1 ) : 1;
  1804.                 $update_meta = update_post_meta( $post->ID, $custom_field, $stored_count );
  1805.  
  1806.                 // Check for errors
  1807.                 if ( is_wp_error($update_meta) )
  1808.                     error_log( $update_meta->get_error_message(), 0 );
  1809.  
  1810.                 // Store session in "viewed" state
  1811.                 $_SESSION["popular-posts-count-{$post->ID}"] = 1;
  1812.             }
  1813.  
  1814.             // Show view the count for testing purposes (add "?show_count=1" onto the URL)
  1815.             if ( isset($_GET['show_count']) && intval($_GET['show_count']) == 1 ) {
  1816.                 echo '<p style="color:red; text-align:center; margin:1em 0;">';
  1817.                 echo get_post_meta( $post->ID, $custom_field, true );
  1818.                 echo ' views of this post</p>';
  1819.             }
  1820.         }
  1821.     }  
  1822. }
  1823. add_action('wp_head', 'base_track_popular_posts');
  1824.  
  1825.  
  1826.  function diw_post_thumbnail_feeds($content) {
  1827.  global $post;
  1828.  if(has_post_thumbnail($post->ID)) {
  1829.   $content = '<div>' . get_the_post_thumbnail($post->ID,$size='thumbnail') . '</div>' . $content;
  1830.  }
  1831.  return $content;
  1832. }
  1833.  
  1834. add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds');
  1835. add_filter('the_content_feed', 'diw_post_thumbnail_feeds');
  1836.  
  1837. require_once 'function_add.php';
  1838.  
  1839. /* Meta-box posts_in_the_top */
  1840. class trueMetaBox {
  1841.     function __construct($options) {
  1842.         $this->options = $options;
  1843.         $this->prefix = $this->options['id'] .'_';
  1844.         add_action( 'add_meta_boxes', array( &$this, 'create' ) );
  1845.         add_action( 'save_post', array( &$this, 'save' ), 1, 2 );
  1846.     }
  1847.     function create() {
  1848.         foreach ($this->options['post'] as $post_type) {
  1849.             if (current_user_can( $this->options['cap'])) {
  1850.                 add_meta_box($this->options['id'], $this->options['name'], array(&$this, 'fill'), $post_type, $this->options['pos'], $this->options['pri']);
  1851.             }
  1852.         }
  1853.     }
  1854.     function fill(){
  1855.         global $post; $p_i_d = $post->ID;
  1856.         wp_nonce_field( $this->options['id'], $this->options['id'].'_wpnonce', false, true );
  1857.         ?>
  1858.         <table class="form-table"><tbody><?php
  1859.         foreach ( $this->options['args'] as $param ) {
  1860.             if (current_user_can( $param['cap'])) {
  1861.             ?><tr><?php
  1862.                 if(!$value = get_post_meta($post->ID, $this->prefix .$param['id'] , true)) $value = $param['std'];
  1863.                 switch ( $param['type'] ) {
  1864.                     case 'text':{ ?>
  1865.                         <th scope="row"><label for="<?php echo $this->prefix .$param['id'] ?>"><?php echo $param['title'] ?></label></th>
  1866.                         <td>
  1867.                             <input name="<?php echo $this->prefix .$param['id'] ?>" type="<?php echo $param['type'] ?>" id="<?php echo $this->prefix .$param['id'] ?>" value="<?php echo $value ?>" placeholder="<?php echo $param['placeholder'] ?>" class="regular-text" /><br />
  1868.                             <span class="description"><?php echo $param['desc'] ?></span>
  1869.                         </td>
  1870.                         <?php
  1871.                         break;                         
  1872.                     }
  1873.                     case 'textarea':{ ?>
  1874.                         <th scope="row"><label for="<?php echo $this->prefix .$param['id'] ?>"><?php echo $param['title'] ?></label></th>
  1875.                         <td>
  1876.                             <textarea name="<?php echo $this->prefix .$param['id'] ?>" type="<?php echo $param['type'] ?>" id="<?php echo $this->prefix .$param['id'] ?>" value="<?php echo $value ?>" placeholder="<?php echo $param['placeholder'] ?>" class="large-text" /><?php echo $value ?></textarea><br />
  1877.                             <span class="description"><?php echo $param['desc'] ?></span>
  1878.                         </td>
  1879.                         <?php
  1880.                         break;                         
  1881.                     }
  1882.                     case 'checkbox':{ ?>
  1883.                         <th scope="row"><label for="<?php echo $this->prefix .$param['id'] ?>"><?php echo $param['title'] ?></label></th>
  1884.                         <td>
  1885.                             <label for="<?php echo $this->prefix .$param['id'] ?>"><input name="<?php echo $this->prefix .$param['id'] ?>" type="<?php echo $param['type'] ?>" id="<?php echo $this->prefix .$param['id'] ?>"<?php echo ($value=='on') ? ' checked="checked"' : '' ?> />
  1886.                             <?php echo $param['desc'] ?></label>
  1887.                         </td>
  1888.                         <?php
  1889.                         break;                         
  1890.                     }
  1891.                     case 'select':{ ?>
  1892.                         <th scope="row"><label for="<?php echo $this->prefix .$param['id'] ?>"><?php echo $param['title'] ?></label></th>
  1893.                         <td>
  1894.                             <label for="<?php echo $this->prefix .$param['id'] ?>">
  1895.                             <select name="<?php echo $this->prefix .$param['id'] ?>" id="<?php echo $this->prefix .$param['id'] ?>"><?php
  1896.                                
  1897.                             $top_posts = get_posts( array(
  1898.                                 'meta_key'        => 'posts_post_top',
  1899.                                 'meta_value' => array('2', '3', '4', '5', '6'),
  1900.                             ) );
  1901.  
  1902.                             foreach ($top_posts as $top_post){
  1903.                                 $top_post_meta = get_post_meta($top_post->ID, 'posts_post_top');
  1904.                                 $top_posts_meta[$top_post_meta[0]] = $top_post_meta[0];
  1905.                             }          
  1906.                                    
  1907.                             foreach($param['args'] as $val=>$name){
  1908.                                 ?><option value="<?php echo $val ?>"<?php echo ( isset ($top_posts_meta[$val]) ) ? ' disabled="disabled"' : '' ?> <?php echo ( $value == $val ) ? ' selected="selected"' : '' ?>><?php echo $name ?></option><?php
  1909.                                                            
  1910.                                 }
  1911.                             ?></select></label><br />
  1912.                             <span class="description"><?php echo $param['desc'] ?></span>
  1913.                         </td>
  1914.                         <?php
  1915.                         break;                         
  1916.                     }
  1917.                 }
  1918.             ?></tr><?php
  1919.             }
  1920.         }
  1921.         ?></tbody></table><?php
  1922.     }
  1923.     function save($post_id, $post){
  1924.         if ( !wp_verify_nonce( $_POST[ $this->options['id'].'_wpnonce' ], $this->options['id'] ) ) return;
  1925.         if ( !current_user_can( 'edit_post', $post_id ) ) return;
  1926.         if ( !in_array($post->post_type, $this->options['post'])) return;
  1927.         foreach ( $this->options['args'] as $param ) {
  1928.             if ( current_user_can( $param['cap'] ) ) {
  1929.                 if ( isset( $_POST[ $this->prefix . $param['id'] ] ) && trim( $_POST[ $this->prefix . $param['id'] ] ) ) {
  1930.                     update_post_meta( $post_id, $this->prefix . $param['id'], trim($_POST[ $this->prefix . $param['id'] ]) );
  1931.                 } else {
  1932.                     delete_post_meta( $post_id, $this->prefix . $param['id'] );
  1933.                 }
  1934.             }
  1935.         }
  1936.     }
  1937. }
  1938.     function save($post_id, $post){
  1939.         if ( !wp_verify_nonce( $_POST[ $this->options['id'].'_wpnonce' ], $this->options['id'] ) ) return;
  1940.         if ( !current_user_can( 'edit_post', $post_id ) ) return;
  1941.         if ( !in_array($post->post_type, $this->options['post'])) return;
  1942.         foreach ( $this->options['args'] as $param ) {
  1943.             if ( current_user_can( $param['cap'] ) ) {
  1944.                 if ( isset( $_POST[ $this->prefix . $param['id'] ] ) && trim( $_POST[ $this->prefix . $param['id'] ] ) ) {
  1945.                     update_post_meta( $post_id, $this->prefix . $param['id'], trim($_POST[ $this->prefix . $param['id'] ]) );
  1946.                 } else {
  1947.                     delete_post_meta( $post_id, $this->prefix . $param['id'] );
  1948.                 }
  1949.             }
  1950.         }
  1951.     }
  1952.  
  1953. $options = array(
  1954.     array(
  1955.         'id'    =>  'posts',
  1956.         'name'  =>  'Вывод постов в ТОП',
  1957.         'post'  =>  array('post', 'page'),
  1958.         'pos'   =>  'normal',
  1959.         'pri'   =>  'high',
  1960.         'cap'   =>  'edit_posts',
  1961.         'args'  =>  array(
  1962.            
  1963.             array(
  1964.                 'id'            =>  'post_top',
  1965.                 'title'         =>  'Вывод постов в топ',
  1966.                 'type'          =>  'select',
  1967.                 //'desc'            =>  'пояснение к полю',
  1968.                 'cap'           =>  'edit_posts',
  1969.                 'args'          =>  array( 
  1970.                     '1' => 'default',
  1971.                     '2' => 'сверху слева',
  1972.                     '3' => 'сверху справа',
  1973.                     '4' => 'снизу слева',
  1974.                     '5' => 'снизу по центру',
  1975.                     '6' => 'снизу справа'
  1976.                 )
  1977.             )
  1978.         )
  1979.     )
  1980.    
  1981. );
  1982.  
  1983. foreach ($options as $option) {
  1984.     $truemetabox = new trueMetaBox($option);
  1985.     }
  1986. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement