Advertisement
Guest User

Untitled

a guest
Aug 6th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.19 KB | None | 0 0
  1. global $global_msp_settings;
  2. $msp_duration           = get_option( "msp_duration" );
  3. $global_msp_settings    = array(
  4.     "domain"    => "MSP",
  5.     "blog_id"   => get_current_blog_id(),
  6.     "duration"  => !empty( $msp_duration ) ? $msp_duration : HOUR_IN_SECONDS * 6,
  7.     "transient" => "msp_posts",
  8.     "default"   => array(
  9.         "post_no"       => 10,
  10.         "excerpt"       => false,
  11.         "category"      => "",
  12.         "thumbnail"     => false,
  13.         "custom_query"  => false
  14.     )
  15. );
  16.  
  17. class Multisite_Posts_Core {
  18.  
  19.     function __construct( $options = array(), $blog_id = false ) {
  20.  
  21.         global $global_msp_settings;   
  22.  
  23.         $this->blog_id      = !empty( $blog_id ) ? $blog_id : $global_msp_settings["blog_id"];
  24.         $this->domain       = $global_msp_settings["domain"];
  25.         $this->default      = $global_msp_settings["default"];
  26.         $this->duration     = $global_msp_settings["duration"];
  27.         $this->transient    = $global_msp_settings["transient"];
  28.         $this->options      = $options ? shortcode_atts( $this->default, $options ) : $this->default;
  29.  
  30.     }
  31.  
  32.     //Get a List Of Blogs
  33.     function get_blog_list( $type = "plain" ) {
  34.  
  35.         global $wpdb;
  36.  
  37.         $blogs_table    = $wpdb->base_prefix . "blogs";
  38.         $blogs_list     = $wpdb->get_results( "
  39.             SELECT blog_id, domain FROM {$blogs_table} ORDER BY blog_id
  40.         " );
  41.         $output         = "";
  42.  
  43.         foreach( $blogs_list as $blog ) {
  44.  
  45.             switch($type) {
  46.                 case "plain":
  47.                     $output[$blog->blog_id] = $blog->domain;
  48.                     break;
  49.                 case "dropdown":
  50.                     $output .= '<option name="' . $blog->blog_id . '">' . $blog->domain . '</option>';
  51.                     break;
  52.             }
  53.  
  54.         }
  55.  
  56.         return $output;
  57.  
  58.     }
  59.  
  60.     //Find match based on options and blog_id
  61.     function options_deep_search( $msp_posts, $options, $blog_id ) {
  62.  
  63.         if( !empty( $msp_posts ) ) {
  64.  
  65.             foreach( $msp_posts as $index => $one_msp_posts ) {
  66.  
  67.                 if( $one_msp_posts["criteria"] == $options &&
  68.                     $one_msp_posts["blog_id"] == $blog_id ) {
  69.  
  70.                     return $index;
  71.  
  72.                 }
  73.  
  74.                 continue;
  75.  
  76.             }
  77.  
  78.         }
  79.  
  80.         return false;
  81.  
  82.     }
  83.  
  84.     //Append to List of Options
  85.     function populate_msp_posts( $msp_posts, $blog_id, $options ) {
  86.  
  87.         switch_to_blog( $blog_id );
  88.  
  89.         $this->query        = !empty( $options["custom_query"] ) ? $options["custom_query"] : array(
  90.             "cat"               => $options["category"],
  91.             "paged"             => 1,
  92.             "post_status"       => "publish",
  93.             "posts_per_page"    => $options["post_no"],
  94.         );
  95.  
  96.         $new_posts = new WP_Query( $this->query );
  97.  
  98.         array_push( $msp_posts, array(
  99.             "criteria"  => $options,
  100.             "all_post"  => $new_posts,
  101.             "blog_id"   => $blog_id,
  102.         ) );
  103.  
  104.         restore_current_blog();
  105.  
  106.         return $msp_posts;
  107.  
  108.     }
  109.  
  110.     //Display the posts
  111.     function display_msp_posts( $one_msp_posts, $echo = false, $options = false ) {
  112.  
  113.         $blog_id    = $one_msp_posts["blog_id"];
  114.         $all_post   = $one_msp_posts["all_post"];
  115.         $criteria   = $one_msp_posts["criteria"];
  116.         $output     = '<ul class="blog-posts';
  117.         $output     .= !empty( $criteria["thumbnail"] ) ? "" : " thumbnail ";
  118.         $output     .= '" data-blogid="' . $blog_id . '">';
  119.  
  120.         if( $all_post->post_count > 0 ) {
  121.  
  122.             while( $all_post->have_posts() ) {
  123.  
  124.                 $all_post->the_post();
  125.  
  126.                 $post_id    = get_the_ID();
  127.                 $post_title = get_the_title();
  128.  
  129.                 $output    .= '<li>';
  130.  
  131.                 if( !empty( $criteria["thumbnail"] ) ) {
  132.                     if( has_post_thumbnail() ) {
  133.                         $output .= get_the_post_thumbnail( $post_id, array(100, 100) );
  134.                     } else {
  135.                         $output .= '<img class="msp-thumbnail" src="' . plugins_url( "assets/msp_noimg.jpg", __FILE__ ) . '" />';
  136.                     }
  137.                 }
  138.  
  139.  
  140.                 $output      .= '<div class="msp-content"><a href="' . get_post_permalink() . '" title="' . $post_title . '">' . $post_title . '</a>';
  141.  
  142.                 if( !empty( $criteria["excerpt"] ) ) {
  143.                     $output  .= '<p>' . get_the_excerpt() . '</p>';
  144.                 }
  145.  
  146.                 $output      .= '</div></li>';
  147.  
  148.             }
  149.  
  150.             wp_reset_postdata();
  151.  
  152.         }
  153.  
  154.         $output  .= '</ul>';
  155.  
  156.         if($echo) {
  157.             echo $output;
  158.             return;
  159.         } else {
  160.             return $output;
  161.         }
  162.  
  163.     }
  164.  
  165.     //Obtain msp posts for one Blog
  166.     function fetch_msp_posts( $options = false, $blog_id = false, $echo = false ) {
  167.  
  168.         $msp_posts  = get_transient( $this->transient );
  169.         $one_msp_posts;
  170.  
  171.         $options    = !empty( $options ) ? $options : $this->default;
  172.         $blog_id    = !empty( $blog_id ) ? $blog_id : $this->blog_id;
  173.         $msp_posts  = !empty( $msp_posts ) ? $msp_posts : array();
  174.         $msp_index  = $this->options_deep_search( $msp_posts, $options, $blog_id );
  175.  
  176.  
  177.         if( $msp_index !== false ) { //Get existing set
  178.  
  179.             $one_msp_posts  = $msp_posts[$msp_index];
  180.  
  181.         } else { //Do independent query
  182.  
  183.             $msp_posts      = $this->populate_msp_posts( $msp_posts, $blog_id, $options );
  184.             $one_msp_posts  = $msp_posts[count($msp_posts) - 1];
  185.             set_transient( $this->transient, $msp_posts, $this->duration );
  186.  
  187.         }
  188.  
  189.         $result = $this->display_msp_posts( $one_msp_posts, $echo, false );
  190.  
  191.         if( !$echo ) return $result;
  192.         return;
  193.  
  194.     }
  195.  
  196. }
  197.  
  198. class Multisite_Posts {
  199.  
  200.     function __construct( $options = array(), $blog_id = false ) {
  201.  
  202.         global $global_msp_settings;
  203.  
  204.         $this->core         = new Multisite_Posts_Core();
  205.         $this->blog_id      = !empty( $blog_id ) ? $blog_id : $global_msp_settings["blog_id"];
  206.         $this->domain       = $global_msp_settings["domain"];
  207.         $this->default      = $global_msp_settings["default"];
  208.         $this->duration     = $global_msp_settings["duration"];
  209.         $this->transient    = $global_msp_settings["transient"];
  210.  
  211.  
  212.         add_action( "admin_init", array($this, "admin_init_callback") );
  213.         add_action( "admin_footer", array($this, "admin_footer_callback") );
  214.         add_action( "wp_enqueue_scripts", array($this, "wp_enqueue_scripts_callback") );
  215.         add_action( "admin_enqueue_scripts", array($this, "admin_enqueue_scripts_callback") );
  216.         add_action( "media_buttons_context", array($this, "media_buttons_context_callback") );
  217.         add_action( "wp_schedule_event_callback", array($this, "wp_schedule_event_callback") );
  218.  
  219.         add_filter( 'the_excerpt', 'do_shortcode' );
  220.         add_filter( 'widget_text', 'do_shortcode' );
  221.  
  222.         add_shortcode( "multisite_posts", array($this, "multisite_posts_shortcode_callback") );
  223.  
  224.         load_plugin_textdomain( "MSP", false, plugin_dir_path( __FILE__ ) . 'languages/' );
  225.  
  226.         register_activation_hook( __FILE__, array($this, "install") );
  227.         register_deactivation_hook( __FILE__, array($this, "uninstall") );
  228.  
  229.     }
  230.  
  231.     function install() {
  232.  
  233.         wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', "wp_schedule_event_callback" );
  234.  
  235.     }
  236.  
  237.     function uninstall() {
  238.  
  239.         wp_clear_scheduled_hook( 'wp_schedule_event_callback' );
  240.         delete_transient( $this->transient );
  241.  
  242.     }
  243.  
  244.     //Options Page Configuration
  245.     function admin_init_callback() {
  246.  
  247.         register_setting( 'general', 'msp_duration', 'esc_attr' );
  248.  
  249.         add_settings_section( 'msp_settings', __('Multisite Posts Configuration', $this->domain), false, 'general' );
  250.  
  251.         add_settings_field( 'msp_duration', __('Cache Duration', $this->domain), array($this, 'msp_settings_callback'), 'general', 'msp_settings', array(
  252.             "id"    => "msp_duration",
  253.             "type"  => "dropdown",
  254.             "values"=> array(
  255.                 array(
  256.                     "name"  => __('Every 6 hours', $this->domain),
  257.                     "value" => HOUR_IN_SECONDS * 6,
  258.                 ),
  259.                 array(
  260.                     "name"  => __('Every 12 hours', $this->domain),
  261.                     "value" => HOUR_IN_SECONDS * 12,
  262.                 ),
  263.                 array(
  264.                     "name"  => __('Every 24 hours', $this->domain),
  265.                     "value" => DAY_IN_SECONDS,
  266.                 ),
  267.                 array(
  268.                     "name"  => __('Every 48 hours', $this->domain),
  269.                     "value" => DAY_IN_SECONDS * 2,
  270.                 ),
  271.                 array(
  272.                     "name"  => __('Every Week', $this->domain),
  273.                     "value" => DAY_IN_SECONDS * 7,
  274.                 ),
  275.             )
  276.         ) );
  277.  
  278.         return;
  279.  
  280.     }
  281.  
  282.     function msp_settings_callback( $args ) {
  283.  
  284.         $value = get_option( $args["id"] );
  285.  
  286.         if( "dropdown" == $args["type"] ) {
  287.  
  288.             ?>
  289.             <select name="<?php echo $args["id"]; ?>">
  290.                 <?php
  291.                     foreach ( $args["values"] as $option ) {
  292.                         echo '<option value="' . $option["value"] . '" ';
  293.                         selected( $value, $option["value"] );
  294.                         echo '>' . $option["name"] . '</option>';
  295.                     }
  296.                 ?>
  297.             </select>
  298.             <?php
  299.  
  300.         }
  301.  
  302.     }
  303.  
  304.     //Button In Editor
  305.     function admin_footer_callback() {
  306.  
  307.         $args       = array("post_no", "category", "blog_id", "custom_query", "excerpt", "thumbnail");
  308.         ?>
  309.         <div id="msp_container" style="display:none;">
  310.             <h2><?php _e("Generate Shortcode", $this->domain); ?></h2>
  311.             <table class="form-table">
  312.                 <?php
  313.                     foreach( $args as $arg ) {
  314.  
  315.                         $label = __( ucwords( str_replace( "_", " ", trim($arg) ) ), $this->domain );
  316.  
  317.                         ?>
  318.                         <tr class="form-field">
  319.                             <th scope="row"><?php echo $label; ?></th>
  320.                             <td>
  321.                             <?php
  322.  
  323.                                 if( in_array( $arg, array("post_no", "category", "custom_query") ) ) {
  324.  
  325.                                     ?>
  326.                                     <input id="<?php echo $arg; ?>" name="<?php echo $arg; ?>" type="text" value="<?php echo esc_attr( $this->default[$arg] ); ?>" />
  327.                                     <?php
  328.  
  329.                                 } else if( $arg == "blog_id" ) {
  330.  
  331.                                     ?>
  332.                                     <select id="<?php echo $arg; ?>" name="<?php echo $arg; ?>">
  333.                                         <?php
  334.                                             $dropdown = $this->core->get_blog_list();
  335.                                             foreach( $dropdown as $key => $value ) {
  336.                                                 ?><option value="<?php echo $key; ?>"><?php echo $value; ?></option><?php
  337.                                             }
  338.                                             unset($dropdown);
  339.                                         ?>
  340.                                     </select>
  341.                                     <?php
  342.  
  343.                                 } else if( in_array( $arg, array("excerpt", "thumbnail") ) ) {
  344.  
  345.                                     ?>
  346.                                     <input id="<?php echo $arg; ?>" name="<?php echo $arg; ?>" type="checkbox" value="on" />
  347.                                     <?php
  348.  
  349.                                 }
  350.  
  351.                             ?>
  352.                             </td>
  353.                         </tr>
  354.                         <?php
  355.                     }
  356.                 ?>
  357.             <tr><td colspan="2"><button class="button button-primary"><?php _e("Insert Shortcode", $this->domain); ?></button></td></tr>
  358.             </table>
  359.         </div>
  360.         <?php
  361.  
  362.     }
  363.  
  364.     function media_buttons_context_callback($context) {
  365.  
  366.         if ( !current_user_can( 'edit_posts' ) &&
  367.             !current_user_can( 'edit_pages' ) &&
  368.             get_user_option( 'rich_editing' ) == 'true' ) {
  369.             return;
  370.         }
  371.  
  372.         $image      = plugins_url("assets/msp_icon.png", __FILE__);
  373.         $context   .= "<a title='Multisite Posts Shortcode' class='thickbox msp_img' href='#TB_inline?width=400&height=600&inlineId=msp_container'><img src='{$image}' style='width: 24px;' /></a>";
  374.  
  375.         return $context;
  376.  
  377.     }
  378.  
  379.     //Update msp posts
  380.     function wp_schedule_event_callback() {
  381.  
  382.         $msp_posts  = get_transient( $this->transient );
  383.         $msp_posts  = !empty( $msp_posts ) ? $msp_posts : array();
  384.         $blogs_list = $this->core->get_blog_list();
  385.  
  386.         foreach( $blogs_list as $blog_id => $domain ) {
  387.  
  388.             $msp_posts = $this->core->populate_msp_posts( $msp_posts, $blog_id, $this->default );
  389.  
  390.         }
  391.  
  392.         set_transient( $this->transient, $msp_posts, $this->duration );
  393.  
  394.         return;
  395.  
  396.     }
  397.  
  398.     //Enqueue scripts
  399.     function wp_enqueue_scripts_callback() {
  400.  
  401.         wp_enqueue_style( "msp", plugins_url( "assets/msp.min.css", __FILE__ ), false, false, 'all' );
  402.  
  403.         return;
  404.  
  405.     }
  406.  
  407.     function admin_enqueue_scripts_callback() {
  408.  
  409.         wp_enqueue_style( "thickbox" );
  410.         wp_enqueue_style( "wp-jquery-ui-dialog" );
  411.         wp_enqueue_script( "thickbox" );
  412.         wp_enqueue_script( "msp", plugins_url( "assets/msp_icon.min.js", __FILE__ ), array("jquery"), false, true );
  413.  
  414.         return;
  415.  
  416.     }
  417.  
  418.     //Shortcode functionality
  419.     function multisite_posts_shortcode_callback($atts) {
  420.  
  421.         $options    = shortcode_atts( $this->default, $atts );
  422.         $blog_id    = !empty( $atts["blog_id"] ) ? $atts["blog_id"] : $this->blog_id;
  423.         $output     = $this->core->fetch_msp_posts( $options, $blog_id, false );
  424.  
  425.         return $output;
  426.  
  427.     }
  428.  
  429. }
  430.  
  431. class Multisite_Posts_Widget extends WP_Widget {
  432.    
  433.     function __construct() {
  434.  
  435.         $this->msp_core             = new Multisite_Posts_Core();
  436.         $this->default              = $this->msp_core->default;
  437.         $this->domain               = $this->msp_core->domain;
  438.         $this->default["blog_id"]   = $this->msp_core->blog_id;
  439.         $this->default["title"]     = "";
  440.  
  441.         parent::__construct(
  442.             'multisite_posts_widget',
  443.             __('Multisite Posts Widget', $this->domain),
  444.             array(
  445.                 'description' => __( 'Get posts from different subsites', $this->domain ),
  446.             )
  447.         );
  448.  
  449.     }
  450.  
  451.     function widget( $args, $instance ) {
  452.  
  453.         $title      = apply_filters( 'widget_title', $instance['title'] );
  454.         $instance   = shortcode_atts( $this->default, $instance );
  455.  
  456.         echo $args["before_widget"];
  457.         if ( !empty( $title ) ) echo $args["before_title"] . $title . $args["after_title"];
  458.         $this->msp_core->fetch_msp_posts($instance, $instance["blog_id"], true);
  459.         echo $args["after_widget"];
  460.  
  461.     }
  462.  
  463.     function update( $new_instance, $old_instance ) {
  464.  
  465.         $instance = $old_instance;
  466.         foreach( $new_instance as $key => $value ) {
  467.  
  468.             $instance[$key] = strip_tags( $new_instance[$key] );
  469.  
  470.             continue;
  471.         }
  472.  
  473.         return $new_instance;
  474.  
  475.     }
  476.  
  477.     function form( $instance ) {
  478.  
  479.         $instance   = shortcode_atts( $this->default, $instance );
  480.         $args       = array("title", "post_no", "category", "blog_id", "custom_query", "excerpt", "thumbnail");
  481.  
  482.         foreach( $args as $arg ) {
  483.  
  484.             $item_id    = $this->get_field_id($arg);
  485.             $item_name  = $this->get_field_name($arg);
  486.             $item_label = __( ucwords( str_replace( "_", " ", trim($arg) ), "MSP" ), $this->domain );
  487.  
  488.             if( in_array( $arg, array("title", "post_no", "custom_query", "category") ) ) {
  489.  
  490.                 ?>
  491.                 <p>
  492.                     <label for="<?php echo $item_id; ?>"><?php echo $item_label; ?></label>
  493.                     <input class="widefat" id="<?php echo $item_id; ?>" name="<?php echo $item_name; ?>" type="text" value="<?php echo esc_attr( $instance[$arg] ); ?>" />
  494.                 </p>
  495.                 <?php
  496.  
  497.             } else if( "blog_id" == $arg ) {
  498.  
  499.                 ?>
  500.                 <p>
  501.                     <label for="<?php echo $item_id; ?>"><?php _e( "Blog ID", $this->domain ); ?></label>
  502.                     <select class="widefat" id="<?php echo $item_id; ?>" name="<?php echo $item_name; ?>">
  503.                         <?php
  504.                             $dropdown = $this->msp_core->get_blog_list();
  505.                             foreach( $dropdown as $key => $value ) {
  506.                                 ?><option value="<?php echo $key; ?>" <?php selected($instance["blog_id"], $key); ?>><?php echo $value; ?></option><?php
  507.                             }
  508.                             unset( $dropdown );
  509.                         ?>
  510.                     </select>
  511.                 </p>
  512.                 <?php
  513.  
  514.             } else if( in_array( $arg, array("excerpt", "thumbnail") ) ) {
  515.  
  516.                 ?>
  517.                 <p>
  518.                     <label for="<?php echo $item_id; ?>"><?php echo $item_label; ?></label>
  519.                     <input class="widefat" id="<?php echo $item_id; ?>" name="<?php echo $item_name; ?>" type="checkbox" <?php checked( $instance[$arg], "on" ); ?> />
  520.                 </p>
  521.                 <?php
  522.  
  523.             }
  524.  
  525.         }
  526.  
  527.     }
  528. }
  529.  
  530. $msp = new Multisite_Posts();
  531. add_action( 'widgets_init', create_function( '', 'register_widget( "Multisite_Posts_Widget" );') );
  532. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement