. */ add_action( 'plugins_loaded', array ( B5F_All_Your_Stack_Posts::get_instance(), 'plugin_setup' ) ); register_activation_hook( __FILE__, array( 'B5F_All_Your_Stack_Posts', 'register_project_template' ) ); register_deactivation_hook( __FILE__, array( 'B5F_All_Your_Stack_Posts', 'deregister_project_template' ) ); class B5F_All_Your_Stack_Posts { protected static $instance = NULL; public $plugin_url = NULL; public $plugin_path = NULL; public $plugin_slug = NULL; public $locale_slug = NULL; public $frontend; public static function get_instance() { NULL === self::$instance and self::$instance = new self; return self::$instance; } public function plugin_setup() { $this->plugin_url = plugins_url( '/', __FILE__ ); $this->plugin_path = plugin_dir_path( __FILE__ ); $this->plugin_slug = plugin_basename(__FILE__); $this->locale_slug = dirname( plugin_basename( __FILE__ ) ); include_once('includes/class-aysp-metabox.php'); new B5F_AYSP_Metabox( $this->plugin_path, $this->plugin_url ); # Description translation $desc = __( 'Grab Questions or Answers from a given user in a given Stack Exchange site and display them in a simple page ready to print (using your system capabilities).', 'aysp' ); # Load translation files $this->plugin_locale( 'aysp' ); if( !is_admin() ) { include_once('includes/class-aysp-frontend.php'); $this->frontend = new B5F_AYSP_Frontend( $this->plugin_path, $this->plugin_url ); } add_filter( 'upgrader_post_install', array( $this, 'refresh_template' ), 10, 3 ); } public function __construct() {} public function refresh_template( $true, $hook_extra, $result ) { if( isset( $hook_extra['plugin'] ) && $this->plugin_slug == $hook_extra['plugin'] ) { self::deregister_project_template(); self::register_project_template(); } return $true; } /** * Call copy template function on Plugin Activation * */ public static function register_project_template() { // Get source and destination for copying from the plugin to the theme directory $destination = self::get_template_destination(); $source = self::get_template_source(); // Copy the template file from the plugin to the destination self::copy_page_template( $source, $destination ); } /** * Remove the plugin template from theme directory * * Returns TRUE if the file not exists, or if the removal is successful * Returns FALSE if the file exists, but was not removed * * @return boolean */ public static function deregister_project_template() { // Get the path to the theme $template_path = get_stylesheet_directory() . '/template-stackapp.php'; // If the template file is in the theme path, delete it. if( file_exists( $template_path ) ) return unlink( $template_path ); return true; } /** * The destination to the plugin directory relative to the currently active theme * * From page-template-plugin * * @return string */ private static function get_template_destination() { return get_stylesheet_directory() . '/template-stackapp.php'; } /** * The path to the template file relative to the plugin. * * From page-template-plugin * * @return string */ private static function get_template_source() { return dirname( __FILE__ ) . '/includes/template-stackapp.php'; } /** * Translation * * @uses load_plugin_textdomain, plugin_basename * @since 2.0.0 * @return void */ public function plugin_locale( $domain ) { $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); $mo_name = $domain . '-' . $locale . '.mo'; $mo_path = WP_LANG_DIR . '/plugins/' . $this->locale_slug . '/' . $mo_name; load_textdomain( $domain, $mo_path ); load_plugin_textdomain( $domain, FALSE, $this->locale_slug . '/languages' ); } /** * Does the actual copy from plugin to template directory * * From page-template-plugin * * @param string $source * @param string $destination */ private static function copy_page_template( $source, $destination ) { // Check if template already exists. If so don't copy it; otherwise, copy if if( ! file_exists( $destination ) ) { // Create an empty version of the file touch( $destination ); // Read the source file starting from the beginning of the file if( null != ( $handle = @fopen( $source, 'r' ) ) ) { // Read the contents of the file into a string. // Read up to the length of the source file if( null != ( $content = fread( $handle, filesize( $source ) ) ) ) { // Relinquish the resource fclose( $handle ); } } // Now open the file for reading and writing if( null != ( $handle = @fopen( $destination, 'r+' ) ) ) { // Attempt to write the contents of the string if( null != fwrite( $handle, $content, strlen( $content ) ) ) { // Relinquish the resource fclose( $handle ); } } } } }