Skip to content

Commit

Permalink
Merge pull request #28 from crowdfavorite/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
cristirusu authored Mar 12, 2018
2 parents 6975924 + acdcc8d commit 0a41b65
Show file tree
Hide file tree
Showing 4,308 changed files with 1,434,898 additions and 483 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ _A collaboration hub for [Capsule](http://crowdfavorite.com/capsule/) content._

This is a WordPress theme. Install it as usual to turn a WordPress instance into a Capsule Server. [Learn more.](http://crowdfavorite.com/capsule/)

Minimum required WordPress version: 3.5
Minimum required WordPress version: 4.1

Target browser compatibility:

Expand All @@ -29,7 +29,8 @@ Developers, contribute on [GitHub](https://github.com/crowdfavorite/wp-capsule-s
- Capsule Server: https://github.com/crowdfavorite/wp-capsule-server
- Capsule UI: https://github.com/crowdfavorite/wp-capsule-ui

Capsule UI is a submodule of both Capsule and Capsule Server, delivering the UI for both packages.
Capsule UI is a subrepo of both Capsule and Capsule Server, delivering the UI for both packages.
Read more about Git subrepo at https://github.com/ingydotnet/git-subrepo

---

Expand Down
68 changes: 68 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.capsule-welcome {
background: #222;
color: #fff;
margin: 30px 10px 10px 0;
padding: 15px;
}
.capsule-welcome h1 {
color: #fff;
font-weight: normal;
line-height: 100%;
margin: 0 0 10px 0;
}
.capsule-welcome p {
font-weight: normal;
line-height: 100%;
margin: 0;
}
.capsule-welcome a,
.capsule-welcome a:visited {
color: #f8f8f8;
}

.capsule-welcome {
background: #222;
color: #fff;
margin-top: 20px;
width: 100%;
}
.capsule-welcome h1 {
font-weight: normal;
line-height: 100%;
margin: 0;
padding: 15px 0 10px 15px;
}
.capsule-welcome p {
font-weight: normal;
line-height: 100%;
margin: 0;
padding: 0 0 15px 15px;
}
.capsule-admin h3 {
margin-top: 25px;
}
.capsule-admin hr {
border: 0;
border-top: 1px solid #999;
margin: 0 100px 10px;
}
.capsule-screenshot {
border: 1px solid #ccc;
padding: 2px;
width: 90%;
}
.capsule-doc-col-left {
float: left;
margin-right: 30px;
margin-bottom: 30px;
max-width: 500px;
width: 45%;
}
.capsule-doc-col-right {
clear: right;
float: left;
margin-bottom: 30px;
max-width: 500px;
width: 45%;
}

29 changes: 29 additions & 0 deletions assets/js/admin-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(function ($) {
var CapsuleAdminProfile = {
init: function() {
$profile = $('.capsule-profile');
$profile.prependTo($profile.closest('form'));
// Reset API key.
$('#cap-regenerate-key').on('click', function (e) {
var id = $(this).data('user-id');
var url = $(this).attr('href');
e.preventDefault();
$.post(
url, {
action: 'cap_new_api_key',
user_id: id
},
function(data) {
if (data) {
$('#cap-api-key').html(data);
}
}
);
});
}
};

$(document).ready(function () {
CapsuleAdminProfile.init();
});
})(jQuery);
16 changes: 16 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function ($) {
var CapsuleAdmin = {
init: function() {
// Menu.
$('#adminmenu').find('a[href*="admin.php?page=capsule-projects"]')
.attr('href', 'edit-tags.php?taxonomy=projects')
.end()
.find('a[href*="admin.php?page=capsule-users"]')
.attr('href', 'users.php');
}
};

$(document).ready(function () {
CapsuleAdmin.init();
});
})(jQuery);
221 changes: 45 additions & 176 deletions capsule-server-import-export.php
Original file line number Diff line number Diff line change
@@ -1,207 +1,76 @@
<?php
class Capsule_Server_Import_Post {

// $data includes three keys:
function __construct($user_id, $post, $tax_data) {
$this->api_key = $api_key;
$this->post = $post;
$this->tax_data = $tax_data;
$this->local_post_id = 0;

$this->user_id = $user_id;
$this->post['post_author'] = $this->user_id;
}

function import() {
$this->import_post();
$this->import_terms();
}

function import_post() {
$this->post['guid'] = $this->post['guid'].'_user_'.$this->post['post_author'];

// Update post if it already exists on the server
$local_id = self::get_post_id_by_guid($this->post['guid']);
if (!$local_id) {
unset($this->post['ID']);
}
else {
$this->post['ID'] = $local_id;
}

remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');

$this->local_post_id = wp_insert_post($this->post);
//@TODO throw error?
}

function import_terms() {
// All the taxonomies sent from the client
$taxonomies = $this->tax_data['taxonomies'];
// The term mappings for each taxonomy
$tax_input = $this->tax_data['tax_input'];
// The taxonomies which had a local mapping
$mapped_taxonomies = $this->tax_data['mapped_taxonomies'];

if (!empty($taxonomies)) {
foreach ($taxonomies as $taxonomy) {
if (isset($tax_input[$taxonomy])) {
$terms = (array) $tax_input[$taxonomy];

// wp_set_post_terms will use the integers as names if not the correct type
if (in_array($taxonomy, $mapped_taxonomies)) {
// This handles forcing hierarchial taxonomies to be have terms as integers

wp_set_post_terms($this->local_post_id, $terms, $taxonomy);
}
else {
// Check if hierarchical, need to pass IDs
if(is_taxonomy_hierarchical($taxonomy)) {
$terms_as_id = array();

foreach ($terms as $term) {
// Get term, create if not exists
$term_id = capsule_create_term($term, $taxonomy);

if ($term_id) {
$terms_as_id[] = $term_id;
}
}

wp_set_post_terms($this->local_post_id, $terms_as_id, $taxonomy);
}
else {
wp_set_post_terms($this->local_post_id, $terms, $taxonomy);
}
}
}
else {
// There were no terms passed, so unset the current terms
wp_set_post_terms($this->local_post_id, array(), $taxonomy);
}
}
}
}

public static function get_post_id_by_guid($guid) {
global $wpdb;
$sql = $wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE guid = %s
", $guid);

$post_id = $wpdb->get_var($sql);

if (!empty($post_id)) {
return $post_id;
}
else {
return false;
}
}

//@TODO remove user from blog, delete their API key
function cap_server_remove_user() {}
}

class Capsule_Server_Export_Terms {

function __construct($taxonomies = array()) {
$this->taxonomies = $taxonomies;
}

/**
* @return array of formatted taxonomies:
* 'taxonomy_1' => array(
* 'term-slug' => array(
* 'id' => 1,
* 'name' => 'Amazing Term',
* 'description' => 'This term is amazing AND useful',
* ),
* 'term-slug-2' ...
* ),
* 'taxonomy_2' ...
*/
function get_terms() {
$taxonomy_array = array();

$terms = get_terms($this->taxonomies, array(
'hide_empty' => false,
'orderby' => 'slug',
'order' => 'ASC',
));
if (is_array($terms)) {
foreach ($terms as $term) {
$taxonomy_array[$term->taxonomy][$term->slug] = array(
'id' => $term->term_id,
'name' => $term->name,
'description' => $term->description,
'taxonomy' => $term->taxonomy,
);
}
}
return $taxonomy_array;
<?php
/**
* Implement post import and taxonomy terms export.
*/

require_once 'inc/class-capsule-server-import-post.php';
require_once 'inc/class-capsule-server-export-terms.php';

/**
* Import export controller function.
*
* @return void
*/
function capsule_server_controller() {
if ( empty( $_POST['capsule_server_action'] ) ) {
return;
}
}

function capsule_server_controller() {
switch ($_POST['capsule_server_action']) {
switch ( $_POST['capsule_server_action'] ) {
case 'insert_post':
$response = array(
'result' => 'error',
);
// Cannot use nonce here as they're salted with unique keys, going to have to rely on api key.
if (isset($_POST['capsule_client_post_data'])) {
if ( isset( $_POST['capsule_client_post_data'] ) ) {
$data = $_POST['capsule_client_post_data'];
if (isset($data['post']) && isset($data['tax']) && isset($data['api_key'])) {
if ($user_id = capsule_server_validate_user($data['api_key'])) {
$capsule_import = new Capsule_Server_Import_Post($user_id, $data['post'], $data['tax']);
$post_id = $capsule_import->import();

if ($capsule_import->local_post_id != 0) {
if ( isset( $data['post'], $data['tax'], $data['api_key'] ) ) {
$user_id = capsule_server_validate_user( $data['api_key'] );
if ( $user_id > 0 ) {
$capsule_import = new Capsule_Server_Import_Post( $user_id, $data['post'], $data['tax'] );
$post_id = $capsule_import->import();

if ( $capsule_import->local_post_id > 0 ) {
$response = array(
'result' => 'success',
'data' => array(
'permalink' => get_permalink($capsule_import->local_post_id),
'data' => array(
'permalink' => get_permalink( $capsule_import->local_post_id ),
),
);
}
}
else {
header('HTTP/1.1 401 Unauthorized');
} else {
header( 'HTTP/1.1 401 Unauthorized' );
die();
}
}
}
header('Content-type: application/json');
echo json_encode($response);
die();
wp_send_json( $response );
break;

case 'get_terms':
if (isset($_POST['capsule_client_post_data']['api_key']) && capsule_server_validate_user($_POST['capsule_client_post_data']['api_key'])) {
$taxonomies = isset($_POST['capsule_client_post_data']['taxonomies']) ? $_POST['capsule_client_post_data']['taxonomies'] : array();
$term_exporter = new Capsule_Server_Export_Terms($taxonomies);
echo serialize($term_exporter->get_terms());
}
else {
header('HTTP/1.1 401 Unauthorized');
if ( isset( $_POST['capsule_client_post_data']['api_key'] ) && capsule_server_validate_user( $_POST['capsule_client_post_data']['api_key'] ) ) {
$taxonomies = isset( $_POST['capsule_client_post_data']['taxonomies'] ) ? $_POST['capsule_client_post_data']['taxonomies'] : array();
$term_exporter = new Capsule_Server_Export_Terms( $taxonomies );
echo wp_json_encode( $term_exporter->get_terms() );
} else {
header( 'HTTP/1.1 401 Unauthorized' );
}
die();
break;

case 'test_credentials':
if (isset($_POST['capsule_client_post_data']['api_key']) && capsule_server_validate_user($_POST['capsule_client_post_data']['api_key'])) {
if ( isset( $_POST['capsule_client_post_data']['api_key'] ) && capsule_server_validate_user( $_POST['capsule_client_post_data']['api_key'] ) ) {
echo 'authorized';
}
else {
header('HTTP/1.1 401 Unauthorized');
} else {
header( 'HTTP/1.1 401 Unauthorized' );
}
die();
break;

default:
break;
}
}
add_action('wp_loaded', 'capsule_server_controller');
// Come in after taxonomies are typically registered but before the wp-gatekeeper plugin.
add_action( 'init', 'capsule_server_controller', 11 );

Loading

0 comments on commit 0a41b65

Please sign in to comment.