Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to embed my code at BitBucket in wp post, anyone can suggest me, a good plugin or anytrick to do it?

share|improve this question

2 Answers 2

Here try the below code, it should give you a good start.

// requrires 3 arguments
// user, project, path
// usage e.g: [bitbucket user="jespern" project="django-piston" path="piston/utils.py"]
function bitbucket_code( $atts ) {
    extract( $atts );
    global $post;
    $path_to_name = str_replace("/", "_", $path);
    $meta_key = "bitbucket_{$user}_{$project}_{$path_to_name}";
    $code = get_post_meta( $post->ID, $meta_key, true );
    if ( !$code ) {
        $bitbucket_url = "https://api.bitbucket.org/1.0/repositories/$user/$project/raw/tip/$path";
        // just in case its not there ;)
        if( !class_exists( 'WP_Http' ) )
                include_once( ABSPATH . WPINC. '/class-http.php' );
        $request = new WP_Http();
        $response = $request->request( $bitbucket_url );
        $code = htmlentities( $response['body'] );
        update_post_meta( $post->ID, $meta_key, $code );
    }
    return '<pre>'. $code . '</pre>';
}               
add_shortcode('bitbucket', 'bitbucket_code');
share|improve this answer

You can to use of WP Bucket for your plugins or themes.

Create plugin "WB Embed Code" using "WP Bucket" :

<?php 
/*
    Plugin Name: WB Embed Code
    Plugin URI: https://bitbucket.org/khosroblog/wb-embed-code/
    Description: A simple plugin created by the WP Bucket plugin for embed bitbucket codes in the wordpress posts with shortcode.
    Author: Hadi Khosrojerdi
    Version: 0.1.0
    Author URI: http://khosroblog.com
    License: GNU General Public License v2 or later 
*/


    /*
    * Set http arguments.
    * 
    * @since 0.1.0
    * @param array $args An array of http arguments.
    */
    function set_http_args( $http_args ){
        // To avoid error display :
        // WARNING: C:\wamp\www\wordpress\wp-includes\class-http.php:1765 - gzinflate(): data error
        $http_args['decompress'] = false;
        return $http_args;
    }
    add_action("wprest_http_request", "set_http_args");


    /*
    * Embed bitbucket code in wordpress posts.
    * 
    * @since 0.1.0
    * @param array $args An array of arguments like repo_slug, class, is_private.
    * @usage e.g : [bitbucket revision="v0.1.2" repo_slug="wp_bucket" owner="khosroblog" path="readme.txt" is_private="0" ]
    * @return html | null .
    */
    function wb_embed_code( $args=array() ){
        global $authordata, $WP_Bucket;
        if( !is_a( $WP_Bucket, "WP_Bucket") ){ return; }

        $defaults = array(
            "repo_slug"     =>  "", // repository slug
            "owner"         =>  "", // username
            "revision"      =>  "master", //A SHA1 value for commit. also you can determine a branch , bookmark or tag name for that.
            "path"          =>  "", // a file or directory exists in the repository
            "is_private"    =>  0
        );

        $args = wp_parse_args( $args, $defaults );
        extract( $args );

        $author_id = (int) $authordata->ID;
        // for public reposities you don't need to user authenticate, so tokens should be null.
        if( !$is_private ){
            $WP_Bucket->config(array(
                "oauth_token"        => null,
                "oauth_token_secret" => null,
            ));

        // for private repositories author should be authenticate. 
        // so click on the "login by bitbucket" button in the "wp-bucket-test" page. ( http://yoursite/wp-bucket-test ) 
        }else{
            $WP_Bucket->config( "wp_user_id=" . $author_id );
        }

        $repository = $WP_Bucket->api("/1.0/repositories/$owner/$repo_slug/raw/$revision/$path");
        if( !is_wp_error( $repository ) ){
            echo "<pre>" . esc_html( $repository ) . "</pre>" ;
        }else{
            //var_dump( $repository  );
        }
    }
    add_shortcode("bitbucket", "wb_embed_code");

?>

You can to download plugin "WB Embed Code" from the following link:

https://bitbucket.org/khosroblog/wb-embed-code/wiki/Home

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.