[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Addons.class.php (source)

   1  <?php
   2  class MP_Addons
   3  {
   4      const option_name = 'MailPress_add-ons';
   5      const folder      = 'add-ons';
   6  
   7  	function __construct()
   8      {
   9          $new = array();
  10          $old = get_option( self::option_name );
  11  
  12          foreach ( self::get_all() as $mp_addon )
  13          {
  14              if ( !$mp_addon['active'] )            continue;
  15              if ( !self::load( $mp_addon['file'] ) )    continue;
  16  
  17              $new[$mp_addon['file']] = $mp_addon['file'];
  18          }
  19  
  20          if ( $new != $old ) update_option( self::option_name, $new );
  21  
  22          do_action( 'MailPress_addons_loaded' );
  23      }
  24  
  25  	public static function load( $file )
  26      {
  27          $file = ABSPATH . PLUGINDIR . "/$file";
  28  
  29          if ( !is_file( $file ) ) return false;
  30          require_once( $file );
  31          return true;
  32      }
  33  
  34  	public static function get_all( $addon_folder = '' )
  35      {
  36          if ( ! $cache_addons = wp_cache_get( 'mp_addons', 'mp_addons' ) ) $cache_addons = array();
  37          if ( isset( $cache_addons[ $addon_folder ] ) ) return $cache_addons[ $addon_folder ];
  38  
  39          $mp_addons = array ();
  40          $addon_root = MP_CONTENT_DIR . self::folder;
  41          if( !empty( $addon_folder ) ) $addon_root .= $addon_folder;
  42  
  43          // Files in addons directory
  44          $addon_files = array();
  45  
  46          $addons_dir = @opendir( $addon_root );
  47          if ( $addons_dir ) 
  48          {
  49              while ( ( $file = readdir( $addons_dir ) ) !== false ) 
  50              {
  51                  if ( substr( $file, 0, 1 ) == '.' ) continue;
  52                  if ( is_dir( "$addon_root/$file" ) ) 
  53                  {
  54                      $addons_subdir = @opendir( "$addon_root/$file" );
  55                      if ( $addons_subdir ) 
  56                      {
  57                          while ( ( $subfile = readdir( $addons_subdir ) ) !== false ) 
  58                          {
  59                              if ( substr( $subfile, 0, 1 ) == '.' ) continue;
  60                              if ( substr( $subfile, -4 ) == '.php' ) $addon_files[] = plugin_basename( "$addon_root/$file/$subfile" );
  61                          }
  62                          @closedir( $addons_subdir );
  63                      }
  64                  } 
  65                  else 
  66                  {
  67                      if ( substr( $file, -4 ) == '.php' ) $addon_files[] = plugin_basename( "$addon_root/$file" );
  68                  }
  69              }
  70              @closedir( $addons_dir );
  71          }
  72          if ( empty( $addon_files ) ) return $mp_addons;
  73  
  74          $active = get_option( self::option_name );
  75          if ( !is_array( $active ) ) $active = array();
  76  
  77          foreach ( $addon_files as $addon_file ) 
  78          {
  79              $file = ABSPATH . PLUGINDIR . "/$addon_file";
  80              if ( !is_readable( $file ) ) continue;
  81              $addon_data = self::get_addon_data( $file, false, false ); //Do not apply markup/translate as it'll be cached.
  82  /*
  83  $out = '';
  84  $out .= '=== ' . $addon_data['Title'] . ' ===' . "\r\n";
  85  $out .= 'Plugin Name: ' . $addon_data['Title'] . "\r\n";
  86  $out .= 'Plugin URI: ' . $addon_data['PluginURI'] . "\r\n";
  87  $out .= 'Description: ' . strip_tags( $addon_data['Description'] ) . "\r\n";
  88  $out .= 'Version: ' . $addon_data['Version'] . "\r\n\r\n";
  89  $out .= "== Description ==\r\n\r\n\r\n";
  90  $out .= "== Installation ==\r\n\r\n\r\n";
  91  $out .= "== Upgrade Notice ==\r\n\r\n\r\n";
  92  $out .= "== Frequently Asked Questions ==\r\n\r\n\r\n";
  93  $out .= "== Screenshots ==\r\n\r\n\r\n";
  94  $out .= "== Changelog ==\r\n\r\n\r\n";
  95  $fr = ABSPATH . PLUGINDIR . "/" . substr( $addon_file, 0, -4 ) . ".readme.txt";
  96  file_put_contents( $fr, $out );
  97  */
  98              if ( empty ( $addon_data['Name'] ) ) continue;
  99  
 100              $addon_data['file'] = plugin_basename( $file ) ;
 101              $addon_data['active'] = ( isset( $active[plugin_basename( $file )] ) ) ;
 102  
 103              $mp_addons[plugin_basename( $file )] = $addon_data;
 104          }
 105          uasort( $mp_addons, array( 'self', 'sort_addons' ) );
 106          $cache_addons[ $addon_folder ] = $mp_addons;
 107          wp_cache_set( 'mp_addons', $cache_addons, 'mp_addons' );
 108  
 109          return $mp_addons;
 110      }
 111  
 112  	public static function sort_addons( $a, $b ) 
 113      {
 114          return strnatcasecmp( $a['Name'], $b['Name'] );
 115      }
 116  
 117  	public static function get_addon_data( $addon_file ) 
 118      {
 119          $default_headers = array(     'Name'         => 'Plugin Name', 
 120                              'PluginURI'     => 'Plugin URI', 
 121                              'Version'         => 'Version', 
 122                              'Description'     => 'Description', 
 123                              'Author'         => 'Author', 
 124                              'AuthorURI'     => 'Author URI', 
 125                              'TextDomain'     => 'Text Domain', 
 126                              'DomainPath'     => 'Domain Path'
 127           );
 128  
 129          $addon_data = get_file_data( $addon_file, $default_headers, 'mp_addon' );
 130  
 131          //For backward compatibility by default Title is the same as Name.
 132          $addon_data['Title'] = $addon_data['Name'];
 133  
 134          return $addon_data;
 135      }
 136  
 137  	public static function is_active( $string, $header = 'Name' ) 
 138      {
 139          $mp_addons = self::get_all();
 140  
 141          foreach( $mp_addons as $k => $v )
 142          {
 143              if ( !$v['active'] ) continue;
 144              switch ($header)
 145              {
 146                  case 'key' :
 147                      if ( $k == $string ) return true;
 148                  break;
 149                  default :
 150                      if ( isset( $v[$header] ) && ( $v[$header] == $string ) ) return true;
 151                  break;
 152              }
 153          }
 154  
 155          return false;
 156      }
 157  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1