Skip to content

Commit

Permalink
Initial Plugin Implementation (#5)
Browse files Browse the repository at this point in the history
* WP Quip implementation

* Update `README`

* Update `README`

* Update `README`. Fix issue with Yoast compatibility. Change Settings page

* Reversed single and double quotes

* Update `readme.txt` for WordPress.org plugin registry

* Update `readme.txt` and `README.md`

* Update Cloud Posse logo

* Add styles to `<pre>` nodes

* Update styles of `<code>` nodes

* Update styles of `<code>` nodes

* Update styles of `<code>` nodes

* Update `ttl` logic and `README`

* Use `wp_enqueue_style` to enqueue CSS

* Update CSS

* Change CSS file name

* Update CSS. Remove CSS classes

* Update CSS

* Change CSS file name
  • Loading branch information
aknysh authored Jan 30, 2018
1 parent 44faf0a commit 48bb9b4
Show file tree
Hide file tree
Showing 13 changed files with 2,576 additions and 2 deletions.
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Numerous always-ignore extensions
*.diff
*.err
*.orig
*.log
*.rej
*.swo
*.swp
*.vi
*~
*.sass-cache
*.iml

# OS or Editor folders
.DS_Store
Thumbs.db
.cache
.project
.settings
.tmproj
*.esproj
nbproject
*.sublime-project
*.sublime-workspace

# Dreamweaver added files
_notes
dwsync.xml

# Komodo
*.komodoproject
.komodotools

# Folders to ignore
.hg
.svn
.CVS
intermediate
.idea
cache
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2017 Cloud Posse, LLC
Copyright 2017-2018 Cloud Posse, LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
158 changes: 157 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,157 @@
# wp-quip
# wp-quip

[Quip](https://quip.com/) integration for WordPress


## Introduction

`WP Quip` plugin uses WordPress shortcodes to embed [Quip](https://quip.com/) documents into WordPress pages and blog posts.

https://codex.wordpress.org/Shortcode

https://codex.wordpress.org/Shortcode_API



## Installation

1. Upload `wp-quip` folder to the `/wp-content/plugins/` directory

2. Activate the `WP Quip` plugin through the `Plugins` menu in WordPress

3. On `Quip Settings` page (menu `Settings/WP Quip`), you can update the default value for `Time-to-Live` (see [Usage](#usage) for more details)

4. On `Quip Settings` page, enter and save `Quip API Access Token`


> To generate a Quip API Access Token, visit this page: https://quip.com/dev/token . Whenever you generate a new token, all previous tokens are automatically invalidated.

#
#### WP Quip plugin Settings page

![WP Quip plugin Settings page](screenshot-1.png)



## Usage

To embed the content of a [Quip](https://quip.com/) document into a WordPress page or blog post, use the `quip` shortcode.

`quip` shortcode accepts two attributes and has the following format:

```
[quip id="mWnnAszre3MW" ttl=7200]
```

where

* `id` (Required) - The ID of the Quip document (_e.g._ https://quip.com/mWnnAszre3MW)

* `ttl` (Optional) - Time-To-Live in seconds.
After the first request to the Quip API, the plugin caches the content of the document (HTML and images) for the specified amount of time (seconds).
All consecutive requests to the same page or blog post will not call the Quip API again but instead will retrieve the document from the internal cache, making the pages faster.
After the `ttl` expires, the plugin will call the Quip API and cache the result again.
If the `ttl` attribute is not provided, the default value of 7200 seconds (2 hours) is used.
You can change the default value in `Quip Settings` (menu `Settings/WP Quip`).
If `ttl` is set to `0`, the plugin will not cache the Quip API responses, and every request to the WordPress page or blog post will call the Quip API.

__NOTE__: Setting `ttl` to `0` also invalidates the document cache.
This could be used if you change the Quip document and want the changes to be reflected on the website immediately.
In this case, update the document in Quip, set `ttl` to `0` in the `quip` shortcode,
refresh the WordPress page or blog post in the browser to invalidate the cache,
and then set `ttl` back to its original value.


#
#### WordPress post editor with a `quip` shortcode

![WordPress post editor with a 'quip' shortcode](screenshot-2.png)


#
#### Quip document embedded into a WordPress blog post

![Quip document embedded into a WordPress blog post](screenshot-3.png)


## References

For more information on Quip API, visit https://quip.com/dev/automation/documentation


## Help

**Got a question?**

File a GitHub [issue](https://github.com/cloudposse/wp-quip/issues), send us an [email](mailto:[email protected]) or reach out to us on [Gitter](https://gitter.im/cloudposse/).


## Contributing

### Bug Reports & Feature Requests

Please use the [issue tracker](https://github.com/cloudposse/wp-quip/issues) to report any bugs or file feature requests.

### Developing

If you are interested in being a contributor and want to get involved in developing `wp-quip`, we would love to hear from you! Shoot us an [email](mailto:[email protected]).

In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

1. **Fork** the repo on GitHub
2. **Clone** the project to your own machine
3. **Commit** changes to your own branch
4. **Push** your work back up to your fork
5. Submit a **Pull request** so that we can review your changes

**NOTE:** Be sure to merge the latest from "upstream" before making a pull request!


## License

[APACHE 2.0](LICENSE) © 2017-2018 [Cloud Posse, LLC](https://cloudposse.com)

See [LICENSE](LICENSE) for full details.

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.


## About

`wp-quip` is maintained and funded by [Cloud Posse, LLC][website]. Like it? Please let us know at <[email protected]>

We love [Open Source Software](https://github.com/cloudposse/)!

See [our other projects][community]
or [hire us][hire] to help build your next cloud platform.

[website]: http://cloudposse.com/
[community]: https://github.com/cloudposse/
[hire]: http://cloudposse.com/contact/

### Contributors


| [![Erik Osterman][erik_img]][erik_web]<br/>[Erik Osterman][erik_web] | [![Andriy Knysh][andriy_img]][andriy_web]<br/>[Andriy Knysh][andriy_web] |
|-------------------------------------------------------|------------------------------------------------------------------|

[erik_img]: http://s.gravatar.com/avatar/88c480d4f73b813904e00a5695a454cb?s=144
[erik_web]: https://github.com/osterman/
[andriy_img]: https://avatars0.githubusercontent.com/u/7356997?v=4&u=ed9ce1c9151d552d985bdf5546772e14ef7ab617&s=144
[andriy_web]: https://github.com/aknysh/
31 changes: 31 additions & 0 deletions css/plugin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
div.wp-quip * {
color: black !important;
}

div.wp-quip pre {
border: 0;
color: black !important;
background: #eee !important;
font-weight: 400 !important;
font-family: Courier, monospace !important;
font-size: 1rem !important;
line-height: 1.6 !important;
overflow: auto !important;
padding: 1.6em !important;
display: block !important;
white-space: pre !important;
margin: 1em 0 1.6em 0 !important;
}

div.wp-quip pre * {
color: black !important;
background: #eee !important;
font-size: 1rem !important;
}

div.wp-quip code {
border: 0;
color: black !important;
background: #eee !important;
font-size: 1rem !important;
}
125 changes: 125 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/*
* Trims zero space unicode character
*/
function cloudposse_wp_quip_plugin_trim_unicode_space( $text ) {
return preg_replace( '/[\x{200B}-\x{200D}]/u', '', $text );
}


/**
* Makes request for Quip document
*
* @param $id
* @param $ttl
* @param $args
* @param bool $include_images
*
* @return bool|mixed|simple_html_dom
* @throws Exception
*/
function cloudposse_wp_quip_plugin_get_html_document( $id, $ttl, $args, $include_images = true ) {
$url = CLOUDPOSSE_WP_QUIP_PLUGIN_QUIP_THREAD_BASE_URL . $id;
$transient_key = CLOUDPOSSE_WP_QUIP_PLUGIN_TRANSIENT_KEY . '-' . $url . "-" . $include_images;

if ( $ttl == 0 ) {
delete_transient( $transient_key );
$html = cloudposse_wp_quip_plugin_get_html_document_from_quip_api( $id, $ttl, $args, $include_images );

return $html;
}

$html = get_transient( $transient_key );
if ( $html !== false && ! empty( $html ) ) {
return $html;
}

$html = cloudposse_wp_quip_plugin_get_html_document_from_quip_api( $id, $ttl, $args, $include_images );
set_transient( $transient_key, $html, $ttl );

return $html;
}


/**
* Makes request for a document from Quip API
*
* @param $id
* @param $ttl
* @param $args
* @param bool $include_images
*
* @return bool|mixed|simple_html_dom
* @throws Exception
*/

function cloudposse_wp_quip_plugin_get_html_document_from_quip_api( $id, $ttl, $args, $include_images = true ) {
$url = CLOUDPOSSE_WP_QUIP_PLUGIN_QUIP_THREAD_BASE_URL . $id;
$response = wp_remote_get( $url, $args );

if ( is_wp_error( $response ) ) {
throw new Exception( $response->get_error_message() );
}

if ( $response['response'] && $response['response']['code'] && $response['response']['code'] !== 200 ) {
$err = $response['body'];
throw new Exception( $err ? $err : 'Quip API Error' );
}

$body = wp_remote_retrieve_body( $response );
$body_obj = json_decode( $body );
$html = str_get_html( $body_obj->html );

if ( $html && is_object( $html ) ) {

foreach ( $html->find( 'p' ) as $p ) {
$p->innertext = cloudposse_wp_quip_plugin_trim_unicode_space( $p->innertext );
}

if ( $include_images ) {
foreach ( $html->find( 'img' ) as $img ) {
$src = str_replace( '/blob/', '', $img->attr['src'] );
$image = cloudposse_wp_quip_plugin_create_encoded_image( $src, $ttl, $args );
$img->outertext = "<img src='data:image/jpeg;base64," . $image . "'>";
}
}
}

$html = '<div class="wp-quip">' . $html . '</div>';

return $html;
}


/*
* Makes request for image, converts binary format to base64
*/
function cloudposse_wp_quip_plugin_create_encoded_image( $src, $ttl, $args ) {
$transient_key = CLOUDPOSSE_WP_QUIP_PLUGIN_TRANSIENT_KEY . '-' . $src;
$image = get_transient( $transient_key );
if ( $image !== false && ! empty( $image ) ) {
return $image;
} else {
$url = CLOUDPOSSE_WP_QUIP_PLUGIN_QUIP_IMAGE_BASE_URL . $src;
$response = wp_remote_retrieve_body( wp_remote_get( $url, $args ) );
$image = base64_encode( $response );
set_transient( $transient_key, $image, $ttl );

return $image;
}
}


/*
* Logs messages
*/
function cloudposse_wp_quip_plugin_log( $message ) {
if ( WP_DEBUG === true ) {
if ( is_array( $message ) || is_object( $message ) ) {
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
Binary file added images/CloudPosse_Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 48bb9b4

Please sign in to comment.