Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#istanbulphp ornek eklenti
<?php
/*
Plugin Name: Origin Content
Plugin URI: http://github.com/mustafauysal/origin-content
Description: Origin resource replacement for media items.
Version: 1.0
Author: Mustafa Uysal
Author URI: http://wphandle.com
License: GPLv2 (or later)
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: wph-origin-content
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class WPH_Origin_Content {
const TEXT_DOMAIN = 'wph-origin-content';
public function __construct() {
add_action( 'init', array( $this, 'setup' ) );
}
public function setup() {
load_plugin_textdomain( self::TEXT_DOMAIN, false, basename( dirname( __FILE__ ) ) . '/languages' );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
if ( ! is_admin() ) {
add_filter( 'pre_option_upload_url_path', array( $this, 'origin_content_url' ) );
}
add_filter( 'the_content', array( $this, 'origin_replacement' ) );
}
public function admin_menu() {
add_options_page(
__( 'Origin Content Options', self::TEXT_DOMAIN ),
__( 'Origin Content', self::TEXT_DOMAIN ),
'manage_options',
'wph-origin-content',
array( $this, 'options' )
);
}
public function admin_init() {
register_setting( 'wph-origin-content-page', 'wph_origin_content_url', array( $this, 'sanitize_url' ) );
add_settings_section( 'default', '', array( $this, 'settings_section' ), 'wph-origin-content-page' );
add_settings_field( 'remote_url', __( 'Origin Content URL', self::TEXT_DOMAIN ), array( $this, 'url_field' ), 'wph-origin-content-page' );
}
public function settings_section() {
printf( "%s <b>%s</b>",
__( "Please enter remote content url Ex: ", self::TEXT_DOMAIN ),
__( "http://example.com/wp-content/uploads", self::TEXT_DOMAIN )
);
}
public function url_field() {
echo '<input type="text" size="50" name="wph_origin_content_url"
value="' . esc_url( get_option( ( 'wph_origin_content_url' ) ) ) . '"/>';
}
public function options() {
echo '<div class="wrap">';
echo sprintf( "<h2>%s</h2>", __( "Origin Content Options", self::TEXT_DOMAIN ) );
echo '<form action="options.php" method="post">';
settings_fields( 'wph-origin-content-page' );
do_settings_sections( 'wph-origin-content-page' );
submit_button( __( 'Save Changes', self::TEXT_DOMAIN ) );
echo '</form></div>';
}
public function origin_content_url() {
$remote_url = get_option( 'wph_origin_content_url' );
if ( isset( $remote_url ) ) {
return $remote_url;
}
$wp_upload_dir = wp_upload_dir();
return $wp_upload_dir['baseurl'];
}
public function origin_replacement( $content ) {
$origin_content_url = $this->origin_content_url();
$wp_content_url = content_url( 'uploads' );
$content = str_replace( $wp_content_url, $origin_content_url, $content );
return $content;
}
// Sanitize and validate input.
public function sanitize_url( $input ) {
return sanitize_text_field( $input );
}
}
new WPH_Origin_Content();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.