Example #1
0
	public static function init() {
    
    global $meow_provider;
    
    load_plugin_textdomain( MASTERPRESS_DOMAIN, false, "masterpress/languages" );
    
    do_action("mp_pre_init");

    self::$cap_mode = get_site_option("mp_cap", "standard");
    
    global $wf;
    
    global $wpdb;
    
    $snp = explode("/", $_SERVER["SCRIPT_NAME"]);
    
    self::$script = basename($snp[count($snp) - 1], ".php");
    
    
    // Register useful libraries separately, so they may be used by themes

    if (WP_DEBUG || MASTERPRESS_DEBUG) {
    
      // legacy picturefill
      wp_register_script('mp-matchmedia',      MASTERPRESS_URL.'js/src/matchmedia.js',                 array(), MASTERPRESS_VERSION);
      wp_register_script('mp-picturefill',     MASTERPRESS_URL.'js/src/picturefill.js',                array('jquery', 'mp-matchmedia'), MASTERPRESS_VERSION);
      

      wp_register_script('jquery-imageload', MASTERPRESS_URL.'js/src/jquery.imageload.js',             array('jquery'), MASTERPRESS_VERSION);
      // new jQuery picturefill
      wp_register_script('jquery-picturefill', MASTERPRESS_URL.'js/src/jquery.picturefill.js',         array('jquery', 'jquery-imageload'), MASTERPRESS_VERSION);

      wp_register_script('jquery-lazyload',    MASTERPRESS_URL.'js/src/jquery.lazyload.js',            array('jquery'), MASTERPRESS_VERSION);
      
      wp_register_script('jquery-fancybox',    MASTERPRESS_URL.'js/src/jquery.fancybox-1.3.4.js',      array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('jquery-scroll-to',   MASTERPRESS_URL.'js/src/jquery.scroll-to.js',           array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('jquery-metadata',    MASTERPRESS_URL.'js/src/jquery.metadata.js',            array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('select2',            MASTERPRESS_URL.'js/src/select2.js',                    array('jquery'), MASTERPRESS_VERSION);
      
      wp_register_script('codemirror',         MASTERPRESS_URL.'js/src/codemirror.js',                 array(), MASTERPRESS_VERSION);
      wp_register_script('codemirror-modes',   MASTERPRESS_URL.'js/codemirror.modes.min.js',           array(), MASTERPRESS_VERSION);
      wp_register_script('mediaelement',      MASTERPRESS_URL.'js/src/mediaelement-and-player.js',     array(), MASTERPRESS_VERSION);
      wp_register_script('handlebars',         MASTERPRESS_URL.'js/src/handlebars-1.0.0.beta.6.js',    array(), MASTERPRESS_VERSION);
      wp_register_script('inflection',         MASTERPRESS_URL.'js/src/inflection.js',                 array(), MASTERPRESS_VERSION);
      wp_register_script('date-js',            MASTERPRESS_URL.'js/src/date.js',                       array(), MASTERPRESS_VERSION);
      
      wp_register_script('mp-iris',     	   MASTERPRESS_URL.'js/src/iris.js',                	   array('jquery', "jquery-ui-core", "jquery-ui-widget"), MASTERPRESS_VERSION);
    
    } else {

      // legacy picturefill (not enqueued)
      wp_register_script('mp-matchmedia',      MASTERPRESS_URL.'js/matchmedia.min.js',                 array(), MASTERPRESS_VERSION);
      wp_register_script('mp-picturefill',     MASTERPRESS_URL.'js/picturefill.min.js',                array('jquery', 'mp-matchmedia'), MASTERPRESS_VERSION);

      wp_register_script('jquery-imageload', MASTERPRESS_URL.'js/jquery.imageload.min.js',             array('jquery'), MASTERPRESS_VERSION);

      // new jQuery picturefill
      wp_register_script('jquery-picturefill', MASTERPRESS_URL.'js/jquery.picturefill.min.js',         array('jquery', 'jquery-imageload'), MASTERPRESS_VERSION);

      wp_register_script('jquery-lazyload',    MASTERPRESS_URL.'js/jquery.lazyload.min.js',            array('jquery'), MASTERPRESS_VERSION);

      wp_register_script('jquery-fancybox',    MASTERPRESS_URL.'js/jquery.fancybox-1.3.4.min.js',      array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('jquery-scroll-to',   MASTERPRESS_URL.'js/jquery.scroll-to.min.js',           array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('jquery-metadata',    MASTERPRESS_URL.'js/jquery.metadata.min.js',            array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('select2',            MASTERPRESS_URL.'js/select2.min.js',                    array('jquery'), MASTERPRESS_VERSION);
      wp_register_script('date-js',            MASTERPRESS_URL.'js/date.min.js',                       array(), MASTERPRESS_VERSION);

      wp_register_script('mediaelement',       MASTERPRESS_URL.'js/mediaelement-and-player.min.js',    array(), MASTERPRESS_VERSION);
      wp_register_script('codemirror',         MASTERPRESS_URL.'js/codemirror.min.js',                 array(), MASTERPRESS_VERSION);
      wp_register_script('codemirror-modes',   MASTERPRESS_URL.'js/codemirror.modes.min.js',           array(), MASTERPRESS_VERSION);
      wp_register_script('handlebars',         MASTERPRESS_URL.'js/handlebars-1.0.0.beta.6.min.js',    array(), MASTERPRESS_VERSION);
      wp_register_script('inflection',         MASTERPRESS_URL.'js/inflection.min.js',                 array(), MASTERPRESS_VERSION);

      if (!self::has_iris()) {
        wp_register_script('mp-iris',     	   MASTERPRESS_URL.'js/iris.min.js',                	   array('jquery', "jquery-ui-core", "jquery-ui-widget"), MASTERPRESS_VERSION);
      }
    }
    
    
    wp_register_style('jquery-fancybox',       MASTERPRESS_URL.'css/fancybox.css',                    array(), MASTERPRESS_VERSION);
    wp_register_style('select2',               MASTERPRESS_URL.'css/select2.css',                     array(), MASTERPRESS_VERSION);
    wp_register_style('codemirror',            MASTERPRESS_URL.'css/codemirror.css',                  array(), MASTERPRESS_VERSION);
    wp_register_style('codemirror-themes',     MASTERPRESS_URL.'css/codemirror.themes.min.css',       array(), MASTERPRESS_VERSION);
    wp_register_style('mediaelement',          MASTERPRESS_URL.'css/mediaelementplayer.min.css',      array(), MASTERPRESS_VERSION);

    
    
    if ( is_admin() ) {

      // run an upgrade (if required)

      MasterPress::upgrade();

      if (defined('MASTERPRESS_NO_UPLOAD_DIR')) {

        MPV::err( 
          sprintf(  
            __( "<b>Important: </b>When trying to determine the location of your uploads directory, WordPress reported the following error: <code>%s</code>That is, your WordPress installation <b>does not currently contain a writable uploads directory</b>, so MasterPress will not function correctly as it cannot create its own required sub-folders.<br>To avoid further problems, you must correct this before creating or editing anything with MasterPress.<br>", MASTERPRESS_DOMAIN ),
            MASTERPRESS_NO_UPLOAD_DIR
          )
        );
        
      } else {
        self::create_directories();
      }
      
      add_action('admin_menu', array('MasterPress', 'admin_menu'));

      wp_enqueue_script("jquery-lazyload");
      wp_enqueue_script("jquery-imageload");
      wp_enqueue_script("jquery-picturefill");
      

      // Add filters for specific screens
      
      if (self::$script == "options-reading") {
        add_filter( 'get_pages',  array("MasterPress", 'add_front_page_supports' ) );
      }
      
      if (self::is_user_editor()) {
        add_filter( 'mce_external_plugins', array( "MasterPress", 'mce_plugins' ) );
        MPC::incl("user");
        add_action( 'show_user_profile', array("MPC_User", "field_sets" ));
        add_action( 'edit_user_profile', array("MPC_User", "field_sets" ));
      }
      
      if (self::is_term_editor() || self::is_term_manage()) {
        MPC::incl("term");
        if (isset($_GET["taxonomy"])) {
          $taxonomy = $_GET["taxonomy"];
        }
      }
      
      if (self::is_term_editor()) {
        add_filter( 'mce_external_plugins', array( "MasterPress", 'mce_plugins' ) );
        // add field UI hook
        add_action( $taxonomy."_edit_form_fields", array("MPC_Term", "field_sets"), 10, 2 );
      }

      if (self::is_term_manage()) {
        add_filter( 'mce_external_plugins', array( "MasterPress", 'mce_plugins' ) );
        // add field UI hook
        add_action( $taxonomy."_add_form_fields", array("MPC_Term", "field_sets"), 10, 2 );
      }
      
      if (self::is_site_content_editor()) {
        MPV::incl("meta");
        MPC::incl("meta");
        add_filter( 'mce_external_plugins', array( "MasterPress", 'mce_plugins' ) );
      }
      
      if (self::is_post_editor()) {
        add_filter( 'mce_external_plugins', array( "MasterPress", 'mce_plugins' ) );
			  add_action('add_meta_boxes', array('MPC_Post', 'post_editor_meta_boxes'));
        add_action('admin_init', array('MasterPress', 'admin_init'));
		  }

      if (self::is_post_manage()) {
        add_filter("manage_posts_columns", array("MPC_Post", "define_post_columns"));
        add_filter("manage_pages_columns", array("MPC_Post", "define_post_columns"));
        add_action("manage_posts_custom_column", array("MPC_Post", "post_column_content"));
        add_action("manage_pages_custom_column", array("MPC_Post", "post_column_content"));
        add_filter('parse_query', array("MPC_Post", "filter_manage_posts"));
        add_filter('request', array("MPC_Post", "request"));
        
        $meow_provider->set_optimize_mode( "post_list" );
        
      }
      
      
      // add save hooks
      
      add_action('edited_term', array("MPC_Meta", 'save_term_meta') );
      add_action('created_term', array("MPC_Meta", 'save_term_meta') );
      add_action('save_post', array("MPC_Meta", 'save_post_meta'));
      add_action('personal_options_update', array("MPC_Meta", 'save_user_meta'));
      add_action('edit_user_profile_update', array("MPC_Meta", 'save_user_meta'));
      

      add_action('save_post', array("MasterPress", 'save_post'));

      // add inline scripts
      
      add_action("admin_head", array("MasterPress", "inline_scripts"));
      add_action("admin_head", array("MasterPress", "inline_styles"));
      add_action("admin_head", array("MasterPress", "inline_meta"));

    }

    

    // cache the list of all post types
    
    
    $args = array();
    
    if (is_admin()) {
      // only order by menu positions in the admin, as the order doesn't matter on the front-end (slightly faster)
      $args = array("orderby" => "menu_position,menu_sub_position");
    }

    $post_types = MPM_PostType::find($args);
    
    MasterPress::$post_types = array();

    foreach ($post_types as $post_type) {
      MasterPress::$post_types[$post_type->name] = $post_type;
    }

    $taxonomies = MPM_Taxonomy::find( );

    MasterPress::$all_taxonomies = array();
    
    MasterPress::$taxonomies = array();
    
    foreach ($taxonomies as $tax) {
      if (!$tax->disabled) {
        MasterPress::$taxonomies[$tax->name] = $tax;
      }

      MasterPress::$all_taxonomies[$tax->name] = $tax;
    
    }
    

    // create necessary file directories
    
    if (is_admin()) {
    
      $combine = false;
      
      // check for a querystring force
      if (isset($_GET["mpft"])) {
        $combine = true;
      }
      
      // check if debug mode had been switched on, then off (javascript "make" still needs to be run manually)

      $opt_debug = get_option("mp_debug");
      
      if (!MASTERPRESS_DEBUG && $opt_debug != MASTERPRESS_DEBUG) {
        $combine = true;
      }

      if ($opt_debug != MASTERPRESS_DEBUG) {
        update_option("mp_debug", MASTERPRESS_DEBUG);
      }

      if (!file_exists(MASTERPRESS_CONTENT_MPFT_CACHE_DIR."mpft-all.css")) {
        $combine = true;
      }

      if (!file_exists(MASTERPRESS_CONTENT_MPFT_CACHE_DIR."mpft-all.js")) {
        $combine = true;
      }
      

      if ($combine) {
        MPU::combine_type_styles();
        MPU::combine_type_scripts();
      }
      
    }
  

    if (self::is_term_manage()) {
      
      MPC::incl("term");

      add_filter('get_terms_args', array("MPC_Term", "filter_manage_terms"));

      foreach (MasterPress::$taxonomies as $tax) {
        add_filter('manage_edit-'.$tax->name.'_columns', array("MPC_Term", "define_term_columns"));
        add_action('manage_'.$tax->name.'_custom_column', array("MPC_Term", "term_column_content"), 10, 3);
      }
    
    }

    // register post types based on database entries

    do_action('mp_pre_register_post_types');

    self::register_post_types();

    do_action('mp_register_post_types');

    // register taxonomies based on database entries

    do_action('mp_pre_register_taxonomies');

    self::register_taxonomies();

    do_action('mp_pre_register_taxonomies');

    // patch template support
    
    self::register_template_support();
    
    
    do_action('mp_register');

    // look for externally defined post types, and insert models for them in MasterPress.
    
    $reg_post_types = $wf->post_types();

    $rpt_keys = array();
    $pt_keys = array();
	
    foreach ($reg_post_types as $rpt) {
      if (!is_woof_silent($rpt)) {
        $rpt_keys[$rpt->name] = $rpt;
      }
    }
    
    foreach ($post_types as $pt) {
      $pt_keys[$pt->name] = $pt;
    }
    
    $ignore_keys = array("revision", "nav_menu_item");
    
    foreach ($rpt_keys as $key => $pt) {
      
      if (!isset($pt_keys[$key]) && !in_array($key, $ignore_keys)) {

        // create an external post type database entry
        
        $ept = new MPM_PostType();
        
        // ensure that unknown properties return "null", not "WOOF_Silent"
        $pt->set_property_mode("standard");
        
        $ept->set(      
          array( 
            "name" => $pt->name,
            "plural_name" => WOOF_Inflector::pluralize($pt->name),
            "disabled" => false,
            "labels" => $pt->labels,
            "description" => "",
            "publicly_queryable" => (bool) $pt->publicly_queryable,
            "exclude_from_search" => (bool) $pt->exclude_from_search,
            "show_ui" => (bool) $pt->show_ui,
            "show_in_menu" => (bool) $pt->show_in_menu,
            "menu_position" => (int) $pt->menu_position,
            "menu_sub_position" => 0,
            "menu_icon" => isset($pt->menu_icon) ? $pt->menu_icon : "",
            "manage_sort_order" => "post_date|desc",
            "capability_type" => $pt->capability_type,
            "capabilities" => $pt->cap_array(),
            "map_meta_cap" => (bool) $pt->map_meta_cap,
            "hierarchical" => (bool) $pt->hierarchical,
            "supports" => implode(",", $pt->supports_keys()),
            "permalink_epmask" => $pt->permalink_epmask,
            "has_archive" => (bool) $pt->has_archive,
            "rewrite" => $pt->rewrite,
            "query_var" => $pt->query_var,
            "can_export" => $pt->can_export,
            "show_in_nav_menus" => $pt->show_in_nav_menus,
            "_builtin" => false,
            "_external" => true,
            "visibility" => array("sites" => "*") 
          )
        );
        
        $ept->insert();
        
        
        
      }
    }
    
    // look for externally defined taxonomies, and insert models for them in MasterPress.
    
    $reg_tax = $wf->taxonomies();

    $rt_keys = array();
    $t_keys = array();

    foreach ($reg_tax as $tax) {
      $rt_keys[$tax->name] = $tax;
    }
    
    foreach ($taxonomies as $tax) {
      $t_keys[$tax->name] = $tax;
    }
    
    $ignore_keys = array("nav_menu", "link_category", "post_format");
    
    foreach ($rt_keys as $key => $tax) {
      if (!isset($t_keys[$key]) && !in_array($key, $ignore_keys)) {

        // create an external post type database entry
        
        $et = new MPM_Taxonomy();
        
        // ensure that unknown properties return "null", not "WOOF_Silent"
        $tax->set_property_mode("standard");
        
        
        $et->set(      
          array( 
            "name" => $tax->name,
            "plural_name" => WOOF_Inflector::pluralize($tax->name),
            "labels" => $tax->labels,
            "disabled" => false,
            "show_in_nav_menus" => (bool) $tax->show_in_nav_menus,
            "show_ui" => (bool) $tax->show_ui,
            "show_tagcloud" => (bool) $tax->show_tagcloud,
            "hierarchical" => (bool) $tax->hierarchical,
            "rewrite" => $tax->rewrite,
            "update_count_callback" => $tax->update_count_callback,
            "query_var" => $tax->query_var,
            "capabilities" => $tax->cap_array(),
            "object_type" => $tax->object_type,
            "_builtin" => false,
            "_external" => true
          )
        );

        $et->insert();
        
      }
    }
    

    
    if (is_admin()) {
      self::dispatch();
    }
    
    foreach ($wf->types() as $type) {
      if ($type->supports("top-level-slugs")) {
        add_filter("woof_".$type->name."_permalink", array("MasterPress", "woof_permalink"), 100, 2);
      }
    }
    
    // if anyone rewrites URLs, add ours to the queue
    add_filter( 'rewrite_rules_array', array("MasterPress", 'add_rewrite_rules' ));

    do_action('mp_rewrite_rules');
    do_action('mp_pre_flush_rewrite_rules');

    add_rewrite_tag("%mp_rest_endpoint%", '([^&]+)');

    if (is_admin()) {
      // flush rewrite rules (only if marked as needed)
      MasterPress::flush_rewrite_rules();
    }
  
    if (is_admin() && MasterPress::current_user_can("manage_mp_tools")) {
      self::tools_actions();
    }
    
    do_action('mp_init');

  }